Yaroslav Mytkalyk
Yaroslav Mytkalyk

Reputation: 17105

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)?

Upvotes: 8

Views: 16158

Answers (4)

Jerry Tian
Jerry Tian

Reputation: 3448

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();

Upvotes: 7

Yaroslav Mytkalyk
Yaroslav Mytkalyk

Reputation: 17105

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>

Upvotes: 5

Budius
Budius

Reputation: 39836

just ask it to repeat:

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

=)

count can also be a defined number.

Upvotes: -2

ognian
ognian

Reputation: 11541

You need to set a repeat mode to the AnimationSet, see:

http://developer.android.com/reference/android/view/animation/Animation.html#attr_android:repeatMode

Upvotes: -1

Related Questions