【问题标题】:How to rotate image slowly in android?如何在android中缓慢旋转图像?
【发布时间】:2016-06-16 08:25:11
【问题描述】:

我想在 Android 上以缓慢的方式旋转图像。 我可以通过创建一个位图并通过 Matrix 类的帮助。但我不知道如何让它变慢,比如旋转应该需要 3 秒。

【问题讨论】:

标签: android matrix bitmap rotation


【解决方案1】:

旋转

旋转动画使用标签。对于旋转动画,需要的标签是 android:fromDegreesandroid:toDegrees,它们定义了旋转角度。

顺时针 - 使用正 toDegrees 值

逆时针方向 - 使用负 toDegrees 值

旋转.xml

<?xml version="1.0" encoding="utf-8"?>
<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="600"
        android:repeatMode="restart"
        android:repeatCount="infinite"
        android:interpolator="@android:anim/cycle_interpolator"/>

</set>

保存在动画文件夹中

public class AnimationActivity extends Activity{

    ImageView img;
    Animation rotate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fadein);

        img = (ImageView) findViewById(R.id.myimageid);

        // load the animation
        rotate = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.rotate);
        img.startAnimation(rotate);

    }
}

Complete Tutorial

【讨论】:

    【解决方案2】:

    在 Kotlin 中,您可以使用 ObjectAnimator 超级轻松地做到这一点。例如:

    ObjectAnimator.ofFloat(view, "rotationX", 180f).apply {
    duration = 2000
    start()
    }
    
    • 视图:要旋转的视图
    • “rotationX”:您要更改的属性名称。
      “rotationX”让您旋转进出屏幕
      “rotationY”为您提供顺时针/逆时针旋转
    • 180f:您希望将视图旋转多少度
    • 持续时间:动画完成所需的毫秒数

    【讨论】:

      【解决方案3】:

      您可以使用旋转动画来实现这一点。

      在该 xml 所在的 res 目录下创建 anim 文件夹。

      rotate_around_center_point.xml

      <?xml version="1.0" encoding="utf-8"?>
      <set xmlns:android="http://schemas.android.com/apk/res/android"
          android:shareInterpolator="false" >
      
          <rotate
              android:duration="2500"
              android:interpolator="@android:anim/linear_interpolator"
              android:pivotX="50%"
              android:pivotY="50%"
              android:repeatCount="infinite"
              android:repeatMode="restart"
              android:toDegrees="360" />
      
      </set>
      

      将动画设置为这样查看。

      ImageView animationTarget = (ImageView) this.findViewById(R.id.testImage);
      Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_around_center_point);
      animationTarget.startAnimation(animation);
      

      【讨论】:

      • 你想要动画的持续时间可以从 xm 更改。
      猜你喜欢
      • 2011-07-20
      • 1970-01-01
      • 2023-03-31
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多