2011-06-02 105 views
4

夥計。我有這樣的代碼(的AsyncTask)RotateAnimation無持續時間

我的動畫()函數:

public void animation() 
     { 
     int currentRotation = 0; 
      anim = new RotateAnimation(currentRotation, (360*4), 
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f); 
      currentRotation = (currentRotation + 45) % 360; 
      anim.setInterpolator(new LinearInterpolator()); 
      anim.setDuration(4000);// i want rotating without this <------------------ 
      anim.setFillEnabled(true); 
      anim.setFillAfter(true); 
      refresh.startAnimation(anim); 
     } 

誰能告訴我這是可能做到這一點,而不anim.setDuration ???? 只是開始..當我按下按鈕(例如)動畫停止。 請幫幫我。 問候,彼得。

最終代碼:

public void animation() 
      { 
      int currentRotation = 0; 
       anim = new RotateAnimation(currentRotation, (360*4), 
         Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f); 
       currentRotation = (currentRotation + 45) % 360; 
       anim.setInterpolator(new LinearInterpolator()); 
       anim.setDuration(4000); 
       // anim.setRepeatMode(Animation.INFINITE); 
       anim.setRepeatCount(Animation.INFINITE); 
       anim.setFillEnabled(true); 
       anim.setFillAfter(true); 
       refresh.startAnimation(anim); 
      } 

和地方refresh.clearAnimation();爲停止動畫 這對我的工作完美..如果這裏有些東西錯了 - 請告訴我..總之謝謝你的答案:)

回答

9

我認爲你應該看重複模式。持續時間是一個循環播放動畫的時間,如果您將其設置爲在此之後重複播放,則可以永久播放。見thisthat

例如,你可以使用:

anim.setRepeatCount(Animation.INFINITE); 
anim.setRepeatMode(Animation.RESTART); 
+0

謝謝你,老兄。這非常有幫助 – Peter 2011-06-02 13:58:00

0

由於PearsonArtPhoto建議,你應該看看重複模式。持續時間是一個循環播放動畫的時間,如果您將其設置爲在此之後重複播放,則可以永久播放。

使用ObjectAnimator來實現結果。 例如,您可以使用:

anim.setRepeatCount(Animation.INFINITE); 
anim.setRepeatMode(ValueAnimator.RESTART); //note the difference here