9

I've been trying for hours, I feel it's time to give up. How can I loop an AnimatorSet defined in xml?

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <objectAnimator />

    <objectAnimator />

    <objectAnimator />

    <objectAnimator />

</set>

I tried dozens of combinations of startOffset, repeatCount and duration on the single objectAnimators, but that's just not the right way.

I read about this promising workaround:

a.addListener(new AnimatorListenerAdapter() {

    @Override
    public void onAnimationEnd(Animator animation) {
        animation.start();
        Log.i();
    }
});

but it just doesn't work: onAnimationEnd is called one time, animation is repeated, and then onAnimationEnd is not called anymore.

Other similar questions here involve wrong answers (referring to the android.view.animation framework) or suggest defining a custom interpolator for a single objectAnimator, but that's not really what I'm looking for. Thank you.

natario
  • 24,954
  • 17
  • 88
  • 158

3 Answers3

5

I had the same issue with an AnimatorSet that played two animations together.

I created the set with animationSet.play(anim1).with(anim2), which resulted in my animations only repeating a single time.

Changing it to animationSet.play(anim1).with(anim2).after(0) resolved my problem and allowed the animation to loop indefinitely.

It appears as though there is a bug that forces you to have at least one sequential step in the animation before animations can loop more than once.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • 1
    I have no time to test it anymore, I'm going to trust you. :-) – natario Sep 03 '15 at 16:33
  • you saved my day, don't know why **animatorSet.playTogether** does not work properly (for real devices). – Phong Nguyen May 02 '19 at 10:47
  • First I used `animatorSet.playSequentially` which worked (looped infinite) but realized that I don't need two animations and tried `animatorSet.play` which looped only once as you describe. Using `animatorSet.play(anim1).after(0)` works for me for once animation. – Bruno Bieri Jun 09 '20 at 08:37
3

I meet absolutely the same situation. After nearly trial of a day, I suddenly suspect that animator should be started on main thread. And it works.

mRandomPointAnimatorSet.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationStart(Animator animation) {
            Log.i(TAG, "onAnimationStart");
            mRandomPointView.setVisibility(VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            Log.i(TAG, "onAnimationEnd");
            mRandomPointView.setVisibility(INVISIBLE);
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    if (isShown()) {
                        requestLayout();
                        mRandomPointAnimatorSet.start();
                    }
                }
            });
        }
    });

Current I don't know why.

robert
  • 598
  • 1
  • 5
  • 12
  • I guess you should mention to pay attention to create the `mHandler` using the main thread. Like `Handler mHandler = new Handler(context.getMainLooper());` as described here: https://stackoverflow.com/a/11125271/ – Bruno Bieri Jun 09 '20 at 08:47
0

You are not adding listener to your animation, when you are restarting animation as recursion. You need create a AnimatorListenerAdapter object, and reuse it.

Hope I am making some sense to you!

Parth Kapoor
  • 1,494
  • 12
  • 23
  • I tried calling `a.start()` rather than `animation.start()` and I was getting the same behaviour, so I think that's not the problem. Will try! – natario Feb 11 '15 at 12:33
  • I just create an AnimatorListenerAdapter object, add it as a listener, and start. Inside the listener I have tried calling `animation.start()` as well as `a.start()` (my final AnimatorSet), and also tried adding the listener right before. Result: `onAnimationEnd` gets triggered just one time. – natario Feb 11 '15 at 13:33
  • Well, first up you need to believe that your IDE has no grudges with you and what ever you have tried is wrong and you need a fresh approach !! – Parth Kapoor Feb 11 '15 at 13:39