如何在周期结束时停止动画?

Cat*_*ine 7 animation android animationutils

我有一个我正在旋转的ImageView用作加载动画.一旦我的数据被加载,我试图停止动画,但不是循环到最后然后停止,动画进入中途点,然后停止,然后图像快速恢复到其原始状态,这看起来很丑陋.

这是我尝试过的:

选项1:

ImageView iv = (ImageView) findViewById(R.id.refreshImage);
if (iv != null) {
    iv.clearAnimation();
}
Run Code Online (Sandbox Code Playgroud)

选项2:

ImageView iv = (ImageView) findViewById(R.id.refreshImage);
if (iv != null && iv.getAnimation() != null) {
    iv.getAnimation().cancel();
}
Run Code Online (Sandbox Code Playgroud)

选项3:

ImageView iv = (ImageView) findViewById(R.id.refreshImage);
if (iv != null && iv.getAnimation() != null) {
    iv.getAnimation().setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            animation.cancel();

        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }
    });
}
Run Code Online (Sandbox Code Playgroud)

最终结果在所有三种情况下都是相同的.如何旋转图像并将其放回原点?

编辑:

一些进一步的信息:我的旋转动画:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
Run Code Online (Sandbox Code Playgroud)

我如何开始动画:

ImageView iv = (ImageView) findViewById(R.id.refreshImage);
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate);
rotation.setRepeatCount(Animation.INFINITE);
iv.startAnimation(rotation);
Run Code Online (Sandbox Code Playgroud)

Jua*_*tés 15

在启动之前将动画repeatcount设置为无限,然后在动作完成时,将动画的repeatcount设置为0.动画将完成当前循环和没有您想要避免的跳转的停止.

//How you start
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate);
          rotation.setRepeatCount(Animation.INFINITE);
iv.startAnimation(rotation);

//You do your stuff while it spins
...

//You tell it not to repeat again
rotation.setRepeatCount(0);
Run Code Online (Sandbox Code Playgroud)

重要的是你首先将它设置为Animation.INFINITE(或-1,因为它们做同样的事情)然后0,如果你把它设置1000为例如,那么根据我的测试,它不会因某种原因而停止.

  • @Catherine对于同时发生多件事的动画集来说可能看起来很蠢,没有测试过,但对于简单的动画,这是要走的路. (2认同)