从去年触摸60秒后StartFlipping为ViewFlipper [英] StartFlipping for ViewFlipper after 60seconds from last touch
问题描述
我的应用程序包含一个 ViewFlipper
对某些图像。应用程序启动时,在 ViewFlipper
startflipping()
。当用户触摸屏幕 ViewFlipper stopflipping()
。我必须这样做,从去年接触60秒后,ViewFlipper重新开始翻转。我的类实现 onTouchListener
我有这种方法 onTouch
:
My app contains a ViewFlipper
with some images. when app starts, the ViewFlipper
startflipping()
. When user touch the screen ViewFlipper stopflipping()
. I must do that after 60 seconds from last touch, ViewFlipper to start again flipping. My class implements onTouchListener
and I have this method 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;
}
和我发现这个方法,找到最后一抹时间:
and I found this method, for finding time of the last touch :
static long timeLastEvent=0;
public long getTimeOfLastEvent() {
long duration = System.currentTimeMillis() - timeLastEvent;
timeLastEvent = System.currentTimeMillis();
return duration;
}
我的问题是:我应该在哪里叫 getTimeOfLastEvent()
?如果我把它放在 onTouch()
我永远不会赶上那一刻getTimeOfLastEvent == 60000,对吧?
My question is : where should I call the getTimeOfLastEvent()
? If I put it on onTouch()
I will never catch the moment when getTimeOfLastEvent==60000, right?
推荐答案
你应该做的就是创建一个 处理程序
(应该是你的活动的实例变量
并应在的onCreate
)
What you should do is create a Handler
(should be an instance variable of your Activity
and should be initialized during onCreate
):
Handler myHandler = new Handler();
此外,你需要一个的Runnable
,可以重新开始翻转(还需要你的活动中声明
):
Also you will need a Runnable
that can start the flipping again (also needs to be declared within your Activity
):
private Runnable flipController = new Runnable() {
@Override
public void run() {
vf.startFlipping();
}
};
然后在你的的onClick
您刚刚发布的Runnable
在处理程序
但延迟60秒钟:
Then in your onClick
you just post the Runnable
on the Handler
but delayed by 60 seconds:
myHandler.postDelayed( flipController, 60000 );
张贴延迟手段:快跑60秒这个code
Posting it delayed means: "Run this code in 60 seconds".
这篇关于从去年触摸60秒后StartFlipping为ViewFlipper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!