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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I have no time to test it anymore, I'm going to trust you. :-)
you saved my day, don't know why animatorSet.playTogether does not work properly (for real devices).
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.
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.

1 Comment

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: stackoverflow.com/a/11125271
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!

3 Comments

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!
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.
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 !!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.