从最后一次触碰开始60秒后为ViewFlipper启动翻转



我的应用程序包含一些图像的ViewFlipper。app启动时,ViewFlipper startflipping()。当用户触摸屏幕ViewFlipper stopflipping()时。我必须在最后一次触碰ViewFlipper后60秒重新开始翻转。我的类实现了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;
    }

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

static long timeLastEvent=0;
public long getTimeOfLastEvent() {
        long duration = System.currentTimeMillis() - timeLastEvent;
        timeLastEvent = System.currentTimeMillis();
        return duration;
    }

我的问题是:我应该在哪里调用getTimeOfLastEvent() ?如果我把它放在onTouch()上,我将永远不会抓住getTimeOfLastEvent==60000的时刻,对吧?

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

Handler myHandler = new Handler();

还需要一个可以再次开始翻转的Runnable(也需要在Activity中声明):

private Runnable flipController = new Runnable() {
  @Override
  public void run() {
    vf.startFlipping();
  }
};

然后在onClick中,您只需将Runnable发布到Handler上,但延迟了60秒:

myHandler.postDelayed( flipController, 60000 );

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

最新更新