Android:如何停止在 ImageView 上应用无限动画?
我有一个 ImageView,我在其上应用了旋转动画。由于我希望旋转连续进行,因此我在rotate.xml 中将repeatCount 设置为无限:
android:repeatCount="infinite"
在onCreate() 中,我加载动画并启动它。
Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.rotate);
objectImg.startAnimation(myAnim);
当按下按钮时,旋转必须停止。因此,在 onClick() 中,我调用了clearAnimation()。
objectImg.startAnimation(myAnim);
我的简单问题是停止动画是否是正确的做法。 我假设clearAnimation()对应于loadAnimation(),但是没有stopAnimation()对应于startAnimation()。
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().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
clearAnimation()
停止动画。View
上没有loadAnimation()
。Use
clearAnimation()
to stop an animation. There is noloadAnimation()
onView
.您还可以调用
anim.cancel();
,但也应该在其之后立即调用anim.reset();
。然后,当您想再次启动它时,只需在视图上调用
startAnimation
即可。You can also call
anim.cancel();
but you should also callanim.reset();
immediately after it.Then when you want to start it again, just call
startAnimation
on the view.只需添加以下两行即可。
Simply add below two lines.
您只需从
rotate.xml
文件中删除此行:android:repeatCount="infinite"
即可做到这一点You can do it simply by removing this line :
android:repeatCount="infinite"
from yourrotate.xml
file