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

你说的曾经没有我的故事 提交于 2019-11-27 02:04:42

问题


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().


回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/3913750/android-how-can-i-stop-an-infinite-animation-applied-on-an-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!