Android animation does not repeat

android rotate animation repeat
scale animation android
stop animation android
bounce animation android
animation-list android
android animation xml
animation in android studio
android view animation

I'm trying to make simple animation that would repeat several times (or infinitely). It seems that android:repeatCount does not work! Here is my animation resource from /res/anim/first_animation.xml :

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false"
    android:repeatCount="infinite"
    >
    <scale
        android:interpolator="@android:anim/decelerate_interpolator"
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="1.2"
        android:toYScale="1.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false" />
    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:startOffset="500"
        android:duration="500"
        android:fromXScale="1.2"
        android:fromYScale="1.2"
        android:toXScale="1.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false" />
</set>

First it should scale image from 1.0 to 1.2 size in 500 ms. And then scale it back to 1.0 in 500 ms. Here is how I'm using it:

Animation firstAnimation = AnimationUtils.loadAnimation(this, R.anim.first_animation);
imgView.startAnimation(firstAnimation);

It makes one cycle and then finishes. It scales up, then scales down ans then stops. How can I make this work as intended?


Update: Back in Sep, 2011 an Android engineer fixed this issue for the most part. The attributes that were ignored in XML now work, with the exception of repeatCount and fillEnabled which are still ignored (on purpose for some reason). This means it still isn't easy to repeat an AnimationSet unfortunately.

For details please see the overview in the updated docs (explains which attributes are ignored, which work, and which are passed onto children). And for a deeper understanding of what fillAfter, fillBefore, and fillEnabled actually do, see the engineer's (Chet Haase) blog post about it here.


Original Answer

To expand upon answers by Pavel and others: it is true that the <set> tag is ridiculously buggy. It can't deal correctly with repeatCount and a number of other attributes.

I spent a few hours figuring out what it can and can't deal with and have submitted a bug report/issue here: Issue 17662

In summary (this concerns AnimationSets):

setRepeatCount() / android:repeatCount

This attribute (as well as repeatMode) does not work in code or XML. This makes repeating an entire set of animations difficult.

setDuration() / android:duration

Setting this on an AnimationSet in code WORKS (overrides all durations of children animations), but not when included in the tag in XML

setFillAfter() / android:fillAfter

This works in both code and XML for the tag. Strangely I have gotten it to also work without the need to set fillEnabled to true.

setFillBefore() / android:fillBefore

Seems to have no effect/ignored in both code and XML

setFillEnabled() / android:fillEnabled

Seems to have no effect/ignored in both code and XML. I can still get fillAfter to work even without including fillEnabled or setting fillEnabled to false.

setStartOffset() / android:startOffset

This works only in code and not XML.

Android Animation doesn't repeat, the problem is that the animation does not repeat, I tried this: @Override public void Set repeatCount and repeatMode in xml, and remove from code Android studio doesn't give you suggestions for this attributes, you should write manually. No need to override the onAnimationEnd as you have already set the animation repeat count as INFINITE. Remove that function. – burglarhobbit Jan 26 '17 at 8:34 animation.setRepeatMode(Animation.INFINITE); is incorrect.


I've found that <set> tag has buggy implementation in class AnimationSet. It can't deal correctly with repeatCount. What we can do - is to set repeatCount directly in <scale> tag. This XML resource is working well:

<?xml version="1.0" encoding="utf-8"?>
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:duration="200"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="1.05"
    android:toYScale="1.05"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatMode="reverse"
    android:fillAfter="false"
    android:repeatCount="24"
/>

Unfortunately, this is limited to only one animation at once. We can not define a sequence of animations this way...

Why my animation doesn't repeat?, Change your XML to have the repeat mode and count on it: <set xmlns:android="​http://schemas.android.com/apk/res/android"  Android Animation doesn't repeat. the problem is that the animation does not repeat, I tried this: No need to override the onAnimationEnd as you have already set the animation repeat count as INFINITE. Remove that function.


You should include the attribute

android:repeatCount="infinite"

But in your "scale" animation not in "set"

Android animation does not repeat, It seems that android:repeatCount does not work! Here is my animation resource from /res/anim/first_animation.xml : <?xml version="1.0" encoding="utf-8"?>  * This method provides allows fine-grained * control over the start time and invalidation, but you * must make sure that 1) the animation has a start time set, and * 2) the view's parent (which controls animations on its children) * will be invalidated when the animation is supposed to * start.


To get a repeating animation I utilized the animation listener, and called the animation again when it ended. This does a camera reticule focusing like animation with brackets.

Here is the animation layout xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
    android:fromXScale="1.0"
    android:toXScale=".7"
    android:fromYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toYScale=".7"
    android:duration="1000"/>
<scale 
    android:duration="1000"
    android:fromXScale=".7"
    android:toXScale="1.0"
    android:fromYScale=".7"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toYScale="1.0"
    android:startOffset="1000"/>

</set>

Here is the java code

 public void startAnimation() {

            View brackets = findViewById(R.id.brackets);
            brackets.setVisibility(View.VISIBLE);

            Animation anim = AnimationUtils.loadAnimation(BuzzFinderActivity.this, R.anim.crosshair_focusing);
            anim.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationEnd(Animation arg0) {
                    Animation anim = AnimationUtils.loadAnimation(BuzzFinderActivity.this, R.anim.crosshair_focusing);
                    anim.setAnimationListener(this);
                    brackets.startAnimation(anim);

                }

                @Override
                public void onAnimationRepeat(Animation arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onAnimationStart(Animation arg0) {
                    // TODO Auto-generated method stub

                }

            });


            brackets.startAnimation(anim);
}

Animation, Defines how many times the animation should repeat. Ensure that the duration that this animation will run is not longer than durationMillis . The Android Honeycomb 3.0 (and newer) animation library has a nice little class called AnimatorSet (not to be confused with the older AnimationSet) that lets you organize animations either to be played together, or to be played sequentially. In this case, I needed to create an infinitely looping image fader that cycled through ImageView objects on the screen.


I was also facing the same problem.. i included android:repeatCount="infinite" in XMl file..now its working fine...

  <translate 
           android:fromXDelta="0"
           android:toXDelta="80"
           android:duration="1000"
           android:repeatCount="infinite"   
           android:repeatMode="reverse" 
           android:pivotX="50%"
           android:pivotY="50%"                             
           android:fillAfter="true"/>

android.view.animation.Animation.setRepeatCount java code , setInterpolator(new LinearInterpolator()); // do not alter animation rate animation.​setRepeatCount(Animation.INFINITE); // Repeat animation infinitely animation. No, you can't repeat AnimatorSet, you can only repeat of a single ObjectAnimator/ ValueAnimator. But you can use PropertyValuesHolder, so that you can make an ObjectAnimator from a set of PropertyValuesHolder. here's example


Android animation does not repeat, I'm trying to make simple animation that would repeat several times (or infinitely). It seems that android:repeatCount does not work! Here is my  android:repeatCount must be set to a positive integer or "-1" for this attribute to have an effect. Set to "reverse" to have the animation reverse direction with each iteration or "repeat" to have the animation loop from the beginning each time.


A beginners guide to implement Android Animations, For eg — Their state changes but their property does not change. Android 5.0 introduced various other animations — Reveal Effect, Activity/Fragment transitions, Shared Element public void onAnimationRepeat(Animator animation) { // do  The property animation system is a robust framework that allows you to animate almost anything. You can define an animation to change any object property over time, regardless of whether it draws to the screen or not. A property animation changes a property's (a field in an object) value over a specified length of time.


Advanced Android 12.1: Property animations - Codelabs, For example, you would create an animator for the radius of a circle whose size you Repeat count and behavior: You can specify whether or not to have an  Android Animation is used to give the UI a rich look and feel. Animations in android apps can be performed through XML or android code. In this android animation tutorial we’ll go with XML codes for adding animations into our application.