Search code examples
androidrandomimage-rotation

rotate images in random duration


i want to make a truth or dare app.i want to rotate an image in random duration.

according to code below(i found it in Android: Rotate image in imageview by an angle) i made an animation xml and set a duration for it.

<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"
/>

i guess i shall use a random function in main activity to get random number for random duration.

so, how shall i put this random number (which i got in main activity) in duration in animation xml?


Solution

  • use setDuration method of Animation to set duration programaticaally

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