2010-08-19 31 views
16

我在TextView上有兩個TranslateAnimations,我希望它們能夠一個接一個地執行。但是,通過使用下面的代碼,只執行第二個代碼。Android動畫一個接一個

我該如何解決這個問題?

TranslateAnimation animation = new TranslateAnimation(
    Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f, 
    Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -150.0f); 
animation.setDuration(200); 
wave.startAnimation(animation); 

TranslateAnimation animation1 = new TranslateAnimation(
    Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f, 
    Animation.ABSOLUTE, 150.0f, Animation.ABSOLUTE, 0.0f); 
animation1.setDuration(200); 
wave.startAnimation(animation1); 
+5

這裏有什麼波? – 2013-09-20 13:41:26

回答

29

編輯:下面安迪靴答案是更好的答案海事組織。


只需設置你的第一個這樣的,它會啓動另外一個,一旦動畫完成:

animation.setAnimationListener(new AnimationListener() { 

     @Override 
     public void onAnimationStart(Animation animation) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      wave.startAnimation(animation1); 

     } 
    }); 

編輯:原因只有你的第二個動畫與當前的代碼執行,是因爲它覆蓋了第一個動畫的播放(實際上都是播放的,但你只能看到最新的動畫)。如果你喜歡我寫的,他們會順序播放,而不是並行播放。

+0

謝謝 試過吧,它的工作原理是我想要的......但只是好奇,有沒有其他更好的方法來做到這一點? – amithgc 2010-08-19 08:36:50

+0

不是我所知道的,sry; _; – pgsandstrom 2010-08-19 15:22:15

+0

謝謝。它工作如何我尋找。 。 。 – 2011-05-27 04:37:15

49

鏈接他們 Animation Set

AnimationSet as = new AnimationSet(true) 
TranslateAnimation animation = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f, 
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -150.0f); 
animation.setDuration(200); 
as.addAnimation(animation); 

TranslateAnimation animation1 = new TranslateAnimation(
Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f, 
Animation.ABSOLUTE, 150.0f, Animation.ABSOLUTE, 0.0f); 
animation1.setDuration(200); 
animation1.setStartOffset(200); 
as.addAnimation(animation1); 

wave.startAnimation(as); 
+1

這真是太棒了。但是,這似乎只爲一個對象序列化動畫。如果您必須序列化不同對象的動畫,則必須使用不同的方法。 – 2015-10-19 09:31:42

+0

這對我有用,但順序錯了,我先放的動畫第二個來了,怎麼解決? – underfilho 2018-02-10 19:35:14

6

在一起,還有就是要達到這個目標時,你需要動畫了很多意見此起彼伏這可能是有用的一個更辦法。您可以使用setStartOffset方法在動畫開始之前設置延遲。所以,如果你知道,第一個動畫結束需要多少時間,你可以將它設置爲第二個動畫的延遲。這是我在他們下面的動畫6 ImageButtonsTextViews一個又一個的例子:

public void animateButtons() { 
    // An array of buttons 
    int[] imageButtonIds = {R.id.searchButton, R.id.favoriteButton, R.id.responseButton, R.id.articleButton, R.id.resumeButton, R.id.subscribeButton}; 
    // Array of textViews 
    int[] textViewIds = {R.id.searchTextView, R.id.favoriteTextView, R.id.responseTextView, R.id.articleTextView, R.id.resumeTextView, R.id.subscribeTextView}; 

    int i = 1; 

    for (int viewId : imageButtonIds) { 

     ImageButton imageButton = (ImageButton) findViewById(viewId); 
     // Animation from a file fade.xml in folder res/anim 
     Animation fadeAnimation = AnimationUtils.loadAnimation(this, R.anim.fade); 
     // Delay for each animation is 100 ms bigger than for previous one 
     fadeAnimation.setStartOffset(i * 100); 
     imageButton.startAnimation(fadeAnimation); 

     // The same animation is for textViews 
     int textViewId = textViewIds[i-1]; 
     TextView textView = (TextView) findViewById(textViewId); 
     textView.startAnimation(fadeAnimation); 

     i ++; 
    } 
} 

在我res/anim文件夾中我有一個文件,叫做fade.xml這些內容:

<?xml version="1.0" encoding="utf-8"?> 

<!-- Fade animation with 500 ms duration --> 

<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
     android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
     android:fromAlpha="0.0" android:toAlpha="1.0" 
     android:duration="500" /> 
+0

好的感謝 – 2013-11-25 15:49:38

+0

對我沒用| - )fadeAnimation.setStartOffset改變了根動畫。有沒有解決方法? – Solivan 2016-08-29 11:15:21

6

,你也可以通過XML本身使用android:startOffset屬性來做到這一點,並且有一個考試:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <scale 
     android:duration="300" 
     android:fromXScale="0%" 
     android:fromYScale="0%" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:toXScale="100%" 
     android:toYScale="100%" /> 
    <alpha 
     android:duration="300" 
     android:fromAlpha="0" 
     android:toAlpha=".5" /> 
    <alpha 
     android:duration="300" 
     android:fromAlpha=".5" 
     android:startOffset="300" 
     android:toAlpha="1" /> 

</set> 
-1

如果喲你使用代碼,你可以撥打

Animation.setStartOffset() 

延遲第二個動畫。

如果您使用xml,您可以使android:ordering="sequentially"屬性使兩個動畫順序執行。

1

創建一個動畫數組並使用創建AnimationSet的方法。

Animation[] animations = { 
      getScaleAnimation(0.4f, 1.3f, 2000), 
      getScaleAnimation(1.3f, 1.0f, 500), 
      getScaleAnimation(0.4f, 1.3f, 1000), 
      getScaleAnimation(1.3f, 1.0f, 3000), 
      getScaleAnimation(0.4f, 1.3f, 500), 
      getScaleAnimation(1.3f, 1.0f, 1700), 
      getScaleAnimation(0.4f, 1.3f, 2100), 
      getScaleAnimation(1.3f, 1.0f, 3400) 
    }; 
    AnimationSet animationSet = addAnimationAr(animations); 
    view.startAnimation(animationSet); 

方法:

public static AnimationSet addAnimationAr(Animation[] animations) { 
    AnimationSet animationSet = new AnimationSet(false); 
    long totalAnimationDuration = 0; 

    for (int i = 0; i < animations.length; i++) { 
     Animation a = animations[i]; 
     a.setStartOffset(totalAnimationDuration); 
     totalAnimationDuration += a.getDuration(); 
     animationSet.addAnimation(a); 
    } 

    return animationSet; 
}