public class MainActivity extends AppCompatActivity { private ImageView iv; private AnimationDrawable ad; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = ((ImageView) findViewById(R.id.iv)); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "123", Toast.LENGTH_SHORT).show(); } }); ImageView iv1 = (ImageView) findViewById(R.id.iv1); ad = (AnimationDrawable) iv1.getDrawable(); iv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //如果动画正在执行,先停止,再执行 if(ad.isRunning()){ ad.stop(); } ad.start(); } }); } public void translateClick1(View view) { //1.X轴移动的起始位置 //2.X轴移动的结束位置 //3.Y轴移动的起始位置 //4.Y轴移动的结束位置 TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 0); //动画执行的时间,5000毫秒 ta.setDuration(5000); //设置动画结束后ImageView保持动画结束时的状态 ta.setFillAfter(true); //让ImageView产生动画效果 iv.startAnimation(ta); } public void alphaClick1(View view) { //1.表示动画的起始状态,1表示完全不透明,0表示完全透明 //2.表示动画的结束状态 AlphaAnimation aa = new AlphaAnimation(1, 0); aa.setDuration(3000); iv.startAnimation(aa); } public void scaleClick1(View view) { //1.X轴起始状态 //2.X轴结束状态 //5。6表示缩放的中心点 // ScaleAnimation sa = new ScaleAnimation(0, 2, 0, 1, iv.getWidth() / 2, iv.getHeight() / 2); //缩放中心点参考ImageView的中心点,0.5f表示宽度/高度的50% ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(3000); iv.startAnimation(sa); } public void rotateClick1(View view) { //1.旋转的起始位置 //2.旋转的结束位置 //3.4表示旋转的中心点 // RotateAnimation ra = new RotateAnimation(0, 360, iv.getWidth() / 2, iv.getHeight() / 2); RotateAnimation ra = new RotateAnimation(0, 359, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f); //设置旋转次数 ra.setRepeatCount(RotateAnimation.INFINITE); //设置重复旋转的模式 // ra.setRepeatMode(RotateAnimation.REVERSE); ra.setDuration(3000); ra.setInterpolator(new LinearInterpolator()); iv.startAnimation(ra); } public void setClick1(View view) { //true表示动画的插值器统一使用AnimationSet默认的插值器 AnimationSet set = new AnimationSet(true); TranslateAnimation ta = new TranslateAnimation(0, iv.getWidth(), 0, iv.getHeight()); ta.setDuration(3000); //将所有动画添加到set中 set.addAnimation(ta); AlphaAnimation aa = new AlphaAnimation(0.2f, 0.8f); aa.setDuration(3000); set.addAnimation(aa); ScaleAnimation sa = new ScaleAnimation(1, 2, 1, 2, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(3000); set.addAnimation(sa); RotateAnimation ra = new RotateAnimation(0, -359, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(3000); set.addAnimation(ra); iv.startAnimation(set); } public void translateClick2(View view) { Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate_anim); animation.setFillAfter(true); iv.startAnimation(animation); } public void alphaClick2(View view) { Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_anim); iv.startAnimation(animation); } public void scaleClick2(View view) { Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale_anim); iv.startAnimation(animation); } public void rotateClick2(View view) { Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim); iv.startAnimation(animation); } public void setClick9(View view) { Animation animation = AnimationUtils.loadAnimation(this, R.anim.set_anim); iv.startAnimation(animation); } public void startNewActivity(View view) { startActivity(new Intent(this, Main2Activity.class)); //添加Activity切换时的动画 overridePendingTransition(R.anim.enter, R.anim.exit); } }