Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 642
natario Avatar asked Feb 11 '15 12:02

natario


2 Answers

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.

like image 75
Bryan Herbst Avatar answered Nov 05 '22 21:11

Bryan Herbst


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.

like image 20
robert Avatar answered Nov 05 '22 23:11

robert