search

Home  >  Q&A  >  body text

java - 做一个视频播放器,想实现一个功能,如果3秒不点击屏幕任何,则下方一个界面布局(显示按钮那些的)自动消失。

就是这个功能不知道怎么实现,就像很多视频播放器那种,如果点击屏幕任何,下方界面布局(显示按钮那些的)就显示出来,然后等几秒钟没有点击,就消失。
先贴个代码:

 sv.setOnTouchListener(new View.OnTouchListener() {
           @Override
           public boolean onTouch(View v, MotionEvent event) {
               if (linearLayout.getVisibility()==View.INVISIBLE)
                   linearLayout.setVisibility(View.VISIBLE);
              Timer timer=new Timer();
               timer.schedule(new TimerTask() {
                   @Override
                   public void run() {
                       linearLayout.setVisibility(View.VISIBLE);
                   }
               },3000);
               return false;
           }
       });

这种方法不行的,会抛出异常,但是我感觉这个思路没有错,肯定是设置一个surfaceview的触摸事件监听器,但是真的不知道如何实现。以上抛出只有原本的线程能操作ui线程,也就是说主要问题是timer这个线程无法操作setvisibility,但是如果不加入其它线程,又如何实现这个功能,不可能在监听器里面用一个延迟函数,如果在这3秒内又又一次触摸时间,那肯定是要刷新这个时间的啊。。的确没想到解决方法。。。

高洛峰高洛峰2802 days ago768

reply all(6)I'll reply

  • 迷茫

    迷茫2017-04-18 10:49:41

    No need for timer

            Handler mHandler = new Handler();
            Runnable mRunnable;
    
            mRunnable = new Runnable() {
                @Override
                public void run() {
                   //隐藏view
                }
            };
            
            myView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_DOWN){
                        mHandler.removeCallbacks(mRunnable);
                        //显示你的view
                    }else if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL){
                        mHandler.postDelayed(mRunnable,3000);
    
                    }
                    return false;
                }
            });

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:49:41

    You are operating the UI thread in a child thread. This is prohibited. Add a handler

           
    
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            linearLayout.setVisibility(View.VISIBLE);
        }
    };
               
    timer.schedule(new TimerTask() {
            @Override
            public void run() {
                 //linearLayout.setVisibility(View.VISIBLE);
                 mHandler.sendEmptyMessage(0);
            }
    },3000);

    reply
    0
  • PHPz

    PHPz2017-04-18 10:49:41

    runOnUiThread

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:49:41

    Just send a delayed handler to hide the layout. There will be unexpected problems when operating the UI in a child thread.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:49:41

    Try using the Handler.postDelay() method.

    Time lastTime;
    触摸屏幕
    if(View.VISIBLE){
      setVisibility(View.INVISIBLE);
    }
      else{
      setVisibility(View.VISIBLE);
      lastTime= new Time();
      Handler.postDelay(3000);
    }
    
    Handler.handleMessage{
      if(View.VISIBLE){
      if(new Time()-lastTime>3S){
        setVisibility(View.INVISIBLE);
        }
      }
    }

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:49:41

    Use directly

    new Handler().postDelayed(new Runnable(){
        @Override
        public void run(){
            method()
        }
    }, 3000);

    reply
    0
  • Cancelreply