2013-12-18 92 views
12

我試圖讓ValueAnimator重複一次。我用SeekBarListView中使用它。由於某種原因,ValueAnimator將完成,再次觸發onAnimationEnd(),但當到達結尾時,onAnimationEnd()永遠不會被第二次調用。ValueAnimator只重複一次

@Override 
    public View getContentView(int position, View convertView, ViewGroup parent) { 
     ... 
     setupTimerBar(t); 
     ... 
    } 


    private AnimatorListenerAdapter generateNewAnimatorListenerAdapter(final TylersContainer t) { 
     return new AnimatorListenerAdapter() { 
      @Override 
      public void onAnimationEnd(Animator animation) { 
       setupTimerBar(t); 
      } 
     }; 
    } 

    private void setupTimerBar(TylersContainer t) 
    {  
     View view = t.getView(); 
     BusTime arrivalTime = t.getBusTime(); 
     int minutes = BusTime.differenceInMiuntes(arrivalTime, BusTime.now()); 
     long milliseconds = minutes * 60 * 1000; 

     final TimerBar seekBar = (TimerBar) view.findViewById(R.id.SeekBar); 

     int progress = Utility.setProgress(arrivalTime, seekBar.getMax()); 
     long duration = Utility.setAnimationDuration(progress); 

     seekBar.setProgress(progress); 
     seekBar.setAnimationDuration(duration); 
     seekBar.setAnimationStartDelay(milliseconds); 
     seekBar.setAnimatorListenerAdapter(generateNewAnimatorListenerAdapter(t)); 
    } 

seekBar對象實際上是包含SeekBarValueAnimator的自定義對象,這裏的相關位:

//Constructor 
    public TimerBar(Context context) { 
     super(context); 

     startTime = Calendar.getInstance(); 
     valueAnimator = ValueAnimator.ofInt(0, getMax()); 

     //Override the update to set this object progress to the animation's value 
     valueAnimator.addUpdateListener(new AnimatorUpdateListener() { 

      @Override 
      public void onAnimationUpdate(ValueAnimator animation) {  
       int animProgress = (Integer) animation.getAnimatedValue(); 
       setProgress(animProgress); 

      } 
     }); 
    } 

    //Specify the start time by passing a long, representing the delay in milliseconds 
    public void setAnimationStartDelay(long milliseconds){ 

     //Set the delay (if need be) and start the counter 
     if(milliseconds > 0) 
      valueAnimator.setStartDelay(milliseconds); 

     valueAnimator.setIntValues(this.getProgress(), this.getMax()); 

     valueAnimator.start(); 
    } 


    //Set the duration of the animation 
    public void setAnimationDuration(long duration){ 
     valueAnimator.setDuration(duration); 
    } 


    public void setAnimatorListenerAdapter(AnimatorListenerAdapter ala){ 
     valueAnimator.addListener(ala); 
    } 

我想不通爲什麼不會重複兩次以上。

我試過使用Repeat屬性,並將其設置爲INIFINITI但這也沒有幫助。


編輯:要清楚,我想獲得的是無限重複本身具有不同的持續時間,每次的動畫。

+0

一些問題:什麼是'TylersContainer'?爲什麼每次執行'setupTimerBar'時添加一個新的監聽器,並且不會刪除任何?爲什麼在'setAnimationStartDelay'之後調用'setAnimatorListenerAdapter'?你是如何驗證'onAnimationEnd'只被調用一次的? – dst

+0

'TylersContainer'是一個持有視圖和下一班車何時到達的時間的對象(顯示在該視圖中)。我每次都添加了一個新的監聽器,因爲這是我在我發現的例子中如何完成的,我也不知道你必須刪除監聽器,我認爲他們在完成後才被刪除。 'setAnimationListenerAdapter'和'setAnimationStartDelay'的順序是微不足道的。我驗證了'onAnimationEnd'只用一些打印語句被調用來記錄貓。 「 – Tyler

+0

」setAnimationListenerAdapter和setAnimationStartDelay的順序很簡單。「我問,因爲我不確定是否有可能在添加偵聽器之前調用onAnimationEnd。通常,您可以嘗試使用android的調試功能逐步執行代碼的執行。也許這會產生結果? – dst

回答

1

如果您正在使用Animator,然後用Animator.AnimatorListener

功能AnimatorListener.onAnimationEnd()其綁定的情況下,使用您的動畫重複有限次數的,並且只調用一次

如果你的動畫重複多次,您應該使用功能AnimatorListener.onAnimationRepeat()代替,這將適用於每次您的動畫重複在每個重複結束後重復

據我瞭解,你需要的是onAnimationRepeat(),所以如果你只是移動要從onAnimationEnd()每個重複到onAnimationRepeat()後要執行的代碼,這應該修復它

參考:http://developer.android.com/reference/android/animation/Animator.AnimatorListener.html

11

我已經取得的設定RepeatMode誤差爲無窮,沒有工作,這必須設置爲RepeatCount

valueAnimator.setRepeatCount(ValueAnimator.INFINITE);