2011-04-24 16 views
3

我在ImageView上應用了一個無限動畫來指示我的應用中正在運行的後臺線程。當線程結束時,我可以通過使用clearAnimation()來停止動畫,但它會將ImageView重新設置回它的起始位置,並且我想要完成當前的動畫循環(該動畫循環已完成起始位置)。有沒有辦法做到這一點?如何停止ImageView上的無限動畫,但讓它完成當前的循環?

回答

6

註冊AnimationListener,並等到onAnimationRepeat()清除它。我沒有嘗試過,但我認爲它會起作用。

+0

討厭鬼,那會是完美的,除了onAnimationRepeat()沒有被調用,這似乎是一個長期存在的bug:http://code.google.com/p/android/issues/detail ?id = 13397(onAnimationStart有效,如果我將動畫的repeatCount設置爲有限數字,onAnimationEnd也是如此) – wirbly 2011-04-24 23:26:43

+1

@wirbly:你可以嘗試通過單個動畫來滾動自己的「無限」,然後再次開始動畫'onAnimationEnd()'。不知道它是否會像無限的變化一樣光滑。但是,如果這種方法奏效,那麼您就不會在您想要的動畫結束時開始下一個動畫。 – CommonsWare 2011-04-24 23:40:02

+0

..這正是我所做的,它的工作原理。謝謝! – wirbly 2011-04-25 00:17:20

4

只需致電setRepeatCount(0),然後聽取onAnimationEnd。有關更多詳細信息,請參閱here

+0

只需將重複計數設置爲零即可。我沒有聽onAnimationEnd,但它似乎工作正常。 – Henry 2012-10-01 21:09:37

+0

@Henry我的用例有點複雜(刷新時會刷新動畫的刷新按鈕),所以我需要onAnimationEnd來隱藏動畫進度指示器,並用按鈕替換它。還有一些解決方法是必要的,使過渡非常順利。查看我鏈接的答案以獲取詳細信息。 onAnimationEnd僅在需要知道動畫實際停止的時間時才需要,因爲它在調用setRepeatCount(0)時不會停止,它首先完成當前循環(這是整個點) – user1431317 2012-12-14 12:54:28

2

您可以在動畫偵聽器的onAnimationEnd()方法中設置動畫的理想位置。然後,視圖將顯示在您設置的座標處而不是初始位置。

animation = new TranslateAnimation(0, -length, 0, 0); //setup here your translation parameters 
animation.setInterpolator(new LinearInterpolator()); 
animation.setDuration(10); 
animation.setAnimationListener(new Animation.AnimationListener() { 
    public void onAnimationStart(Animation animation) { 
     animationRunning = true; 
    } 
    public void onAnimationRepeat(Animation animation) { 
    } 
    public void onAnimationEnd(Animation animation) { 
     animationRunning = false; 
     // setAnimationPositionAfterPositionChange(); 
     LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) runningText.getLayoutParams(); 
     params.leftMargin = currentPosition; //Calculate you current position here 
     runningText.setLayoutParams(params); 
    } 
}); 
runningText.setAnimation(animation); 
0

如果您使用的動畫() 您使用setListener(空)來阻止它的動畫, 爲我工作。

image.animate() 
      .translationXBy(-width* 0.5f) 
      .setDuration(300) 
      .setStartDelay(6000) 
      .setListener(new AnimatorListenerAdapter() { 
       @Override 
       public void onAnimationEnd(Animator animation) { 
        image.animate().rotationBy(90).setDuration(1000).setStartDelay(1).setListener(null); 
       } 
      });