How can I repeat a property animation?

11.9k views Asked by At

I'm doing an animation of bubbles on the screen, but the bubbles stop after finishing the animation time. How do I repeat the animation or make it infinite?

bub.animate();
bub.animate().x(x2).y(y2);
bub.animate().setDuration(animationTime);       
bub.animate().setListener(new AnimatorListenerAdapter() {

    @Override
    public void onAnimationStart(Animator animation) {
        animators.add(animation); 
    } 

    @Override
    public void onAnimationRepeat(Animator animation) {
    }

    @Override
    public void onAnimationEnd(Animator animation) {
    }
});
6

There are 6 answers

0
codegames On

in kotlin you can do it like this. create a runnable. inside it animate the view and set withEndAction to runnable itself. and at the end run runnable to animation start.

var runnable: Runnable? = null
runnable = Runnable {
    view.animate()
        .setDuration(10000)
        .rotationBy(360F)
        .setInterpolator(LinearInterpolator())
        .withEndAction(runnable)
        .start()
}
runnable.run()
2
asylume On

Since ViewPropertyAnimator is only good for simple animations, use more advanced ObjectAnimator class - basically method setRepeatCount and additionally setRepeatMode.

1
Nidhanshu Sharma On
final ViewPropertyAnimator animator = view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000); //Animator object

    animator.setListener(new android.animation.Animator.AnimatorListener() {
        ...

        @Override
        public void onAnimationEnd(final android.animation.Animator animation) {
            animation.setListener(this); //It listens for animation's ending and we are passing this to start onAniationEnd method when animation ends, So it works in loop
            view.setRotation(0);
            view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000).setListener(this).start();
        }

    });
1
Dmide On

You can use CycleInterpolator. For example, like this:

    int durationMs = 60000;
    int cycleDurationMs = 1000;
    view.setAlpha(0f);
    view.animate().alpha(1f)
            .setInterpolator(new CycleInterpolator(durationMs / cycleDurationMs))
            .setDuration(durationMs)
            .start();
1
Trevor On

Here is an example in Kotlin with a simple way to repeat the animation by recursively calling it in withEndAction

Example

private var animationCount = 0

private fun gyrate() {
    val scale = if (animationCount++ % 2 == 0) 0.92f else 1f
    animate().scaleX(scale).scaleY(scale).setDuration(725).withEndAction(::gyrate)
}

This repeatedly animates the size of a view to get smaller, return to normal, get smaller, return to normal, etc. This is a pretty simple pattern to repeat whatever animation you'd like.

0
android developer On

This is actually possible. Here's an example of rotating a view:

        final ViewPropertyAnimator animator = view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000);

        animator.setListener(new android.animation.Animator.AnimatorListener() {
            ...

            @Override
            public void onAnimationEnd(final android.animation.Animator animation) {
                animation.setListener(null);
                view.setRotation(0);
                view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000).setListener(this).start();
            }

        });

You can also use "withEndAction" instead of a listener.