如何执行一个动画然后执行另一个动画

时间:2019-06-19 17:40:56

标签: android animation

我不希望我的两只动物同时开始,一个又一个地开始。 你有什么想法吗?

我的动画:

Fade1

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromAlpha="1.0"
       android:toAlpha="0.0"
       android:duration="1500"
/>

Fade2

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromAlpha="1.0"
       android:toAlpha="0.0"
       android:duration="3000"
/>

我的想法没用

 private void animations_sequencing(){
        //The first anim start
        try{
            text.setText("Hello");
            text.startAnimation(AnimationUtils.loadAnimation(Main2Activity.this, R.anim.fade1));
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //The first anim is end, the seconde anim start
        try{
            text.setText("World");
            text.startAnimation(AnimationUtils.loadAnimation(Main2Activity.this, R.anim.fade2));
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

2 个答案:

答案 0 :(得分:0)

您必须实现setAnimationListener才能在动画结束时进行捕获。

text.startAnimation(AnimationUtils.loadAnimation(Main2Activity.this, R.anim.fade1).setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                //start second animation
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            }));

答案 1 :(得分:0)

方法如下:

Animation anim2;

private void anim(){

    Animation anim = AnimationUtils.loadAnimation(Main2Activity.this, R.anim.fade);
        anim2 = AnimationUtils.loadAnimation(Main2Activity.this, R.anim.fade2);

        anim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }
        @Override
        public void onAnimationEnd(Animation animation) {
            text.setText("World");
            text.startAnimation(anim2);
        }
        @Override
        public void onAnimationRepeat(Animation animation) {
        }

    });
        text.startAnimation(anim);
    }
}