2011-11-28 47 views
3

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

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的那一刻,对吧?

回答

5

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

Handler myHandler = new Handler(); 

你也将需要一个Runnable,可以重新开始翻转(也需要在您的Activity中声明):

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

然后在你的onClick你刚才发布RunnableHandler但延迟了60秒:

myHandler.postDelayed(flipController, 60000); 

张贴延迟意味着:“在60秒内运行此代码”。

+0

作品...非常感谢:) – Gabrielle