我正在使用RotateAnimation来旋转我在Android中用作自定义循环旋转器的图像。下面是我的rotate_indefinite .xml文件,我把它放在res/anim/目录下:

<?xml version="1.0" encoding="UTF-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:duration="1200" />    

当我将此应用到我的ImageView使用AndroidUtils.loadAnimation(),它的工作很棒!

spinner.startAnimation( 
    AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely) );

唯一的问题是,图像旋转似乎在每个周期的顶部暂停。

换句话说,图像旋转360度,短暂停顿,然后再次旋转360度,等等。

我怀疑问题是动画使用默认的插值器,如android:iterpolator="@android:anim/accelerate_interpolator" (AccelerateInterpolator),但我不知道如何告诉它不插值动画。

我怎么能关闭插值(如果这确实是问题),使我的动画周期顺利?


当前回答

你可以使用下面的代码:

view.animate().rotation(360.0f).setDuration(1000);

其他回答

private fun rotateTheView(view: View?, startAngle: Float, endAngle: Float) {
    val rotate = ObjectAnimator.ofFloat(view, "rotation", startAngle, endAngle)
    //rotate.setRepeatCount(10);
    rotate.duration = 400
    rotate.start()
}

无论我怎么尝试,我都无法使用代码(和setRotation)来平滑旋转动画。我最终做的是把度数变化做得非常小,以至于小的停顿都不明显。如果你不需要做太多的旋转,执行这个循环的时间可以忽略不计。效果是平滑的旋转:

        float lastDegree = 0.0f;
        float increment = 4.0f;
        long moveDuration = 10;
        for(int a = 0; a < 150; a++)
        {
            rAnim = new RotateAnimation(lastDegree, (increment * (float)a),  Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
            rAnim.setDuration(moveDuration);
            rAnim.setStartOffset(moveDuration * a);
            lastDegree = (increment * (float)a);
            ((AnimationSet) animation).addAnimation(rAnim);
        }

100%正确的答案

var rotate =旋转动画( 0 f,华氏360度, 动画。RELATIVE_TO_SELF, 0.5度, 动画。RELATIVE_TO_SELF, 0.5度 ) 旋转。持续时间= 999 旋转。repeatCount =动画。无限 imageview . . setAnimation(旋转)

100%的工作! !

尝试使用toDegrees="359",因为360°和0°是相同的。

修剪包裹<rotate>-Element的<set>-Element可以解决这个问题!

感谢沙拉菲!

所以你的Rotation_ccw.xml应该是这样的:

<?xml version="1.0" encoding="utf-8"?>

<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="-360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="2000"
    android:fillAfter="false"
    android:startOffset="0"
    android:repeatCount="infinite"
    android:interpolator="@android:anim/linear_interpolator"
    />