natario
natario

Reputation: 25204

Repeating an AnimatorSet animation multiple times

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.

Upvotes: 9

Views: 7193

Answers (3)

robert
robert

Reputation: 618

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.

Upvotes: 3

Bryan Herbst
Bryan Herbst

Reputation: 67259

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.

Upvotes: 5

Parth Kapoor
Parth Kapoor

Reputation: 1504

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!

Upvotes: 0

Related Questions