一个接一个的Android动画 [英] Android Animation one after other

查看:35
本文介绍了一个接一个的Android动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我该如何解决这个问题?

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

推荐答案

Andy Boots 下面的回答是更好的答案.

Andy Boots answer below is the better answer 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.

这篇关于一个接一个的Android动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆