开发者

Android: How can I stop an infinite animation applied on an ImageView?

开发者 https://www.devze.com 2023-01-19 11:06 出处:网络
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:

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:

and开发者_运维百科roid: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().


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


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.


You can do it simply by removing this line : android:repeatCount="infinite" from your rotate.xml file


Simply add below two lines.

anim?.cancelAnimation()

anim?.progress = 0f
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号