简体   繁体   English

Android动画重复

[英]Android Animation Repeating

I have three views with the same translate animation. 我有三个具有相同翻译动画的视图。 Using a random number from 0-2, one of the three views is animated. 使用0-2之间的随机数,可以对三个视图之一进行动画处理。 I'm having trouble repeating the animation and the delay of each animation from each other (should be around 2000ms). 我在重复动画和每个动画的延迟方面遇到麻烦(应该在2000ms左右)。

Animation move = AnimationUtils.loadAnimation(this, R.anim.move);
View view1 = (View) findViewById(R.id.view1);
View view2 = (View) findViewById(R.id.view2);
View view3 = (View) findViewById(R.id.view3);

Random color_box_fall_random = new Random();
int random_int = (color_box_fall_random.nextInt(2));

for (int i = 0; i < 10; i++){
    if (random_int == 0){
        view1.startAnimation(move);
    }
    else if (random_int == 1){
        view2.startAnimation(move);
    }
    else{
        view3.startAnimation(move);
    }
}

move.xml move.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">

    <translate
        android:startOffset="2000"
        android:fromYDelta="-200"
        android:toYDelta="50%p"
        android:duration="2000" />
</set>

You need to use an AnimationListener to start your next animation after one ends. 结束之后,您需要使用AnimationListener来开始下一个动画。 You can post a runnable with a 2 second delay to get your delay. 您可以延迟2秒发布可运行对象,以获取延迟。

Animation move;
View view1;
View view2;
View view3;
Random color_box_fall_random;
int i;
Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    move = AnimationUtils.loadAnimation(this, R.anim.move);
    view1 = findViewById(R.id.view1);
    view2 = findViewById(R.id.view2);
    view3 = findViewById(R.id.view3);

    color_box_fall_random = new Random();

    handler = new Handler();

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

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            i++;
            if(i < 10) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        switch(color_box_fall_random.nextInt(2)){
                            case 0:
                                view1.startAnimation(move);
                                break;
                            case 1:
                                view2.startAnimation(move);
                                break;
                            case 2:
                                view3.startAnimation(move);
                                break;
                        }
                    }
                }, 2000);
            }
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });

    switch(color_box_fall_random.nextInt(2)){
        case 0:
            view1.startAnimation(move);
            break;
        case 1:
            view2.startAnimation(move);
            break;
        case 2:
            view3.startAnimation(move);
            break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM