0

我为视频播放器创建了一个自定义控制面板。现在我想给出一个类似于默认 MediaController 的效果,其中面板在触摸屏幕时变得可见,并且在最后一次触摸后再次变为不可见。我可以使用这种类型的代码。

Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(60000);
                } catch (InterruptedException e) {
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // make the panel invisible 
                    }
                });
            }
        };

我可以在触摸屏幕时启动线程,并在 60 秒后使其不可见。但在我的情况下,如果用户在这 60 秒之间再次触摸屏幕,则面板应该在最后一次触摸后 60 秒后消失。如何考虑这种情况?

4

2 回答 2

1

I would recommend using a combination of Runnables and a Handler. You can do Handler calls using postDelayed() to do something after, say, 60 seconds.

Here's an example:

private Handler mHandler = new Handler();

mHandler.post(showControls); // Call this to show the controls

private Runnable showControls = new Runnable() {    
   public void run() {
      // Code to show controls
      mHandler.removeCallbacks(showControls);
      mHandler.postDelayed(hideControls, 60000);
   }
};

private Runnable hideControls = new Runnable() {
   public void run() {
      // Code to hide the controls
   }
};
于 2011-09-28T10:06:17.650 回答
1

Simply delete/cancel current timer.

Btw, you should not do it by Thread, but by posting message to a Handler. Such future timer task doesn't need another thread.

于 2011-09-28T10:06:22.693 回答