Animation是一个用于View,Surfaces和其它对象实现动画效果的抽象类,其中常用的类是TranslateAnimation用于控制位置的改变
一下列出一些重要的属性和方法
Xml属性
android:duration:运行动画的时间
android:interpolator:定义用于平滑动画运动的时间内插
android:repeatCount:定义动画重复的时间
方法:
set:RepeatCount(int ):定义动画重复的时间
setRepeatMode(int):通过设置重复时间定义动画的行为
setStartOffset(long):以毫秒为单位的动画运行前的延迟,一旦开始时间就达到了
Cancel():取消动画
hasStarted():判断动画是否已在运行
initialize(int width, int height, int parentWidth, int parentHeight):初始化动画
reset():重置动画
Start()启动动画
其中还有一些常量
RESTART:重新运行
INFINITE:永无终止地运行
一:基本动画实现(xml中定义和代码中定义)
渐变透明动画效果(alpha AlphaAnimation)
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http:schemas.android.com/apk/res/android" >
- <!-- 表示透明度从0.1到1.0,时长为3000ms。-->
- <alpha
- android:fromAlpha="0.1"
- android:toAlpha="1.0"
- android:duration="3000"
- />
- </set>
AlphaAnimation类对象构造方法
AlphaAnimation(float fromAlpha, float toAlpha)
setDuration(3000) 设置持续时间
第一个参数fromAlpha为 动画开始时候透明度
第二个参数toAlpha为 动画结束时候透明度
尺寸渐变动画效果(scale ScaleAnimation)
- <span style="font-size:13px;"><?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http:schemas.android.com/apk/res/android">
- <scale
- android:interpolator=
- "@android:anim/accelerate_decelerate_interpolator"
- android:fromXScale="0.0"
- android:toXScale="1.4"
- android:fromYScale="0.0"
- android:toYScale="1.4"
- android:pivotX="50%"
- android:pivotY="50%"
- android:fillAfter="false"
- android:duration="700" />
- </set></span>
ScaleAnimation类对象构造方法
ScaleAnimation(float fromX, float toX, float fromY, float toY,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
第一个参数fromX为动画起始时 X坐标上的伸缩尺寸
第二个参数toX为动画结束时 X坐标上的伸缩尺寸
第三个参数fromY为动画起始时Y坐标上的伸缩尺寸
第四个参数toY为动画结束时Y坐标上的伸缩尺寸
说明:
以上四种属性值
0.0表示收缩到没有
1.0表示正常无伸缩
值小于1.0表示收缩
值大于1.0表示放大
第五个参数pivotXType为动画在X轴相对于物件位置类型
第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
第七个参数pivotXType为动画在Y轴相对于物件位置类型
第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
画面移动动画效果(translate TranslateAnimation)
- <span style="font-size:13px;"><?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http:schemas.android.com/apk/res/android">
- <translate
- android:fromXDelta="30"
- android:toXDelta="-80"
- android:fromYDelta="30"
- android:toYDelta="300"
- android:duration="2000"
- /></span>
TranslateAnimation类对象构造
TranslateAnimation(float fromXDelta, float toXDelta,
float fromYDelta, float toYDelta)
第一个参数fromXDelta为动画起始时 X坐标上的移动位置
第二个参数toXDelta为动画结束时 X坐标上的移动位置
第三个参数fromYDelta为动画起始时Y坐标上的移动位置
第四个参数toYDelta为动画结束时Y坐标上的移动位置
画面旋转动画效果(rotate RotateAnamation)
- <span style="font-size:13px;"><?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http:schemas.android.com/apk/res/android">
- <rotate
- android:interpolator="@android:anim/accelerate_decelerate_interpolator"
- android:fromDegrees="0"
- android:toDegrees="+350"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="3000" /></span>
RotateAnimation(float fromDegrees, float toDegrees,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
第一个参数fromDegrees为动画起始时的旋转角度
第二个参数toDegrees为动画旋转到的角度
以下坐标参考为父类的(0,0)坐标,为最上角的位置
第三个参数pivotXType为动画在X轴相对于物件位置类型
第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
第五个参数pivotXType为动画在Y轴相对于物件位置类型
第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
二:如何使用java代码中的动画效果
使用从View父类继承过来的方法startAnimation()来为View或是子类View等添加一个动画效果
public void startAnimation (Animation animation)
三:如何使用XML中定义的动画
public static Animation loadAnimation (Contextcontext, int id)
//第一个参数Context为程序的上下文
//第二个参数id为动画XML文件的引用
//例子:
myAnimation=AnimationUtils.loadAnimation(this,R.anim.my_action);
//使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件
四:Activity界面切换动画实现方法
在startIntentActivity()后面加上OverriderPendingTransition(int enterAnim,int outAnim);
由于2个动画会同时执行,可设置后一个动画启动延时
五:逐帧动画(Frame By Frame)
Frame by frame 指将一幅幅图片按序播放,效果像gif动画:
在layout/myframeanimation.xml里定义动画。
- <span style="font-size:13px;"><?xml version="1.0" encoding="utf-8"?>
- <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="false">
- <item android:drawable="@drawable/rkof" android:duration="200" />
- <item android:drawable="@drawable/hkof" android:duration="200" />
- <item android:drawable="@drawable/gkof" android:duration="200" />
- <item android:drawable="@drawable/fkof" android:duration="200" />
- <item android:drawable="@drawable/ekof" android:duration="200" />
- <item android:drawable="@drawable/dkof" android:duration="200" />
- <item android:drawable="@drawable/ckof" android:duration="200" />
- <item android:drawable="@drawable/bkof" android:duration="200" />
- <item android:drawable="@drawable/akof" android:duration="200" />
- </animation-list></span>
<!-- android:oneshot属性为true,它将会在最后一帧停下来,如果设置
为false这个动画将循环播放 -->
定义AnimationDrawable对像
- <span style="font-size:13px;">ImageView myImage = (ImageView) findViewById(R.id.rocket_image);
- myImage.setBackgroundResource(R.anim.myframeanimation);
- AnimationDrawable frameAnimation=(AnimationDrawable) myImage.getBackground();
- </span>
- <span style="font-size:13px;"> </span>
六:动画监听事件,在动画结束时启动另一个动画
定义第一个动画并启动,设置动画监听事件,在动画结束时启动第二个动画
- <span style="font-size:13px;">Animation animation_1 = AnimationUtils.loadAnimation(
- ChangeActivity.this, R.anim.c_a_1);
- c_a_iv.startAnimation(animation_1);
- animation_1.setAnimationListener(new Animation.AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) {
- }
- @Override
- public void onAnimationRepeat(Animation animation) {
- }
- @Override
- public void onAnimationEnd(Animation animation) {
- c_a_iv.startAnimation(animation_2);
- }
- });</span>
七:重载Animation方法自定义动画
- public class CustomAnimation extends Animation{
- private int halfWidth;
- private int halfHeight;
- public void initialize(int width, int height, int parentWidth, int parentHeight){
- super.initialize(width, height, parentWidth, parentHeight);
- setDuration(1000);//动画持续时间
- setFillAfter(true);//动画结束时保持动画
- halfWidth = width/2;
- halfHeight = height/2;
- setInterpolator(new LinearInterpolator());//线性动画,这个的意思就是整个动画的速率是不变的,也就是线性的
- }
- protected void applyTransformation(float interpolatedTiem, Transformation t){
- final Matrix matrix = t.getMatrix();
- matrix.preScale(interpolatedTiem, interpolatedTiem);
- matrix.preRotate(interpolatedTiem*360);
- matrix.preTranslate(-halfWidth, 0);
- matrix.postTranslate(halfWidth, 0);
- }
- }
继承Animation类来自定义动画
实现它的applyTransformation方法,这个方法是一个回调方法,在动画进行的过程中,系统会一直不停的调用这个方法,每次调用,这个方法给我们传进来一个变换矩阵,通过对这个矩阵的操作,我们就可以实现自己的动画效果
matrix.preScale(interpolatedTime, interpolatedTime);
这个方法是进行画面缩放,传给它的参数第一个参数interpolatedTime 代表当前方法掉用时,动画进行的一个时间点,这个值的范围是0到1,也就是说动画刚开始的时候传进来的interpolatedTime为0,动画进行中的时候,传进来的是0到1之间的小数,动画结束的时候传进来的是1。
而preScale方法第二个参数接受的也是0到1,代表缩放的比例,0是最小,1是原始尺寸。
所以这个变换的意思就是,以当前动画进行的时间为参考,逐渐放大我们的可见对象。再说白了,就是从一个点,慢慢放大,最后恢复到原始尺寸,这样的效果。
matrix.preRotate(interpolatedTime * 360);
这个方法是旋转动画,根据动画的时间,将可见对象旋转一周。
这两个变换叠加再一起,就实现了一个这样的效果,我们的可见对象从画面正中央,旋转着逐渐放大,最终充满可见区域。
matrix.preTranslate(-halfWidth, -halfHeight);
设置动画的扩散点,进行伸缩或者旋转动画时以此点为中心向四周扩散。
matrix.postTranslate(halfWidth, halfHeight);
动画的扩散中心在主界面上的坐标。
转自 http://blog.youkuaiyun.com/bluecat_crystal/article/details/6728800