9

我有一个我正在旋转的 ImageView 用作加载动画。加载数据后,我尝试停止动画,但不是循环到最后然后停止,而是动画到一半,然后停止,然后图像恢复到原始状态,这看起来很丑陋.

这是我尝试过的:

选项1:

ImageView iv = (ImageView) findViewById(R.id.refreshImage);
if (iv != null) {
    iv.clearAnimation();
}

选项 2:

ImageView iv = (ImageView) findViewById(R.id.refreshImage);
if (iv != null && iv.getAnimation() != null) {
    iv.getAnimation().cancel();
}

选项 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) {

        }
    });
}

在所有三种情况下,最终结果都是相同的。如何旋转图像并让它最终回到它开始的位置?

编辑:

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

<?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" />

我如何开始动画:

ImageView iv = (ImageView) findViewById(R.id.refreshImage);
Animation rotation = AnimationUtils.loadAnimation(this, R.anim.rotate);
rotation.setRepeatCount(Animation.INFINITE);
iv.startAnimation(rotation);
4

3 回答 3

16

在开始之前将动画重复计数设置为无限,然后当动作完成时,将动画的重复计数设置为 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);

重要的是,您首先将其设置为Animation.INFINITE(或 -1,因为它们执行相同的操作)然后0设置为 ,例如,如果您将其设置1000为,那么根据我的测试,它不会因某种原因停止。

于 2013-06-25T18:05:11.937 回答
1

您可以使用rotation.setFillAfter(true);

于 2019-07-05T11:30:22.527 回答
-2

iv.clearAnimation(); 停止动画

于 2016-02-26T03:52:25.563 回答