package com.example.viewanimationtest; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.util.Log; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; /* *Since the child thread cannot directly modify the main UI, *it is implemented using the Handler mechanism. * */ public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private ImageView mImageView; public static final int TRANSLATE_ANI = 1 ; public static final int ROTATE_ANI = 2 ; public static final int SCALE_ANI = 3 ; public static final int ALPHA_ANI = 4 ; private Handler mHandler = new Handler(Looper.getMainLooper()){ @Override public void handleMessage( @NonNull Message msg) { switch (msg.what) { case TRANSLATE_ANI: mImageView.clearAnimation(); //When setting a new animation, first clear the previous animation. Animation translateAnimation = new TranslateAnimation( 0 , 500 , 0 , 500 ); //The animation moves from(0,0)to (500,500). translateAnimation.setDuration( 2000 ); mImageView.setAnimation(translateAnimation); break ; case ROTATE_ANI: mImageView.clearAnimation(); Animation rotateAnimation = new RotateAnimation( 0 , 360 , 0 , 0 ); //The animation rotates from 0° to 360° around(0,0). rotateAnimation.setDuration( 2000 ); mImageView.setAnimation(rotateAnimation); break ; case SCALE_ANI: mImageView.clearAnimation(); Animation scaleAnimation = new ScaleAnimation( 0 , 1 , 0 , 1 ); scaleAnimation.setDuration( 2000 ); mImageView.setAnimation(scaleAnimation); break ; case ALPHA_ANI: mImageView.clearAnimation(); Animation alphaAnimation = new AlphaAnimation( 0 , 1 ); alphaAnimation.setDuration( 2000 ); mImageView.setAnimation(alphaAnimation); break ; default : Log.d( "1111" , "default" ); break ; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImageView =(ImageView)findViewById(R.id.image_view); Button translateButton = (Button) findViewById(R.id.translate_button); Button scaleButton = (Button) findViewById(R.id.scale_button); Button rotateButton = (Button) findViewById(R.id.rotate_button); Button alphaButton = (Button) findViewById(R.id.alpha_button); translateButton.setOnClickListener( this ); scaleButton.setOnClickListener( this ); rotateButton.setOnClickListener( this ); alphaButton.setOnClickListener( this ); mImageView.clearAnimation(); //If some animations exist, clear them first. //set mix animation AnimationSet animationSet = new AnimationSet( true ); //set default alpha animation Animation alphaAnimation = new AlphaAnimation( 0 , 1 ); //The animation is from fully transparent to fully opaque. alphaAnimation.setDuration( 2000 ); //The animation lasts 2 seconds. //set default scale animation Animation scaleAnimation = new ScaleAnimation( 0 , 1 , 0 , 1 ); //The animation is from 0% to 100%. scaleAnimation.setDuration( 2000 ); //add sub-animations to combined animation animationSet.addAnimation(alphaAnimation); animationSet.addAnimation(scaleAnimation); //show the combined animation mImageView.setAnimation(animationSet); } @SuppressLint ( "NonConstantResourceId" ) @Override public void onClick(View v) { switch (v.getId()) { case R.id.translate_button: new Thread( new Runnable() { @Override public void run() { Message msg = new Message(); msg.what = TRANSLATE_ANI; mHandler.sendMessage(msg); } }).start(); break ; case R.id.rotate_button: new Thread( new Runnable() { @Override public void run() { Message msg = new Message(); msg.what = ROTATE_ANI; mHandler.sendMessage(msg); } }).start(); break ; case R.id.scale_button: new Thread( new Runnable() { @Override public void run() { Message msg = new Message(); msg.what = SCALE_ANI; mHandler.sendMessage(msg); } }).start(); break ; case R.id.alpha_button: new Thread( new Runnable() { @Override public void run() { Message msg = new Message(); msg.what = ALPHA_ANI; mHandler.sendMessage(msg); } }).start(); break ; default : break ; } } } |