我的应用程序包含一个带有一些图片的ViewFlipper
,当应用程序启动时,ViewFlipper
会自动开始翻转。当用户触摸屏幕时,ViewFlipper
会停止翻转。我必须在最后一次触摸后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;
}
我发现了一种寻找最近一次触摸时间的方法:
static long timeLastEvent=0;
public long getTimeOfLastEvent() {
long duration = System.currentTimeMillis() - timeLastEvent;
timeLastEvent = System.currentTimeMillis();
return duration;
}
我的问题是:我应该在什么地方调用getTimeOfLastEvent()
?如果我将其放在onTouch()
中,我永远也无法捕捉到getTimeOfLastEvent==60000
的时刻,对吗?