gpt4 book ai didi

android - 如何在循环结束时停止动画?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:35:02 25 4
gpt4 key购买 nike

我有一个正在旋转的 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);

最佳答案

在开始之前将动画重复次数设置为无限,然后当 Action 结束时,将动画的重复次数设置为 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 例如,根据我的测试,它不会因为某种原因停止。

关于android - 如何在循环结束时停止动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17303972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com