Android AnimationSet for multiple animations

This animation demo combines scaling and rotation animations, plays them together. The wheel image used in this demo grows bigger and rotates at the same time. The scaling and rotation animations are created and then added to the AnimationSet, then start the animation on the image with this AnimationSet.

activity_scale_rotation.xml, the layout file for the scaling and rotation animations 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="Scale and Rotate"
        android:onClick="scaleRotate"/>
    <ImageView
        android:id="@+id/iv_wheel"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="100dp"
        android:layout_gravity="center"
        android:src="@drawable/wheel_image" />
</LinearLayout>

ScaleRotationActivity.java, the activity file for the scaling and rotation animation demo.

public class ScaleRotationActivity extends AppCompatActivity {

    private ImageView ivWheel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scale_rotation);
        ivWheel = (ImageView) findViewById(R.id.iv_wheel);
    }

    public void scaleRotate(View v) {
        ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 3.0f, 1f, 3.0f, ivWheel.getWidth() / 2.0f, ivWheel.getHeight() / 2.0f);
        scaleAnimation.setDuration(3000); // scale to 3 times as big in 3 seconds
        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationStart(Animation animation) {
                Log.d("ScaleRotationActivity", "Scale started...");
            }
            public void onAnimationEnd(Animation animation) {
                Log.d("ScaleRotationActivity", "Scale ended...");
            }
            public void onAnimationRepeat(Animation animation) {

            }
        });

        float mAngleToRotate = 360f * 12; // rotate 12 rounds
        RotateAnimation wheelRotation = new RotateAnimation(0.0f, mAngleToRotate, ivWheel.getWidth()/2.0f, ivWheel.getHeight()/2.0f);
        wheelRotation.setDuration(5000); // rotate 12 rounds in 5 seconds
        wheelRotation.setInterpolator(this, android.R.interpolator.accelerate_decelerate);
        wheelRotation.setAnimationListener(new Animation.AnimationListener() {
            public void onAnimationStart(Animation animation) {
                Log.d("ScaleRotationActivity", "Rotation started...");
            }
            public void onAnimationEnd(Animation animation) {
                Log.d("ScaleRotationActivity", "Rotation ended...");
            }
            public void onAnimationRepeat(Animation animation) {

            }
        });

        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(wheelRotation);
        animationSet.addAnimation(scaleAnimation);
        ivWheel.startAnimation(animationSet);
    }
}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search