Android: How can I stop an infinite animation appl

2019-01-18 01:39发布

I have an ImageView on which I have applied a rotate animation. Since I want the rotation to go on continuously, I gave the repeatCount as infinite in my rotate.xml:

android:repeatCount="infinite"

In onCreate(), I load the animation and start it.

Animation myAnim    = AnimationUtils.loadAnimation(this, R.anim.rotate);
objectImg.startAnimation(myAnim); 

When a button is pressed, the rotation must stop. Hence in my onClick(), I called clearAnimation().

objectImg.startAnimation(myAnim); 

My simple question is whether stopping the animation is the right thing to do. I assume clearAnimation() corresponds to loadAnimation(), but there is no stopAnimation() that corresponds to startAnimation().

3条回答
smile是对你的礼貌
2楼-- · 2019-01-18 02:20

Use clearAnimation() to stop an animation. There is no loadAnimation() on View.

查看更多
成全新的幸福
3楼-- · 2019-01-18 02:25

You can also call anim.cancel(); but you should also call anim.reset(); immediately after it. Then when you want to start it again, just call startAnimation on the view.

查看更多
够拽才男人
4楼-- · 2019-01-18 02:39

clearAnimation() on View is best solution , it stops and reset previous state of view.

查看更多
登录 后发表回答