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

时间:2021-05-19 02:12:01

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:

我有一个ImageView,我已经应用了旋转动画。由于我希望旋转继续进行,因此我在rotate.xml中将repeatCount视为无限:

android:repeatCount="infinite"

In onCreate(), I load the animation and start it.

在onCreate()中,我加载动画并启动它。

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

按下按钮时,必须停止旋转。因此在我的onClick()中,我调用了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().

我的简单问题是停止动画是否正确。我假设clearAnimation()对应于loadAnimation(),但是没有与startAnimation()对应的stopAnimation()。

3 个解决方案

#1


27  

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.

你也可以调用anim.cancel();但你也应该调用anim.reset();紧接着它。然后,当您想再次启动它时,只需在视图上调用startAnimation。

#2


57  

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

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

#3


0  

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

view上的clearAnimation()是最佳解决方案,它会停止并重置以前的视图状态。

#1


27  

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.

你也可以调用anim.cancel();但你也应该调用anim.reset();紧接着它。然后,当您想再次启动它时,只需在视图上调用startAnimation。

#2


57  

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

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

#3


0  

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

view上的clearAnimation()是最佳解决方案,它会停止并重置以前的视图状态。