Android动画一个接一个

Android动画一个接一个,android,animation,Android,Animation,我在TextView上有两个TranslateAnimation,我希望它们一个接一个地执行。但是,通过使用下面的代码,只执行第二个代码 我怎样才能解决这个问题 TranslateAnimation animation = new TranslateAnimation( Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -150.0f)

我在TextView上有两个TranslateAnimation,我希望它们一个接一个地执行。但是,通过使用下面的代码,只执行第二个代码

我怎样才能解决这个问题

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);

编辑:Andy Boots下面的答案是更好的答案


只需将第一个设置为这样,动画完成后,它将启动另一个:

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);

        }
    });
编辑:仅使用当前代码执行第二个动画的原因是,它会覆盖第一个动画的播放(两个动画实际上都已播放,但您只能看到最新开始的动画)。如果你像我写的那样,它们将按顺序播放,而不是并行播放。


还有一种方法可以实现这一目标,当您需要一个接一个地设置许多视图的动画时,这种方法非常有用。可以使用
setStartOffset
方法在动画开始之前设置延迟。因此,如果您知道第一个动画结束需要多长时间,可以将其设置为第二个动画的延迟。这是一个例子,我在下面依次设置了六个
图像按钮和六个
文本视图的动画:

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" />

您也可以通过XML本身使用
android:startOffset
属性来实现这一点,这里有一个示例:


如果您使用code,您可以调用

Animation.setStartOffset() 
延迟第二个动画


如果您使用xml,您可以
android:ordering=“sequential”
属性使两个动画按顺序执行。

创建动画数组并使用创建动画集的方法

    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;
}
publicstaticanimationset addanimationator(Animation[]animations){
AnimationSet AnimationSet=新的AnimationSet(false);
长totalAnimationDuration=0;
对于(int i=0;i
对于简单的动画,可以通过使用
animate()
withEndAction()
函数来实现,如下所示: 动画可绘制 在Android中运行这样的动画非常简单。事实上,Android API中有一个类专门用于此特定任务,名为。AnimationDrawable是一组图像

步骤1:创建动画Drwable
步骤2:将可绘制的动画加载到ImageView中
步骤3:启动动画

步骤1: 创建一个XML并像这样添加所有图像

<animation-list 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:oneshot="true">
     <item android:drawable="@drawable/cat_image_1" android:duration="200" />
     <item android:drawable="@drawable/cat_image_2" android:duration="200" />
     <item android:drawable="@drawable/cat_image_3" android:duration="200" />
</animation-list>

谢谢,我试过了,它能按我的要求工作。。。但我只是好奇,还有其他更好的方法吗?谢谢。它的工作方式,我正在寻找。“只要给你的第一个钥匙就行了,它会开始的”-该死的。。。上学。:)你能帮助@pgsandstrom吗?这太棒了。然而,这似乎只是为一个对象序列化动画。如果必须序列化不同对象的动画,则必须使用不同的方法。这对我来说很有效,但顺序错误,我放在第一位的动画排在第二位,如何解决?不适用于我|-)fadeAnimation.setStartOffset更改根动画。有什么解决办法吗?你试过了吗?工作不正常更新这个好主意!我试着用
it.animate().yBy(-10F).withEndAction{it.animate().yBy(10F)}
进行“跳跃”,如果你反复点击它,它就会射向月球。我想我必须硬编码
y
    ImageView img = findViewById(R.id.imageView);
    img.animate().rotation(180).alpha(0).setDuration(3000).withEndAction(new Runnable() {
        @Override
        public void run() {
            ImageView img = findViewById(R.id.imageView);
            img.animate().rotation(0).alpha(1).setDuration(2000);
        }
    });
<animation-list 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:oneshot="true">
     <item android:drawable="@drawable/cat_image_1" android:duration="200" />
     <item android:drawable="@drawable/cat_image_2" android:duration="200" />
     <item android:drawable="@drawable/cat_image_3" android:duration="200" />
</animation-list>
public void onCreate(Bundle savedInstanceState) {
    
    //Step 2
    ImageView myimage = (ImageView) findViewById(R.id.my_image);
    myimage.setBackgroundResource(R.drawable.rocket_thrust);

    //Step 3
    AnimationDrawable catAnimation = (AnimationDrawable) rocketImage.getBackground();
    catAnimation.start();

}