Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop AnimatorSet

I have made an AnimatorSet of three ObjectAnimator which I want to repeat sequentally.

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially">

    <objectAnimator
        android:propertyName="rotation"
        android:duration="300"
        android:valueFrom="0"
        android:valueTo="5"
        android:valueType="floatType"/>

    <objectAnimator
        android:propertyName="rotation"
        android:duration="600"
        android:valueFrom="5"
        android:valueTo="-5"
        android:valueType="floatType"/>

    <objectAnimator
        android:propertyName="rotation"
        android:duration="300"
        android:valueFrom="-5"
        android:valueTo="0"
        android:valueType="floatType"/>

</set>

But if I set the CycleInterpolator to AnimatorSet because the Animators will start sequentally

public void setInterpolator (TimeInterpolator interpolator) Added in API level 11

Sets the TimeInterpolator for all current child animations of this AnimatorSet.

So I tried looping by restarting AnimatorSet by setting a listener, but it stops for few milliseconds and the effect of restarting AnimatorSet is noticeable.

a.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation)
    {
        animation.start();
    }
});

What can I do to loop it (except building a huge list of ObjectAnimators or writing my own animation using Thread and Handler)?

like image 492
Yaroslav Mytkalyk Avatar asked Jul 05 '13 15:07

Yaroslav Mytkalyk


2 Answers

In desperate need of this feature too, turns out some one already have found the solution.

http://www.jefflinwood.com/2013/04/repeating-android-animations-with-animatorset/

All credit belongs to the original author.

mAnimationSet.addListener(new AnimatorListenerAdapter() {

@Override
public void onAnimationEnd(Animator animation) {
    super.onAnimationEnd(animation);
    mAnimationSet.start();
}

});
mAnimationSet.start();
like image 192
Jerry Tian Avatar answered Sep 23 '22 12:09

Jerry Tian


A workaround for this particular case is creating an AnimatorSet with first item to rotate one half and the second to keep rotating

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially">

    <objectAnimator
        android:propertyName="rotation"
        android:duration="150"
        android:valueFrom="0"
        android:valueTo="-5"
        android:valueType="floatType"/>

    <objectAnimator
        android:propertyName="rotation"
        android:duration="300"
        android:valueFrom="-5"
        android:valueTo="5"
        android:repeatMode="reverse"
        android:repeatCount="infinite"
        android:valueType="floatType"/>

</set>
like image 33
Yaroslav Mytkalyk Avatar answered Sep 23 '22 12:09

Yaroslav Mytkalyk