Android学习笔记--动画特效

直接上代码,有事先不多写,以后在补

MainActivity

  1 package com.wuxianedu.animation;
  2 
  3 import android.support.annotation.AnimRes;
  4 import android.support.v7.app.AppCompatActivity;
  5 import android.os.Bundle;
  6 import android.view.View;
  7 import android.view.animation.AlphaAnimation;
  8 import android.view.animation.Animation;
  9 import android.view.animation.AnimationSet;
 10 import android.view.animation.AnimationUtils;
 11 import android.view.animation.RotateAnimation;
 12 import android.view.animation.ScaleAnimation;
 13 import android.view.animation.TranslateAnimation;
 14 import android.widget.CompoundButton;
 15 import android.widget.ImageView;
 16 import android.widget.Switch;
 17 
 18 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 19 
 20     private ImageView imgView;
 21     private boolean isCheched = true;
 22 
 23     @Override
 24     protected void onCreate(Bundle savedInstanceState) {
 25         super.onCreate(savedInstanceState);
 26         setContentView(R.layout.activity_main);
 27 
 28 
 29         Switch switch1 = (Switch) findViewById(R.id.switch1);
 30         switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
 31             @Override
 32             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 33                 MainActivity.this.isCheched = isChecked;
 34             }
 35         });
 36 
 37 
 38         findViewById(R.id.button).setOnClickListener(this);
 39         findViewById(R.id.button2).setOnClickListener(this);
 40         findViewById(R.id.button3).setOnClickListener(this);
 41         findViewById(R.id.button4).setOnClickListener(this);
 42         findViewById(R.id.button5).setOnClickListener(this);
 43         imgView = (ImageView) findViewById(R.id.imageView);
 44     }
 45 
 46 
 47     @Override
 48     public void onClick(View v) {
 49         switch (v.getId()){
 50             case R.id.button://渐变透明动画
 51                 if(isCheched){
 52                     alphaJava();
 53                 }else{
 54                     loadByXml(R.anim.alpha_animation);
 55                 }
 56                 break;
 57             case R.id.button2://缩放动画
 58 //                ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1);
 59 //                scaleJava();
 60                 loadByXml(R.anim.scal_animation);
 61                 break;
 62             case R.id.button3://位移动画
 63 //                translateJava();
 64                 loadByXml(R.anim.translate_animation);
 65                 break;
 66             case R.id.button4://旋转动画
 67 //                rotateJava();
 68                 loadByXml(R.anim.rotate_animation);
 69                 break;
 70             case R.id.button5://动画集
 71                 AnimationSet animationSet = new AnimationSet(true);
 72 
 73                 //创建三个动画
 74                 RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,
 75                         Animation.RELATIVE_TO_SELF,0.5f);
 76                 ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,
 77                         Animation.RELATIVE_TO_SELF,0.5f);
 78                 TranslateAnimation translateAnimation = new TranslateAnimation(0,800,0,800);
 79 
 80                 //将动画添加到动画集中
 81                 animationSet.addAnimation(rotateAnimation);
 82                 animationSet.addAnimation(scaleAnimation);
 83                 animationSet.addAnimation(translateAnimation);
 84 
 85                 //设置时长
 86                 animationSet.setDuration(3000);
 87 
 88                 //启动动画
 89                 imgView.startAnimation(animationSet);
 90                 break;
 91         }
 92     }
 93 
 94     /**
 95      * 位移动画
 96      */
 97     private void translateJava() {
 98         TranslateAnimation translateAnimation = new TranslateAnimation(0,800,0,800);
 99         translateAnimation.setDuration(3000);
100         imgView.startAnimation(translateAnimation);
101     }
102 
103     /**
104      * 旋转动画
105      */
106     private void rotateJava() {
107         RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,
108                 Animation.RELATIVE_TO_SELF,0.5f);
109         rotateAnimation.setDuration(3000);
110         imgView.startAnimation(rotateAnimation);
111     }
112 
113     /**
114      * 缩放动画
115      */
116     private void scaleJava() {
117         ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,
118                 Animation.RELATIVE_TO_SELF,0.5f);
119         scaleAnimation.setDuration(3000);
120         imgView.startAnimation(scaleAnimation);
121     }
122 
123     /**
124      * 通过XMl加载动画
125      * @param resId 动画资源,布局文件
126      */
127     private void loadByXml(@AnimRes int resId) {
128         Animation animation = AnimationUtils.loadAnimation(this,resId);
129         imgView.startAnimation(animation);
130     }
131 
132     /**
133      * 渐变透明动画
134      */
135     private void alphaJava(){
136         AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
137         alphaAnimation.setDuration(3000);
138         imgView.startAnimation(alphaAnimation);
139     }
140 }
原文地址:https://www.cnblogs.com/langfei8818/p/6067437.html