从最后一次触摸开始60秒后开始翻转ViewFlipper

Gab*_*lle 3 android touch viewflipper

我的应用程序包含ViewFlipper一些图像.当应用程序启动时,ViewFlipper startflipping().当用户触摸屏幕时ViewFlipper stopflipping().我必须在最后一次触摸60秒后执行此操作,ViewFlipper再次开始翻转.我的类实现了onTouchListener,我有这个方法onTouch:

public boolean onTouch(View arg0, MotionEvent arg1) {


        switch (arg1.getAction()) {
        case MotionEvent.ACTION_DOWN: {

            downXValue = arg1.getX();
            break;
        }

        case MotionEvent.ACTION_UP: {

            currentX = arg1.getX();


            if (downXValue < currentX) {
                // Set the animation
                vf.stopFlipping();
                vf.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_right_out));
                vf.setInAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_right_in));
                // Flip!
                vf.showPrevious();
            }


            if (downXValue > currentX) {
                // Set the animation
                vf.stopFlipping();
                vf.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_left_out));
                vf.setInAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_left_in));
                // Flip!
                vf.showNext();
            }

            if (downXValue == currentX) {
                final int idImage = arg0.getId();

                vf.stopFlipping();
                System.out.println("id" + idImage);
                System.out.println("last touch "+getTimeOfLastEvent());

            }
            break;
        }
        }

        // if you return false, these actions will not be recorded
        return true;
    }
Run Code Online (Sandbox Code Playgroud)

我发现了这种方法,用于找到最后一次触摸的时间:

static long timeLastEvent=0;
public long getTimeOfLastEvent() {

        long duration = System.currentTimeMillis() - timeLastEvent;
        timeLastEvent = System.currentTimeMillis();
        return duration;
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是:我应该在哪里打电话getTimeOfLastEvent()?如果我穿上它,onTouch()我将永远不会抓住getTimeOfLastEvent == 60000的那一刻,对吧?

kas*_*rch 5

你应该做的是创建一个Handler(应该是你的实例变量,Activity应该在初期化时onCreate):

Handler myHandler = new Handler();
Run Code Online (Sandbox Code Playgroud)

此外,您将需要一个Runnable可以再次开始翻转(也需要在您的内部声明Activity):

private Runnable flipController = new Runnable() {
  @Override
  public void run() {
    vf.startFlipping();
  }
};
Run Code Online (Sandbox Code Playgroud)

然后在你的onClick,你刚刚发布RunnableHandler,但通过延迟60秒钟:

myHandler.postDelayed( flipController, 60000 );
Run Code Online (Sandbox Code Playgroud)

将其延迟发布意味着:"在60秒内运行此代码".