随机旋转图像

rotate images in random duration

我想来一场真心话大冒险app.i想随机旋转一张图片。

根据下面的代码(我在Android: Rotate image in imageview by an angle中找到了) 我制作了一个动画 xml 并为其设置了持续时间。

<rotate
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromDegrees="45"
  android:toDegrees="45"
  android:pivotX="50%"
  android:pivotY="50%"
  android:duration="0"
  android:startOffset="0"
/>

我想我应该在 main activity 中使用随机函数来获得随机持续时间的随机数。

所以,我应该如何将这个随机数(我在主要 activity 中得到)放入动画的持续时间 xml?

使用动画的setDuration方法以编程方式设置持续时间

Animation animation = AnimationUtils.loadAnimation(context, R.anim.your_animation);
animation.setDuration(300);
view.startAnimation(animation);