简体   繁体   中英

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. I'm having trouble repeating the animation and the delay of each animation from each other (should be around 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

<?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. You can post a runnable with a 2 second delay to get your delay.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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