一个接一个的Android动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3519682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:35:37  来源:igfitidea点击:

Android Animation one after other

androidanimation

提问by amithgc

I have two TranslateAnimations on a TextView and I want them to execute one after other. However, by using the code below, only the second one is executed.

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

How can I solve this?

我该如何解决这个问题?

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

回答by andy boot

Link them together with 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);

回答by pgsandstrom

EDIT:Andy Boots answer below is the better answer imo.

编辑:Andy Boots 下面的回答是更好的回答 imo。



Just set your first one like this and it'll start the other one, once the animation finishes:

只需像这样设置你的第一个,一旦动画完成,它就会启动另一个:

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

        }
    });

edit: The reason only your second animation is executed with your current code, is because it overrides the playing of the first animation (both actually are played, but you only see the latest one to start). If you do like I wrote, they will play sequentially instead of in parallel.

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

回答by Khaled AbuShqear

also you can do this by XML itself using android:startOffsetattribute , and there is an examble:

你也可以通过 XML 本身使用android:startOffsetattribute来做到这一点,并且有一个例子:

<?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>

回答by Denis Kutlubaev

There is one more approach to reach this goal which can be useful when you need to animate a lot of views one after another. You can use setStartOffsetmethod to set a delay before animation begins. So, if you know, how much time will take for your first animation to end, you can set this as a delay for your second animation. This is an example where I animated six ImageButtonsand six TextViewsbelow them one after another:

还有另一种方法可以实现这一目标,当您需要一个接一个地为大量视图设置动画时,这会很有用。您可以使用setStartOffset方法在动画开始之前设置延迟。因此,如果您知道第一个动画结束需要多长时间,您可以将其设置为第二个动画的延迟。这是一个例子,我在它们下面一个接一个地动画六个ImageButtons和六个TextViews

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 ++;
    }
}

In my res/animfolder I have a file, called fade.xmlwith these contents:

在我的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" />

回答by Mete

Create an animation array and use method for creating 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);

Method:

方法:

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

回答by Andy Xiao

If you use code, you can call

如果你使用代码,你可以调用

Animation.setStartOffset() 

to delay the second animation.

延迟第二个动画。

if you use xml you can android:ordering="sequentially"property to make the two animations perform sequentially.

如果使用 xml,则可以通过android:ordering="sequentially"属性使两个动画按顺序执行。

回答by lilotop

For simple animations, you can achieve this by using the animate()and withEndAction()functions, like so:

对于简单的动画,您可以使用animate()withEndAction()函数来实现,如下所示:

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