Android rotation animation

Roate the wheel image in 360 * 12 degree around the center of the wheel image in 3 seconds. The pivot point of the roation will be the center of the wheel image. The x position is half the width of the image and the y position is the half the height of the image.

float mAngleToRotate = 360f * 12; // rotate 12 rounds
RotateAnimation wheelRotation = new RotateAnimation(0.0f, mAngleToRotate, ivWheel.getWidth()/2.0f, ivWheel.getHeight()/2.0f);
wheelRotation.setDuration(3000); // rotate 12 rounds in 3 seconds
wheelRotation.setInterpolator(this, android.R.interpolator.accelerate_decelerate);
ivWheel.startAnimation(wheelRotation);

wheelRotation.setAnimationListener(new Animation.AnimationListener() {
    public void onAnimationStart(Animation animation) {
        Log.d("RotationActivity", "Rotation started...");
    }
    
    public void onAnimationEnd(Animation animation) {
        Log.d("RotationActivity", "Rotation ended...");
    }

    public void onAnimationRepeat(Animation animation) {

    }
});

activity_rotate.xml, the layout file for the rotation animation demo.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btn_rotate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Rotate"
        android:onClick="rotate"/>
    <ImageView
        android:id="@+id/iv_wheel"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:src="@drawable/wheel_image"
        />
</LinearLayout>

RotateActivity.java, the activity file for the rotation animation demo.

public class RotationActivity extends AppCompatActivity {

    private ImageView ivWheel;

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

        ivWheel = (ImageView) findViewById(R.id.iv_wheel);
    }

    /**
     * Roate the wheel image in 360 * 12 degree around the center of the wheel image in 3 seconds
     * The center of the wheel image is ivWheel.getWidth()/2.0f, ivWheel.getHeight()/2.0f
     * @param v
     */
    public void rotate(View v) {
        float mAngleToRotate = 360f * 12; // rotate 12 rounds
        RotateAnimation wheelRotation = new RotateAnimation(0.0f, mAngleToRotate, ivWheel.getWidth()/2.0f, ivWheel.getHeight()/2.0f);
        wheelRotation.setDuration(3000); // rotate 12 rounds in 3 seconds
        wheelRotation.setInterpolator(this, android.R.interpolator.accelerate_decelerate);
        ivWheel.startAnimation(wheelRotation);

        wheelRotation.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationEnd(Animation animation) {
                Log.d("RotationActivity", "Roation started...");
            }

            public void onAnimationRepeat(Animation animation) {

            }

            public void onAnimationStart(Animation animation) {
                Log.d("RotationActivity", "Roation ended...");
            }
        });
    }

}

Complete example in Github

Search within Codexpedia

Custom Search

Search the entire web

Custom Search