我正在使用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),但我不知道如何告诉它不插值动画。

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


当前回答

也许,这样做会有帮助:

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        imageView.animate().rotationBy(360).withEndAction(this).setDuration(3000).setInterpolator(new LinearInterpolator()).start();
    }
};

imageView.animate().rotationBy(360).withEndAction(runnable).setDuration(3000).setInterpolator(new LinearInterpolator()).start();

顺便说一下,你可以像这样旋转360度以上:

imageView.animate().rotationBy(10000)...

其他回答

你可以使用下面的代码:

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

无论我怎么尝试,我都无法使用代码(和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);
        }

尝试使用超过360以避免重新启动。

我使用3600而不是360,这对我来说很好:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="3600"
    android:interpolator="@android:anim/linear_interpolator"
    android:repeatCount="infinite"
    android:duration="8000"
    android:pivotX="50%"
    android:pivotY="50%" />

100%正确的答案

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

100%的工作! !

在芬兰湾的科特林:

 ivBall.setOnClickListener(View.OnClickListener {

            //Animate using XML
            // val rotateAnimation = AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely)

            //OR using Code
            val rotateAnimation = RotateAnimation(
                    0f, 359f,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f

            )
            rotateAnimation.duration = 300
            rotateAnimation.repeatCount = 2

            //Either way you can add Listener like this
            rotateAnimation.setAnimationListener(object : Animation.AnimationListener {

                override fun onAnimationStart(animation: Animation?) {
                }

                override fun onAnimationRepeat(animation: Animation?) {
                }

                override fun onAnimationEnd(animation: Animation?) {

                    val rand = Random()
                    val ballHit = rand.nextInt(50) + 1
                    Toast.makeText(context, "ballHit : " + ballHit, Toast.LENGTH_SHORT).show()
                }
            })

            ivBall.startAnimation(rotateAnimation)
        })