Looping Animation Android

  android, android-studio, animation, java

how to looping animation in android studio

animation like this
https://drive.google.com/file/d/13XJcgUzuGnq5cWSwIE1GzHrdCWbvKkqS/view?usp=sharing

I’ve made animation for infinite loop, but it doesn’t work
can you help me

code for ball animation from bottom to top

// Ball bottom to top
            long duration1 = 3000;
            Animation tilt_up = AnimationUtils.loadAnimation(
                    getApplicationContext(),
                    R.anim.tilt_up);
            tilt_up.setDuration(duration1);
            tilt_up.setAnimationListener(new Animation.AnimationListener(){

                @Override
                public void onAnimationStart(Animation animation){
                }
                @Override
                public void onAnimationRepeat(Animation animation){
                    animation.setRepeatCount(Animation.INFINITE);
                    animation.setRepeatMode(Animation.RESTART);
                    tilt_up.setRepeatMode(Animation.RESTART);
                    tilt_up.setRepeatCount(Animation.INFINITE);
                    ball.startAnimation(tilt_up);
                }

                @Override
                public void onAnimationEnd(Animation animation){
                    ball.clearAnimation();
                }
            });
            ball.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ball.startAnimation(tilt_up);
                    tilt_up.setDuration(duration1);
                }
            }, 0);

code for ball animation from left to right

// Ball left to right
            long duration2 = 500;
            long hold1Duration = duration1 + duration2;
            Animation move_right = AnimationUtils.loadAnimation(
                    getApplicationContext(),
                    R.anim.move_right);
            move_right.setRepeatMode(Animation.RESTART);
            move_right.setRepeatCount(Animation.INFINITE);
            move_right.setAnimationListener(new Animation.AnimationListener(){
                @Override
                public void onAnimationStart(Animation animation){
                    ball.setVisibility(View.INVISIBLE);
                }
                @Override
                public void onAnimationRepeat(Animation animation){
                    animation.setRepeatCount(Animation.INFINITE);
                    animation.setRepeatMode(Animation.RESTART);
                    move_right.setRepeatMode(Animation.RESTART);
                    move_right.setRepeatCount(Animation.INFINITE);
                    ball2.startAnimation(move_right);
                }

                @Override
                public void onAnimationEnd(Animation animation){
                    ball2.clearAnimation();
                }
            });
            ball2.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ball.clearAnimation();
                    ball2.startAnimation(move_right);
                    move_right.setDuration(duration2);
                }
            }, duration1);

Code for ball animation from top to bottom

                long duration3 =  6000;
                long exhaleDuration =  hold1Duration + duration3;
                Animation tilt_down = AnimationUtils.loadAnimation(
                        getApplicationContext(),
                        R.anim.tilt_down);
                tilt_down.setRepeatMode(Animation.RESTART);
                tilt_down.setRepeatCount(Animation.INFINITE);
                tilt_down.setAnimationListener(new Animation.AnimationListener(){
                    @Override
                    public void onAnimationStart(Animation animation){
                    }
                    @Override
                    public void onAnimationRepeat(Animation animation){
                        animation.setRepeatCount(Animation.INFINITE);
                        animation.setRepeatMode(Animation.RESTART);
                        tilt_down.setRepeatMode(Animation.RESTART);
                        tilt_down.setRepeatCount(Animation.INFINITE);
                        ball3.startAnimation(tilt_down);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation){
                        ball3.clearAnimation();
                    }
                });

                ball3.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ball3.startAnimation(tilt_down);
                        tilt_down.setDuration(duration3);
                    }
                }, hold1Duration);```

code for ball animation from right to left

    //Ball right to left
                long duration4 = 500;
                long hold2Duration =  exhaleDuration + duration4;
                Animation move_left = AnimationUtils.loadAnimation(
                        getApplicationContext(),
                        R.anim.move_hold_2);
                move_left.setRepeatMode(Animation.RESTART);
                move_left.setRepeatCount(Animation.INFINITE);
                move_left.setAnimationListener(new Animation.AnimationListener(){
                    @Override
                    public void onAnimationStart(Animation animation){
                    }
                    @Override
                    public void onAnimationRepeat(Animation animation){
                        animation.setRepeatCount(Animation.INFINITE);
                        animation.setRepeatMode(Animation.RESTART);
                        move_left.setRepeatMode(Animation.RESTART);
                        move_left.setRepeatCount(Animation.INFINITE);
                        ball4.startAnimation(move_left);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation){
                        ball4.clearAnimation();

                        /* Change Button And Ball Visibility */
                        ball.setVisibility(View.VISIBLE);
                    }
                });
                ball4.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ball4.startAnimation(move_left);
                        move_left.setDuration(duration4);
                    }
                }, exhaleDuration);

Source: Android Questions

LEAVE A COMMENT