Android动画控件之Animation
2015-09-20 20:06
585 查看
概述:
android的动画效果包括:移动,渐变透明度,旋转,缩放。实现动画的方式有两种:在java代码中动态实现,在xml中静态实现。
demo
动态实现:/* 动画的透明度渐变 */ AlphaAnimation alphaAnimation = new AlphaAnimation(1f,0);//透明度从1到0 alphaAnimation.setDuration(1000);//完成渐变的时间 alphaAnimation.setStartOffset(200);//响应时间 mImageViewAnim.startAnimation(alphaAnimation);//用一个ImageView加载animation,Image务必放入src或者加载过background /* 动画的移动 */ TranslateAnimation translateAnimation = //从一个坐标到另一个坐标的移动,参数依次为:起始点横、纵坐标,结束点横、纵坐标 new TranslateAnimation(-mImageViewAnim.getMeasuredWidth(),0,0,0); translateAnimation.setDuration(1000); translateAnimation.setStartOffset(200); mImageViewAnim.startAnimation(translateAnimation); /* 动画的旋转 */ RotateAnimation rotateAnimation = new RotateAnimation(0,360);//默认沿着左上角旋转 rotateAnimation.setDuration(1000); rotateAnimation.setStartOffset(200); mImageViewAnim.startAnimation(rotateAnimation); /* 动画的缩放 */ ScaleAnimation scaleAnimation = new ScaleAnimation(1f,2f,1f,2f);//水平尺寸变化,竖直尺寸变化 scaleAnimation.setDuration(1000); scaleAnimation.setStartOffset(200); mImageViewAnim.startAnimation(scaleAnimation);
动画的合并加载,需要一个AnimationSet,将所有的animation加载进去:
/** * 声明一个AnimationSet,是一个存储animation的集合 * false:被此set加载的每个animation用自己的interpolator; * true:所有animation功用一个interpolator */ AnimationSet animationSet = new AnimationSet(false); AlphaAnimation alphaAnimation = new AlphaAnimation(1f,0); TranslateAnimation translateAnimation = new TranslateAnimation(0,0,mImageViewAnim.getMeasuredWidth(),mImageViewAnim.getMeasuredHeight()); RotateAnimation rotateAnimation = new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF ,0.5f,RotateAnimation.RELATIVE_TO_SELF ,0.5f);//RELATIVE_TO_SELF的意思是位置相对于自己 ScaleAnimation scaleAnimation = new ScaleAnimation(1f,2f,1f,2f); alphaAnimation.setDuration(1000); translateAnimation.setDuration(1000); rotateAnimation.setDuration(1000); scaleAnimation.setDuration(1000); //添加各个动画 animationSet.addAnimation(alphaAnimation); animationSet.addAnimation(translateAnimation); animationSet.addAnimation(rotateAnimation); animationSet.addAnimation(scaleAnimation); mImageViewAnim.startAnimation(animationSet);
这样运行的效果就是所有动画组合咋一起的效果。
动画的静态加载,需要在res目录下新建一个文件夹anim,在里面新建一个资源文件,我的叫rotation.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/anticipate_interpolator"> <!-- cycle_interpolator:来回车轮效果; accelerate_decelerate_interpolator:先加速后减速 bounce_interpolator:这个针对平移,下落反复弹起效果 anticipate_interpolator:准备效果--> <alpha android:fromAlpha="1" android:toAlpha="0" android:duration="2000"> <!-- 从全显示到不显示,完成时间2000ms--> <!-- 从全显示到不显示,完成时间2000ms--> </alpha> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:startOffset="100" android:duration="2000" android:repeatCount="0"> <!--参数依次是:从0度到360度相对自己中心旋转, 100ms后开始转动,每次旋转持续2000ms,旋转(0+1)次--> </rotate> <scale android:fromXScale="1" android:toXScale="2" android:fromYScale="1" android:toYScale="2" android:pivotX="50%" android:pivotY="50%" android:duration="2000"> <!-- 水平从1倍大小变为两倍,竖直从1倍大小变为两倍 以自己的中心点为轴,完成动画需要2000ms--> </scale> <translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="0" android:toYDelta="300" android:duration="2000"> <!-- 初始水平位置:0 终止水平位置:0 初始竖直位置:0 终止竖直位置:300--> </translate> </set>
在java代码中调用这个资源:
AnimationUtils utils = new AnimationUtils(); Animation animation = utils.loadAnimation(getApplicationContext(),R.anim.rotation); mImageViewAnim.startAnimation(animation);
运行的效果是几种xml中几种动画的综合。
相关文章推荐
- Android开发中Eclispe相关问题及相应解决
- ubuntu14.04下android Sdk manager使用代理方法总结
- Android 学习第17课,使用文件的数据存储(4种存储模式)
- Android基础学习之Tab控件
- 我的Android设计模式(二) 观察者模式
- openssl for android使用
- Android自定义ViewGroup
- Android中的onActivityResult和setResult方法的使用
- [Android] 高效加载大图、多图解决方案,有效避免程序OOM
- android中dx、dp、dip、sp单位的区别
- Android 4.4(KitKat)中的设计模式-Graphics子系统
- android 上传/下载 图片
- Android中AIDL及其使用
- Android studio使用问题(持续更新)
- Android基础学习之GridView控件
- 探讨Android中的内置浏览器和Chrome
- 录制Android屏幕Gif的方法
- Android PopupWindow使用之地区、学校选择二级联动
- 使用CheckBox
- Android 图片特效处理:drawBitmapMesh——图像扭曲+动感