Android animation does not repeat

后端 未结 23 1442
囚心锁ツ
囚心锁ツ 2020-11-27 14:01

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 anim

相关标签:
23条回答
  • 2020-11-27 14:29

    i got this to go ...i was trying to get a view to rotate in a circle continuously .

    previous i was using rotation.setRepeatMode(-1) but that didn't work. switched to setrepeatcount and it works. This is on jelly bean 4.2.2

     ObjectAnimator rotation = ObjectAnimator.ofFloat(myview,
                              "rotation", 360).setDuration(2000);
                    rotation.setRepeatMode(-1);
              rotation.setRepeatCount(Animation.INFINITE); 
     rotation.start();
    
    0 讨论(0)
  • 2020-11-27 14:30

    Add the following class to your project:

    import android.view.View;
    import android.view.animation.Animation;
    
    public class AnimationRepeater implements Animation.AnimationListener
    {
        private View view;
        private Animation animation;
        private int count;
    
        public AnimationRepeater(View view, Animation animation)
        {
            this.view = view;
            this.animation = animation;
            this.count = -1;
        }
    
        public AnimationRepeater(View view, Animation animation, int count)
        {
            this.view = view;
            this.animation = animation;
            this.count = count;
        }
    
        public void start()
        {
            this.view.startAnimation(this.animation);
            this.animation.setAnimationListener(this);
        }
    
        @Override
        public void onAnimationStart(Animation animation) { }
    
        @Override
        public void onAnimationEnd(Animation animation)
        {
            if (this.count == -1)
                this.view.startAnimation(animation);
            else
            {
                if (count - 1 >= 0)
                {
                    this.animation.start();
                    count --;
                }
            }
        }
    
        @Override
        public void onAnimationRepeat(Animation animation) { }
    }
    

    For infinite loop of your view, do the following:

    Animation a = AnimationUtils(Context, R.anim.animation);
    new AnimationRepeater(View, a).start();
    

    If you want to repeat the animation for N-times only, do the following:

    Animation a = AnimationUtils(Context, R.anim.animation);
    new AnimationRepeater(View, a, int N).start();
    

    N stands for number of repetitions.

    0 讨论(0)
  • 2020-11-27 14:31

    you have to add just one line in your xml code that i suggested below .

    <scale
        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:repeatCount="infinite" // just add this one line 
        android:fillAfter="false"
        />
    </set>
    
    0 讨论(0)
  • 2020-11-27 14:32

    One need to listen for completion of first animation then re-start the animation in onStopAnimation call back, give a try to this link

    0 讨论(0)
  • 2020-11-27 14:36

    i just came across this issue while working on a backwards compatible app. so frustrating! i ended up coding a nice workaround class that can be called from onCreate and will kickoff any animation resource into an indefinite loop.

    the class, AnimationLooper, is available here: https://gist.github.com/2018678

    0 讨论(0)
提交回复
热议问题