Android - Animation(一)

来源:互联网 发布:telnet端口失败 编辑:程序博客网 时间:2024/05/17 04:29

Android主要有三种动画:补间动画View Animation/Tween Animation)、帧动画Drawable Animation/Frame Animation)和属性动画(Property Animation,android3.0引入)



上篇   补间动画和帧动画的简单实现


补间动画View Animation/Tween Animation)

View Animation(Tween Animation):补间动画,主要完成对一个View对象进行移动、缩放、旋转和透明度渐变来达到动画的效果,使用补间动画主要涉及到的类及相互间的继承关系如下:



Animation的四个子类分别对应补间动画的移动、缩放、旋转和透明度渐变操作。下面分别对补间动画的四种效果做详细说明:

一、AlphaAnimation-透明度渐变的补间动画实现步骤为:
第一步,获取AlphaAnimation实例,而获取AlphaAnimation示例的方式有两种:
A、通过Java代码new的方式
AlphaAnimation类的构造函数定义如下:
[java] view plain copy
  1. public AlphaAnimation(float fromAlpha, float toAlpha) {  
  2.     mFromAlpha = fromAlpha;  
  3.     //起始透明度,值在0.0~1.0之间,0.0表示完全透明,1.0表示完全不透明  
  4.     mToAlpha = toAlpha;  
  5.     //结束透明度,值在0.0~1.0之间,0.0表示完全透明,1.0表示完全不透明  
  6. }  
示例:
[java] view plain copy
  1. AlphaAnimation mAlphaAnimation = new AlphaAnimation(1.0f, 0.1f);      
B、先在res\anim目录下的xml文件中用<alpha>标签来定义,然后再用AnimationUtils类的loadAnimation方法来获取
示例:
[java] view plain copy
  1. <alpha  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromAlpha="1.0"  
  4.     android:toAlpha="0.0">  
  5. </alpha>    
  6.       
  7. AlphaAnimation  mAlphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(this, R.anim.alphanim);   
第二步,获取了AlphaAnimation实例之后,将需要动画效果的view对象和AlphaAnimation实例进行绑定,可选方式也有两种:
A、
[java] view plain copy
  1. myView.startAnimation(mAlphaAnimation);  
B、
[java] view plain copy
  1. myView.setAnimation(mAlphaAnimation);  
  2. mAlphaAnimation.start();  

二、RotateAnimation-旋转补间动画实现步骤为:
第一步,获取RotateAnimation实例,而获取RotateAnimation示例的方式有两种:
A、通过Java代码new的方式
RotateAnimation类有几个重要的成员变量:
[java] view plain copy
  1. public class RotateAnimation extends Animation {  
  2.     private float mFromDegrees;  //围绕旋转点旋转mFromDegrees角度为起始旋转状态  
  3.     private float mToDegrees;  //围绕旋转点旋转mToDegrees角度为终止旋转状态  
  4.     private int mPivotXType = ABSOLUTE;  //计算旋转点基于左上角在X坐标上的偏移量的方式  
  5.     private int mPivotYType = ABSOLUTE;  //计算旋转点基于左上角在Y坐标上的偏移量的方式  
  6.     private float mPivotXValue = 0.0f;  //旋转点基于左上角在X坐标上的偏移量  
  7.     private float mPivotYValue = 0.0f;  //旋转点基于左上角在Y坐标上的偏移量  
  8.     private float mPivotX;  //旋转点的X坐标  
  9.     private float mPivotY;  //旋转点的Y坐标     
RotateAnimation类的几个构造函数定义如下:
[java] view plain copy
  1. public RotateAnimation(float fromDegrees, float toDegrees) {  
  2.     mFromDegrees = fromDegrees;   //围绕旋转点旋转mFromDegrees角度为起始旋转状态  
  3.     mToDegrees = toDegrees;  //围绕旋转点旋转mToDegrees角度为终止旋转状态  
  4.     mPivotX = 0.0f;  //使用该构造函数默认旋转点的X坐标为左上角的X坐标  
  5.     mPivotY = 0.0f;  //使用该构造函数默认旋转点的Y坐标为左上角的Y坐标  
  6. }     
  7.       
  8. public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) {  
  9.     mFromDegrees = fromDegrees;  //围绕旋转点旋转mFromDegrees角度为起始旋转状态  
  10.     mToDegrees = toDegrees;  //围绕旋转点旋转mToDegrees角度为终止旋转状态  
  11.     mPivotXType = ABSOLUTE;    
  12.     mPivotYType = ABSOLUTE;  
  13.     //使用该构造函数,实质上就是设置旋转点基于左上角在X坐标上的偏移量为pivotX  
  14.     mPivotXValue = pivotX;   
  15.     //使用该构造函数,实质上就是设置旋转点基于左上角在Y坐标上的偏移量为pivotY  
  16.     mPivotYValue = pivotY;    
  17.     initializePivotPoint();  
  18.     /*private void initializePivotPoint() { 
  19.         if (mPivotXType == ABSOLUTE) {  mPivotX = mPivotXValue; } 
  20.         if (mPivotYType == ABSOLUTE) {  mPivotY = mPivotYValue; } 
  21.     }*/     
  22. }     
  23.       
  24. public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,int pivotYType, float pivotYValue) {   
  25.     /*使用该构造函数,fromDegrees和toDegrees的参数同上边的构造函数,在pivotXType的值为 
  26.     Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF和Animation.RELATIVE_TO_PARENT 
  27.     三种情况下,旋转点基于左上角  在X坐标上的偏移量分别为: 
  28.     pivotXValue、pivotXValue*自身的宽度和pivotXValue*父控件的宽度,Y坐标亦然*/  
  29. }  
示例:
[java] view plain copy
  1. RotateAnimation  mRotateAnimation = new RotateAnimation(90180, Animation.RELATIVE_TO_SELF, 1,  
  2.         Animation.RELATIVE_TO_SELF, 1);  
  3. RotateAnimation  mRotateAnimation = new RotateAnimation(1803601030);  
B、先在res\anim目录下的xml文件中用<rotate>标签来定义,然后再用AnimationUtils类的loadAnimation方法来获取
示例:
[java] view plain copy
  1. <rotate  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromDegrees="180"  
  4.     android:toDegrees="360">  
  5. </rotate>   
  6.       
  7. RotateAnimation  mRotateAnimation  = (RotateAnimation  ) AnimationUtils.loadAnimation(this, R.anim.alphanim);     
第二步,获取了RotateAnimation  实例之后,将需要动画效果的view对象和RotateAnimation  实例进行绑定,可选方式也有两种:
A、
[java] view plain copy
  1. myView.startAnimation(mRotateAnimation  );  
B、
[java] view plain copy
  1. myView.setAnimation(mRotateAnimation  );  
  2. mRotateAnimation  .start();   


三、ScaleAnimation-缩放补间动画实现步骤为:
第一步,获取ScaleAnimation实例,而获取ScaleAnimation示例的方式也有两种:
A、通过Java代码new的方式
和RotateAnimation类似,ScaleAnimation类也有几个重要的成员变量: 
[java] view plain copy
  1. public class ScaleAnimation extends Animation {  
  2.     private float mFromX;  //基于伸缩参照点在X轴方向缩放mFromX倍为起始缩放状态  
  3.     private float mToX;  //基于伸缩参照点在X轴方向缩放mToX倍为结束缩放状态  
  4.     private float mFromY;  //基于伸缩参照点在Y轴方向缩放mFromY倍为起始缩放状态  
  5.     private float mToY;  //基于伸缩参照点在Y轴方向缩放mToY倍为结束缩放状态  
  6.     //以上四个属性的值,0.0表示缩放到没有,1.0表示正常无缩放,值小于1.0表示收缩,值大于1.0表示放大  
  7.     private int mPivotXType = ABSOLUTE;  //计算伸缩参照点基于左上角在X坐标上的偏移量的方式  
  8.     private int mPivotYType = ABSOLUTE;  //计算伸缩参照点基于左上角在Y坐标上的偏移量的方式  
  9.     private float mPivotXValue = 0.0f;  //伸缩参照点基于左上角在X坐标上的偏移量  
  10.     private float mPivotYValue = 0.0f;  //伸缩参照点基于左上角在Y坐标上的偏移量  
  11.     private float mPivotX;  //伸缩参照点的X坐标  
  12.     private float mPivotY;  //伸缩参照点的Y坐标  
ScaleAnimation类的构造函数的定义和RotateAnimation类似,只列举其一:
[java] view plain copy
  1. public ScaleAnimation(float fromX, float toX, float fromY, float toY,  
  2.         int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {  
  3.     /*使用该构造函数,fromX、toX、fromY和toY的参数同其他的构造函数,在pivotXType的值为 
  4.     Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF和Animation.RELATIVE_TO_PARENT 
  5.     三种情况下,伸缩参照点基于左  上角在X坐标上的偏移量分别为: 
  6.     pivotXValue、pivotXValue*自身的宽度和pivotXValue*父控件的宽度,Y坐标亦然*/  
  7. }  
示例:
[java] view plain copy
  1. ScaleAnimation  mScaleAnimation = new ScaleAnimation(1221,  
  2.                                                                Animation.RELATIVE_TO_SELF,2,Animation.RELATIVE_TO_SELF,2);  
  3. ScaleAnimation  mScaleAnimation = new ScaleAnimation(1221,);  
B、先在res\anim目录下的xml文件中用<scale>标签来定义,然后再用AnimationUtils类的loadAnimation方法来获取
示例:
[java] view plain copy
  1. <scale  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromXScale="1"  
  4.     android:toXScale="2"  
  5.     android:fromYScale="2"  
  6.     android:toYScale="1"  
  7.     android:pivotX="30"  
  8.     android:pivotY="30">  
  9. </scale>    
  10.       
  11. ScaleAnimation mScaleAnimation = (ScaleAnimation) AnimationUtils.loadAnimation(this, R.anim.alphanim);  
第二步,获取了ScaleAnimation实例之后,将需要动画效果的view对象和ScaleAnimation实例进行绑定,可选方式也有两种:
A、
[java] view plain copy
  1. myView.startAnimation(mScaleAnimation );  
B、
[java] view plain copy
  1. myView.setAnimation(mScaleAnimation );  
  2. mScaleAnimation .start();  

四、TranslateAnimation-移动补间动画实现步骤为:
第一步,获取TranslateAnimation实例,而获取TranslateAnimation示例的方式也有两种:
A、通过Java代码new的方式
和RotateAnimation、ScaleAnimation类似,TranslateAnimation类重要的成员变量及其初始值如下:
[java] view plain copy
  1. public class TranslateAnimation extends Animation {  
  2.     private int mFromXType = ABSOLUTE;   //计算移动起始点基于左上角在X坐标上的偏移量的方式  
  3.     private int mToXType = ABSOLUTE;    //计算移动终止点基于左上角在X坐标上的偏移量的方式  
  4.     private int mFromYType = ABSOLUTE;   //计算移动起始点基于左上角在Y坐标上的偏移量的方式    
  5.     private int mToYType = ABSOLUTE;    //计算移动终止点基于左上角在Y坐标上的偏移量的方式  
  6.     private float mFromXValue = 0.0f;   //基于移动起始点在X轴方向移动mFromXValue为起始移动状态  
  7.     private float mToXValue = 0.0f;   //基于移动终止点在X轴方向移动mToXValue 为终止移动状态  
  8.     private float mFromYValue = 0.0f;   //基于移动起始点在Y轴方向移动mFromYValue 为起始移动状态  
  9.     private float mToYValue = 0.0f;  //基于移动终止点在Y轴方向移动mToYValue 为终止移动状态  
  10. }  
TranslateAnimation类的构造函数的定义RotateAnimation、ScaleAnimation类似,不再列举
示例:
[java] view plain copy
  1. TranslateAnimation mTranslateAnimation = new TranslateAnimation(050050);  
B、先在res\anim目录下的xml文件中用<translate>标签来定义,然后再用AnimationUtils类的loadAnimation方法来获取
示例:
[java] view plain copy
  1. <translate  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromXDelta="0"  
  4.     android:toXDelta="50"  
  5.     android:fromYDelta="0"  
  6.     android:toYDelta="50">  
  7. </translate>  
  8.   
  9. TranslateAnimation mTranslateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.alphanim);  
第二步,获取了TranslateAnimation实例之后,将需要动画效果的view对象和TranslateAnimation实例进行绑定,可选方式也有两种:
A、
[java] view plain copy
  1. myView.startAnimation(mTranslateAnimation );  
B、
[java] view plain copy
  1. myView.setAnimation(mTranslateAnimation );  
  2. mTranslateAnimation .start();  

以上是四种补间动画的简单实现,下边为这四种动画的一些共同属性:

Ⅰ、在xml中定义一个补间动画时,上述四种动画拥有的共同属性为:
A、android:interpolator="" 用于改变动画速度的插值器,下面是几种常见的插值器(下表来于网络):
B、android:duration=""设置动画的持续时间
C、android:fillEnabled=""
D、android:fillBefore=" " 可选值为true和false
E、android:fillAfter=" " 可选值为true和false
F、android:repeatCount=" " infinite   定义动画的重复次数 设置为-1则会无限重复
G、android:repeatMode=" " 定义动画的重复行为  可选值为restart和reverse  
H、android:startOffset=" "  设置动画开始之前的等待时间
上述属性中,C、D、E需要进一步验证

Ⅱ、如果是在Java代码中定义补间动画,在创建相应的Animation对象之后,可以调用setInterpolator(Interpolator)、setDuration(long)、setFillEnabled(boolean)、setFillBefore(boolean)、setFillAfter(boolean)、setRepeatCount(int)、setRepeatMode(int)、getStartOffset()方法设置和xml文件中对应的属性。


AnimationSet的使用

在文章的开头,使用补间动画主要涉及到的类及相互间的继承图中,还有一个类需要介绍,它就是Animation的直接子类AnimationSet
来看AnimationSet类的定义:
[java] view plain copy
  1. /** 
  2.  * Represents a group of Animations that should be played together.The transformation of each individual animation are composed  
  3.  * together into a single transform. If AnimationSet sets any properties that its children also set(for example, duration or fillBefore),  
  4.  * the values of AnimationSet override the child values. 
  5.  * 代表一组动画,这些动画应该被一起执行,如果AnimationSet 设置了一些他的children 也设置的属性, 
  6.  * 比如duration 或 fillBefore,那么,将会产生值被覆盖的操作 
  7.  * The way that AnimationSet inherits behavior from Animation is important to understand. Some of the Animation attributes applied 
  8.  * to AnimationSet affect the AnimationSet itself, some are pushed down to the children, and some are ignored,as follows: 
  9.  * duration, repeatMode, fillBefore, fillAfter: These properties, when set on an AnimationSet object, will be pushed down to all  child  
  10.  * animations. 
  11.  * 一些动画属性应用到AnimationSet,将会覆盖children的值, 
  12.  * 比如:duration, repeatMode, fillBefore, fillAfter 
  13.  * repeatCount, fillEnabled: These properties are ignored for AnimationSet. 
  14.  * repeatCount, fillEnabled:这些值会被AnimationSet忽略 
  15.  * startOffset, shareInterpolator: These properties apply to the AnimationSet itself. 
  16.  * startOffset, shareInterpolator:这些值只会作用于AnimationSet 本身 
  17.  */  
  18.  //以上是部分注释及简单翻译  
  19. public class AnimationSet extends Animation { }  
AnimationSet类的主要作用是实现各种动画效果的组合,和上边四种补间动画的使用一样,AnimationSet的使用步骤也分两步:
第一步,获取AnimationSet实例,方式也有两种:
A、通过Java代码来创建
示例
[java] view plain copy
  1. mAnimationSet = new AnimationSet(true);  
  2. mRotateAnimation = new RotateAnimation(90180);  
  3. mTranslateAnimation = new TranslateAnimation(020020);  
  4. mScaleAnimation = new ScaleAnimation(1221);  
  5. mAnimationSet.addAnimation(mRotateAnimation);  
  6. mAnimationSet.addAnimation(mScaleAnimation);  
  7. mAnimationSet.addAnimation(mTranslateAnimation);  
B、先在res\anim目录下的xml文件中用<set>标签来定义,再用AnimationUtils类的loadAnimation方法获取
示例
[java] view plain copy
  1. <set  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:interpolator="@android:anim/accelerate_decelerate_interpolator">  
  4.     <alpha  
  5.         android:fromAlpha="0.1"  
  6.         android:toAlpha="1.0">  
  7.     </alpha>  
  8.     <rotate  
  9.         android:fromDegrees="90"  
  10.         android:toDegrees="180">  
  11.     </rotate>  
  12.     <scale  
  13.         android:fromXScale="1"  
  14.         android:toXScale="2"  
  15.         android:fromYScale="2"  
  16.         android:toYScale="1">  
  17.     </scale>  
  18. </set>  
[java] view plain copy
  1. AnimationSet  mAnimationSet = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.alphanim);  
第二步,获取AnimationSet实例之后,将需要动画效果的view对象和AnimationSet实例进行绑定,可选方式也有两种:
A、
[java] view plain copy
  1. myView.startAnimation(mAnimationSet );  
B、
[java] view plain copy
  1. myView.setAnimation(mAnimationSet );  
  2. mAnimationSet .start();  

这里需要提的是通过xml文件定义AnimationSet时的属性android:shareInterpolator=" " 可选值为true和false,如果设为true,则代表这个AnimationSet下的所有动画共享一个插值器,反之,不共享,需要每个动画自己定义插值器。



帧动画Drawable Animation/Frame Animation)

帧动画就是将一系列图片按照顺序轮流展示以达到动画的效果,以下是使用帧动画涉及到的类及其继承关系图:

AnimationDrawable类的定义:

简单翻译:
AnimationDrawable用来创建 可通过一系列的Drawable对象来定义的frame-by-frame animations,创建一个frame-by-frame动画最简单的方式是在XML中的res/drawable/文件夹下定义,然后,将它设置为一个视图对象的背景,再调用start()运行它。定义在XML文件中的一个AnimationDrawable由一个单一的<animation-list>标签和一系列嵌套<item>标签组成,每个<item>标签定义了一个帧动画。看下面的例子
 ... ...
之下是加载和播放动画的代码
 ... ...
注释比较详细,不上示例了

需要注意的是:

<animation-list>元素是必须的,并且必须要作为根元素,可以包含一或多个<item>元素;属性android:onshot的值如果定义为true的话,此动画只会执行一次,如果为false则一直循环。

<item>元素代表一帧动画,android:drawable指定此帧动画所对应的图片资源,android:druation代表此帧持续的时间,单位为毫秒。



0 0
原创粉丝点击