温馨提示×

Android中如何实现旋转动画

小亿
122
2024-04-23 14:53:44
栏目: 编程语言

在Android中实现旋转动画可以通过使用属性动画或补间动画来实现。以下是两种不同方法的示例:

  1. 使用属性动画实现旋转动画:
ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(yourView, "rotation", 0f, 360f);
rotateAnimator.setDuration(1000); // 设置动画持续时间
rotateAnimator.setRepeatCount(ObjectAnimator.INFINITE); // 设置动画重复次数,可以设置为INFINITE表示无限循环
rotateAnimator.setInterpolator(new LinearInterpolator()); // 设置动画插值器
rotateAnimator.start(); // 开始动画
  1. 使用补间动画实现旋转动画:

在res/anim文件夹下创建一个rotate.xml文件,内容如下:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"
        android:repeatCount="infinite" />
</set>

然后在代码中使用AnimationUtils加载这个动画并应用到View上:

Animation rotateAnimation = AnimationUtils.loadAnimation(context, R.anim.rotate);
yourView.startAnimation(rotateAnimation);

以上就是在Android中实现旋转动画的两种方式,开发者可以根据具体需求选择适合的方法来实现。

0