Android:如何停止在 ImageView 上应用的无限动画? [英] Android: How can I stop an infinite animation applied on an ImageView?
问题描述
我有一个 ImageView,我在上面应用了旋转动画.由于我希望旋转持续进行,因此我在rotate.xml 中将repeatCount 设为无限:
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"
在 onCreate() 中,我加载动画并启动它.
In onCreate(), I load the animation and start it.
Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.rotate);
objectImg.startAnimation(myAnim);
按下按钮时,旋转必须停止.因此,在我的 onClick() 中,我调用了 clearAnimation().
When a button is pressed, the rotation must stop. Hence in my onClick(), I called clearAnimation().
objectImg.startAnimation(myAnim);
我的简单问题是停止动画是否正确.我假设 clearAnimation() 对应于 loadAnimation(),但没有 stopAnimation() 对应于 startAnimation().
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().
推荐答案
你也可以调用 anim.cancel();
但你也应该调用 anim.reset();
代码> 紧随其后.然后当你想再次启动它时,只需在视图上调用 startAnimation
即可.
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.
这篇关于Android:如何停止在 ImageView 上应用的无限动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!