2011-11-28 49 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