Android:如何停止在 ImageView 上应用无限动画?

发布于 2024-09-27 01:59:50 字数 535 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失而复得 2024-10-04 01:59:50

使用 clearAnimation() 停止动画。 View 上没有 loadAnimation()

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

又爬满兰若 2024-10-04 01:59:50

您还可以调用 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.

空气里的味道 2024-10-04 01:59:50

只需添加以下两行即可。

anim?.cancelAnimation()

anim?.progress = 0f

Simply add below two lines.

anim?.cancelAnimation()

anim?.progress = 0f
路弥 2024-10-04 01:59:50

您只需从 rotate.xml 文件中删除此行: android:repeatCount="infinite" 即可做到这一点

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文