2010-08-19 32 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; 
}