Android学习笔记--动画特效

一身转战三千里,一剑曾当百万师。这篇文章主要讲述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 20private ImageView imgView; 21private boolean isCheched = true; 22 23@Override 24protected void onCreate(Bundle savedInstanceState) { 25super.onCreate(savedInstanceState); 26setContentView(R.layout.activity_main); 27 28 29Switch switch1 = (Switch) findViewById(R.id.switch1); 30switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 31@Override 32public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 33MainActivity.this.isCheched = isChecked; 34} 35}); 36 37 38findViewById(R.id.button).setOnClickListener(this); 39findViewById(R.id.button2).setOnClickListener(this); 40findViewById(R.id.button3).setOnClickListener(this); 41findViewById(R.id.button4).setOnClickListener(this); 42findViewById(R.id.button5).setOnClickListener(this); 43imgView = (ImageView) findViewById(R.id.imageView); 44} 45 46 47@Override 48public void onClick(View v) { 49switch (v.getId()){ 50case R.id.button://渐变透明动画 51if(isCheched){ 52alphajava(); 53}else{ 54loadByXml(R.anim.alpha_animation); 55} 56break; 57case R.id.button2://缩放动画 58 //ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1); 59 //scaleJava(); 60loadByXml(R.anim.scal_animation); 61break; 62case R.id.button3://位移动画 63 //translateJava(); 64loadByXml(R.anim.translate_animation); 65break; 66case R.id.button4://旋转动画 67 //rotateJava(); 68loadByXml(R.anim.rotate_animation); 69break; 70case R.id.button5://动画集 71AnimationSet animationSet = new AnimationSet(true); 72 73//创建三个动画 74RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f, 75Animation.RELATIVE_TO_SELF,0.5f); 76ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f, 77Animation.RELATIVE_TO_SELF,0.5f); 78TranslateAnimation translateAnimation = new TranslateAnimation(0,800,0,800); 79 80//将动画添加到动画集中 81animationSet.addAnimation(rotateAnimation); 82animationSet.addAnimation(scaleAnimation); 83animationSet.addAnimation(translateAnimation); 84 85//设置时长 86animationSet.setDuration(3000); 87 88//启动动画 89imgView.startAnimation(animationSet); 90break; 91} 92} 93 94/** 95* 位移动画 96*/ 97private void translateJava() { 98TranslateAnimation translateAnimation = new TranslateAnimation(0,800,0,800); 99translateAnimation.setDuration(3000); 100imgView.startAnimation(translateAnimation); 101} 102 103/** 104* 旋转动画 105*/ 106private void rotateJava() { 107RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f, 108Animation.RELATIVE_TO_SELF,0.5f); 109rotateAnimation.setDuration(3000); 110imgView.startAnimation(rotateAnimation); 111} 112 113/** 114* 缩放动画 115*/ 116private void scaleJava() { 117ScaleAnimation scaleAnimation = new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f, 118Animation.RELATIVE_TO_SELF,0.5f); 119scaleAnimation.setDuration(3000); 120imgView.startAnimation(scaleAnimation); 121} 122 123/** 124* 通过XMl加载动画 125* @param resId 动画资源,布局文件 126*/ 127private void loadByXml(@AnimRes int resId) { 128Animation animation = AnimationUtils.loadAnimation(this,resId); 129imgView.startAnimation(animation); 130} 131 132/** 133* 渐变透明动画 134*/ 135private void alphaJava(){ 136AlphaAnimation alphaAnimation = new AlphaAnimation(1,0); 137alphaAnimation.setDuration(3000); 138imgView.startAnimation(alphaAnimation); 139} 140 }

【Android学习笔记--动画特效】 

    推荐阅读