Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat an AnimationSet with sequentially added animations

I am trying to make an animation in my Activity, to be repeated infinite times. I have already tried it in XML file with repeatCount and repeatMode attributes, but it doesn't work. The thing is that myanimation.xml file is constructed of a set of different animations.

My XML file for that animation

<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/bounce_interpolator"
>



<translate


    android:startOffset="1000"
    android:fillAfter="true"
    android:fromXDelta="10"
    android:fromYDelta="10"
    android:toXDelta="50"
    android:toYDelta="-200"
    android:duration="1800"
    android:interpolator="@android:anim/bounce_interpolator"

    />



<translate

    android:fillAfter="true"
    android:startOffset="2000"
    android:fromYDelta="10"
    android:fromXDelta="10"
    android:toXDelta="100"
    android:toYDelta="270"
    android:duration="1800"
    android:interpolator="@android:anim/bounce_interpolator" />

<translate

    android:fillAfter="true"
    android:startOffset="3000"
    android:fromYDelta="10"
    android:fromXDelta="10"
    android:toXDelta="130"
    android:toYDelta="-270"
    android:duration="1800"
    android:interpolator="@android:anim/bounce_interpolator"
    />

<translate

    android:fillAfter="true"
    android:startOffset="4000"
    android:fromYDelta="10"
    android:fromXDelta="10"
    android:toXDelta="140"
    android:toYDelta="270"
    android:duration="1800"
    android:interpolator="@android:anim/bounce_interpolator"
    />

<translate

    android:fillAfter="true"
    android:startOffset="5000"
    android:fromYDelta="10"
    android:fromXDelta="10"
    android:toXDelta="90"
    android:toYDelta="-270"
    android:duration="1800"
    android:interpolator="@android:anim/bounce_interpolator"
    />

And in onCreate() I have animation tied to a ImageView object.

    ImageView ball = (ImageView) findViewById(R.id.animationBall);
    final Animation myAnimation = AnimationUtils.loadAnimation(this,   R.anim.ball_animation);
    ball.startAnimation(myAnimation);

The animation works fine, the only thing is that it doesn't want to repeat itself, even if I set the setRepeatMode() or setRepeatCount() methods.

like image 683
user3475581 Avatar asked Jun 09 '15 12:06

user3475581


1 Answers

For what its worth, setRepeatMode() and setRepeatCount() have to be set on the Animation objects, and not on the AnimationSet object. That's potentially a mistake you may have made.

So either call those methods on the Animation objects or add those attributes to the XML of the translate schema.

Another approach is to set an endlessly repeating animation as follows:

mAnimationSet.addListener(new AnimatorListenerAdapter() {

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

});
mAnimationSet.start();
like image 149
Y.S Avatar answered Sep 28 '22 08:09

Y.S