如何在Android中进行流畅的图像旋转?

emm*_*mby 209 animation android view

我正在使用a RotateAnimation来旋转我在Android中用作自定义循环微调器的图像.这是我的rotate_indefinitely.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" />    
Run Code Online (Sandbox Code Playgroud)

当我将它应用于我的ImageView使用时AndroidUtils.loadAnimation(),效果很好!

spinner.startAnimation( 
    AnimationUtils.loadAnimation(activity, R.anim.rotate_indefinitely) );
Run Code Online (Sandbox Code Playgroud)

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

换句话说,图像旋转360度,暂停,然后再旋转360度等.

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

如何关闭插值(如果这确实是问题)以使我的动画循环顺利进行?

Bak*_*yor 195

你是对的AccelerateInterpolator; 你应该使用LinearInterpolator.

您可以使用android.R.anim.linear_interpolator动画XML文件中的内置函数android:interpolator="@android:anim/linear_interpolator".

或者您可以在项目中创建自己的XML插值文件,例如命名res/anim/linear_interpolator.xml:

<?xml version="1.0" encoding="utf-8"?>
<linearInterpolator xmlns:android="http://schemas.android.com/apk/res/android" />
Run Code Online (Sandbox Code Playgroud)

并添加到您的动画XML:

android:interpolator="@anim/linear_interpolator"
Run Code Online (Sandbox Code Playgroud)

特别注意:如果你的旋转动画在一个集合中,设置插值器似乎不起作用.旋转顶部元素可以修复它.(这会节省你的时间.)

  • 我已经尝试了所有可用的插补器,包括线性,我仍然在每个周期的开始时得到这个小"挂钩". (24认同)
  • 如果你的旋转动画在一个集合中,设置插值器似乎不起作用.旋转顶部元素可以修复它 (11认同)
  • 将`<rotate>`元素移出`<set>`解决了它. (5认同)

Rab*_*s G 83

我也有这个问题,并尝试在xml中设置线性插值器而没有成功.对我有用的解决方案是在代码中将动画创建为RotateAnimation.

RotateAnimation rotate = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(5000);
rotate.setInterpolator(new LinearInterpolator());

ImageView image= (ImageView) findViewById(R.id.imageView);

image.startAnimation(rotate);
Run Code Online (Sandbox Code Playgroud)

  • 如果你想让动画保持在最后的方向,添加`rotate.setFillAfter(true);` (4认同)
  • 如果您希望动画永久无休止地添加,请添加`rotate.setRepeatCount(Animation.INFINITE);` (3认同)

Lui*_*dez 35

这很好用

<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1600"
    android:fromDegrees="0"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:toDegrees="358" />
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="1600"
    android:fromDegrees="358"
    android:interpolator="@android:anim/linear_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:toDegrees="0" />
Run Code Online (Sandbox Code Playgroud)

  • 反过来,只需要repeatcount 2并设置android:repeatMode ="reverse" - 不需要有两个不同的XML文件. (5认同)
  • 为什么358而不是359或360? (3认同)

Vit*_*nko 30

也许,这样的事情会有所帮助:

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();
Run Code Online (Sandbox Code Playgroud)

顺便说一句,你可以旋转超过360像:

imageView.animate().rotationBy(10000)...
Run Code Online (Sandbox Code Playgroud)


Fer*_*ego 29

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


Ash*_*man 13

ObjectAnimator.ofFloat(view, View.ROTATION, 0f, 360f).setDuration(300).start();
Run Code Online (Sandbox Code Playgroud)

尝试这个。


Chr*_*ock 6

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

感谢Shalafi!

所以你的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"
    />
Run Code Online (Sandbox Code Playgroud)


Gee*_*kur 6

以编程方式旋转对象。

//顺时针旋转:

    public void rorate_Clockwise(View view) {
        ObjectAnimator rotate = ObjectAnimator.ofFloat(view, "rotation", 180f, 0f);
//        rotate.setRepeatCount(10);
        rotate.setDuration(500);
        rotate.start();
    }
Run Code Online (Sandbox Code Playgroud)

//逆时针旋转:

 public void rorate_AntiClockwise(View view) {
        ObjectAnimator rotate = ObjectAnimator.ofFloat(view, "rotation", 0f, 180f);
//        rotate.setRepeatCount(10);
        rotate.setDuration(500);
        rotate.start();
    } 
Run Code Online (Sandbox Code Playgroud)

视图是您的ImageView或其他小部件的对象。

rotation.setRepeatCount(10); 用于重复旋转。

500是您的动画持续时间。


小智 5

正如 hanry 上面提到的,放置线性迭代器就可以了。但如果旋转位于集合内,则必须放置 android:shareInterpolator="false" 以使其平滑。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
**android:shareInterpolator="false"**
>
<rotate
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="300"
    android:fillAfter="true"
    android:repeatCount="10"
    android:repeatMode="restart"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%" />
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="3000"
    android:fillAfter="true"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:toXScale="0"
    android:toYScale="0" />
</set>
Run Code Online (Sandbox Code Playgroud)

如果 Sharedinterpolator 不为 false,则上述代码会出现故障。


Hit*_*ahu 5

在科特林:

 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)
        })
Run Code Online (Sandbox Code Playgroud)