博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android动画-动画分类及代码演示样例
阅读量:6940 次
发布时间:2019-06-27

本文共 6137 字,大约阅读时间需要 20 分钟。

原来一直对动画一知半解,仅仅知道依照网上的方法会用即可了,可是自己写起来感觉确实有点费劲,今天最终研究了代码实现,一下子感觉清晰多了。先把总结例如以下,代码中有具体的凝视。

动画分类

   1.Peoperty Animation

      这个动画是Android3.0之后推出的眼下用处不大。

   2.View Animation

       这类动画也叫tween animation 主要分为 渐变动画(AlphaAnimation)旋转动画(RotateAnimation)

缩放动画(ScaleAnimation)位移动画(TranslateAnimation)

 3.Drawable Animation

      这类动画也叫帧动画 FrameAnimation

先上tween animation

MainActivity.java

package com.example.testanimation;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;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;public class MainActivity extends Activity {	private ImageView imgView;	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);				imgView = (ImageView)this.findViewById(R.id.imgView) ;		Button btnAlpha = (Button)this.findViewById(R.id.btnAlpha) ;		btnAlpha.setOnClickListener(new AnimationOnClickListener(AnimationType.alpha));		Button btnRotate = (Button)this.findViewById(R.id.btnRotate) ;		btnRotate.setOnClickListener(new AnimationOnClickListener(AnimationType.rotate));		Button btnTraslate = (Button)this.findViewById(R.id.btnTraslate) ;		btnTraslate.setOnClickListener(new AnimationOnClickListener(AnimationType.traslate));		Button btnScale = (Button)this.findViewById(R.id.btnScale) ;		btnScale.setOnClickListener(new AnimationOnClickListener(AnimationType.scale));		Button btnComplex = (Button)this.findViewById(R.id.btnComplex) ;		btnComplex.setOnClickListener(new AnimationOnClickListener(AnimationType.complex));					}		enum AnimationType {		alpha,		rotate,		traslate,		scale,		complex	}		class AnimationOnClickListener implements OnClickListener {				private AnimationType mAnimationType;				public AnimationOnClickListener (AnimationType animationType) {			this.mAnimationType = animationType;		}				@Override		public void onClick(View v) {			switch (mAnimationType) {			case alpha:				/**				 * 透明度从不透明变为0.2透明度				 */				AlphaAnimation _alphaAnimation = new AlphaAnimation(1.0f, 0.2f);				_alphaAnimation.setDuration(200);				_alphaAnimation.setFillAfter(true);//动画运行完的状态显示				imgView.startAnimation(_alphaAnimation);				break;			case rotate:				/**				 * RotateAnimation 以图片中点为圆心旋转360度				 * params:				 * pivotXType 中心点x坐标类型 RELATIVE_TO_SELF相对于自己,RELATIVE_TO_PARENT相对于父view				 * pivotYType 同上				 * 				 * pivotXValue,pivotYValue(圆心)				 * 				 */				RotateAnimation _rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);				_rotateAnimation.setDuration(3000);//				_rotateAnimation.setRepeatMode(Animation.REVERSE);				imgView.startAnimation(_rotateAnimation);				break;			case traslate:				/**				 * 依照图片的宽高2倍的位移移动				 */				TranslateAnimation _translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 2f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 2f);				_translateAnimation.setDuration(3000);				_translateAnimation.setFillAfter(true);				imgView.startAnimation(_translateAnimation);				break;			case scale:				/**				 * ScaleAnimation 以图片左上角为精巧点,依照1.5倍尺寸放大				 * params:				 * pivotXType 中心点x坐标类型 RELATIVE_TO_SELF相对于自己,RELATIVE_TO_PARENT相对于父view				 * pivotYType 同上				 * pivotXValue,pivotYValue(精巧点)				 */				ScaleAnimation _scaleAnimation = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f, Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_SELF, 0f);				_scaleAnimation.setDuration(300);				_scaleAnimation.setZAdjustment(Animation.ZORDER_TOP);				_scaleAnimation.setRepeatCount(1);				_scaleAnimation.setRepeatMode(Animation.REVERSE);//必须设置setRepeatCount此设置才生效,动画运行完毕之后依照逆方式动画返回				imgView.startAnimation(_scaleAnimation);				break;			case complex:				AnimationSet _animationSet = new AnimationSet(false);				AlphaAnimation _alphaAnimation2 = new AlphaAnimation(1.0f, 0.2f);				_alphaAnimation2.setDuration(1000);				_alphaAnimation2.setRepeatCount(1);				_alphaAnimation2.setRepeatMode(Animation.REVERSE);//				_alphaAnimation2.setFillAfter(true);//设此地方不好使,必须设置到AnimationSet中												TranslateAnimation _translateAnimation2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 2f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 2f);				_translateAnimation2.setDuration(1000);				_translateAnimation2.setRepeatCount(1);				_translateAnimation2.setRepeatMode(Animation.REVERSE);//				_translateAnimation2.setFillAfter(true);								_animationSet.addAnimation(_alphaAnimation2);				_animationSet.addAnimation(_translateAnimation2);				_animationSet.setFillAfter(true);//				_animationSet.setRepeatCount(1);//				_animationSet.setRepeatMode(Animation.REVERSE);//这两个属性设此地不好使,必须单个设置								imgView.startAnimation(_animationSet);				break;			default:				break;			}					}	}	@Override	public boolean onCreateOptionsMenu(Menu menu) {		// Inflate the menu; this adds items to the action bar if it is present.		getMenuInflater().inflate(R.menu.main, menu);		return true;	}}
activity_main.xml

以下为frame animation

public class FrameAnimationAcitvity extends Activity {	private ImageView imageView;	private AnimationDrawable ad;    @Override    protected void onCreate(Bundle savedInstanceState) {    	super.onCreate(savedInstanceState);    	setContentView(R.layout.animation_list);    	Button button=(Button)findViewById(R.id.button1);    	imageView=(ImageView)findViewById(R.id.imageView1);    	imageView.setBackgroundResource(R.drawable.framedrawable);    	ad=(AnimationDrawable)imageView.getBackground();    	button.setOnClickListener(new View.OnClickListener() {			@Override			public void onClick(View v) {                  	ad.start();						}		});    }}
framedrawable.xml

附上tween动画的源代码下载链接

转载地址:http://xkinl.baihongyu.com/

你可能感兴趣的文章
Sharepoint学习笔记—ECMAScript对象模型系列-- 11、 Enable/Disable Ribbon上的Button
查看>>
python类库26[sqlite]
查看>>
苹果与三星的专利纠纷
查看>>
boost库在工作(36)网络服务端之六
查看>>
关于java.lang.IllegalArgumentException: View not attached to window manager 错误的分析
查看>>
CSS——float属性备忘笔记
查看>>
利用pushState开发无刷页面切换(转)
查看>>
(翻译)理解Java当中的回调机制
查看>>
Discuz! X 插件开发手册
查看>>
Spring 注解@Component,@Service,@Controller,@Repository
查看>>
让PHP7达到最高性能的几个Tips(转)
查看>>
朗文在线词典的使用
查看>>
7-9-有向无环图拓扑排序-图-第7章-《数据结构》课本源码-严蔚敏吴伟民版
查看>>
求最短路径的三种算法: Ford, Dijkstra和Floyd
查看>>
(求助大牛)关于vs2010上的AVS代码bug问题~~
查看>>
JQuery上传插件Uploadify使用详解
查看>>
重构第26天 移除双重否定(Remove Double Negative)
查看>>
均值、方差、标准差及协方差、协方差矩阵详解
查看>>
oracle 清除当前用户的回收站
查看>>
有些事必须去做——写在离职之后创业之前
查看>>