Collins
Collins

Reputation: 145

How to rotate image slowly in android?

I want to rotate an Image in a slow way on Android. I can do this by creating a Bitmap and by the help of of Matrix class. But i don't know how to make it slow, like it should take 3 seconds to rotate.

Upvotes: 1

Views: 1507

Answers (3)

Code on the Rocks
Code on the Rocks

Reputation: 17764

In Kotlin you can use the ObjectAnimator do this super easily. For example:

ObjectAnimator.ofFloat(view, "rotationX", 180f).apply {
duration = 2000
start()
}
  • view: the view you want to rotate
  • "rotationX": the propertyName you want to alter.
    "rotationX" gives you rotation into and out of the screen
    "rotationY" gives you clockwise/counterclockwise rotation
  • 180f: how many degrees you want to rotate the view
  • duration: the number of milliseconds the animation should take to complete

Upvotes: 1

Sohail Zahid
Sohail Zahid

Reputation: 8149

Rotate

Rotate animation uses tag. For rotate animation required tags are android:fromDegrees and android:toDegrees which defines rotation angles.

Clock wise – use positive toDegrees value

Anti clock wise – use negative toDegrees value

rotate.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>

Save in anim folder

enter image description here

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

Upvotes: 1

Kumar M
Kumar M

Reputation: 1014

You can use rotate animation to achieve this.

Create anim folder under res directoy inside that place this xml.

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>

Set animation to view like this.

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

Upvotes: 0

Related Questions