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. publicAlphaAnimation(floatfromAlpha,floattoAlpha){
  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. AlphaAnimationmAlphaAnimation=newAlphaAnimation(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. AlphaAnimationmAlphaAnimation=(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. publicclassRotateAnimationextendsAnimation{
  2. privatefloatmFromDegrees;//围绕旋转点旋转mFromDegrees角度为起始旋转状态
  3. privatefloatmToDegrees;//围绕旋转点旋转mToDegrees角度为终止旋转状态
  4. privateintmPivotXType=ABSOLUTE;//计算旋转点基于左上角在X坐标上的偏移量的方式
  5. privateintmPivotYType=ABSOLUTE;//计算旋转点基于左上角在Y坐标上的偏移量的方式
  6. privatefloatmPivotXValue=0.0f;//旋转点基于左上角在X坐标上的偏移量
  7. privatefloatmPivotYValue=0.0f;//旋转点基于左上角在Y坐标上的偏移量
  8. privatefloatmPivotX;//旋转点的X坐标
  9. privatefloatmPivotY;//旋转点的Y坐标
RotateAnimation类的几个构造函数定义如下: [java] view plain copy
  1. publicRotateAnimation(floatfromDegrees,floattoDegrees){
  2. mFromDegrees=fromDegrees;//围绕旋转点旋转mFromDegrees角度为起始旋转状态
  3. mToDegrees=toDegrees;//围绕旋转点旋转mToDegrees角度为终止旋转状态
  4. mPivotX=0.0f;//使用该构造函数默认旋转点的X坐标为左上角的X坐标
  5. mPivotY=0.0f;//使用该构造函数默认旋转点的Y坐标为左上角的Y坐标
  6. }
  7. publicRotateAnimation(floatfromDegrees,floattoDegrees,floatpivotX,floatpivotY){
  8. mFromDegrees=fromDegrees;//围绕旋转点旋转mFromDegrees角度为起始旋转状态
  9. mToDegrees=toDegrees;//围绕旋转点旋转mToDegrees角度为终止旋转状态
  10. mPivotXType=ABSOLUTE;
  11. mPivotYType=ABSOLUTE;
  12. //使用该构造函数,实质上就是设置旋转点基于左上角在X坐标上的偏移量为pivotX
  13. mPivotXValue=pivotX;
  14. //使用该构造函数,实质上就是设置旋转点基于左上角在Y坐标上的偏移量为pivotY
  15. mPivotYValue=pivotY;
  16. initializePivotPoint();
  17. /*privatevoidinitializePivotPoint(){
  18. if(mPivotXType==ABSOLUTE){mPivotX=mPivotXValue;}
  19. if(mPivotYType==ABSOLUTE){mPivotY=mPivotYValue;}
  20. }*/
  21. }
  22. publicRotateAnimation(floatfromDegrees,floattoDegrees,intpivotXType,floatpivotXValue,intpivotYType,floatpivotYValue){
  23. /*使用该构造函数,fromDegrees和toDegrees的参数同上边的构造函数,在pivotXType的值为
  24. Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF和Animation.RELATIVE_TO_PARENT
  25. 三种情况下,旋转点基于左上角在X坐标上的偏移量分别为:
  26. pivotXValue、pivotXValue*自身的宽度和pivotXValue*父控件的宽度,Y坐标亦然*/
  27. }
示例: [java] view plain copy
  1. RotateAnimationmRotateAnimation=newRotateAnimation(90,180,Animation.RELATIVE_TO_SELF,1,
  2. Animation.RELATIVE_TO_SELF,1);
  3. RotateAnimationmRotateAnimation=newRotateAnimation(180,360,10,30);
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. RotateAnimationmRotateAnimation=(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. publicclassScaleAnimationextendsAnimation{
  2. privatefloatmFromX;//基于伸缩参照点在X轴方向缩放mFromX倍为起始缩放状态
  3. privatefloatmToX;//基于伸缩参照点在X轴方向缩放mToX倍为结束缩放状态
  4. privatefloatmFromY;//基于伸缩参照点在Y轴方向缩放mFromY倍为起始缩放状态
  5. privatefloatmToY;//基于伸缩参照点在Y轴方向缩放mToY倍为结束缩放状态
  6. //以上四个属性的值,0.0表示缩放到没有,1.0表示正常无缩放,值小于1.0表示收缩,值大于1.0表示放大
  7. privateintmPivotXType=ABSOLUTE;//计算伸缩参照点基于左上角在X坐标上的偏移量的方式
  8. privateintmPivotYType=ABSOLUTE;//计算伸缩参照点基于左上角在Y坐标上的偏移量的方式
  9. privatefloatmPivotXValue=0.0f;//伸缩参照点基于左上角在X坐标上的偏移量
  10. privatefloatmPivotYValue=0.0f;//伸缩参照点基于左上角在Y坐标上的偏移量
  11. privatefloatmPivotX;//伸缩参照点的X坐标
  12. privatefloatmPivotY;//伸缩参照点的Y坐标
ScaleAnimation 类的构造函数的定义和RotateAnimation类似,只列举其一: [java] view plain copy
  1. publicScaleAnimation(floatfromX,floattoX,floatfromY,floattoY,
  2. intpivotXType,floatpivotXValue,intpivotYType,floatpivotYValue){
  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. ScaleAnimationmScaleAnimation=newScaleAnimation(1,2,2,1,
  2. Animation.RELATIVE_TO_SELF,2,Animation.RELATIVE_TO_SELF,2);
  3. ScaleAnimationmScaleAnimation=newScaleAnimation(1,2,2,1,);
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. ScaleAnimationmScaleAnimation=(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. publicclassTranslateAnimationextendsAnimation{
  2. privateintmFromXType=ABSOLUTE;//计算移动起始点基于左上角在X坐标上的偏移量的方式
  3. privateintmToXType=ABSOLUTE;//计算移动终止点基于左上角在X坐标上的偏移量的方式
  4. privateintmFromYType=ABSOLUTE;//计算移动起始点基于左上角在Y坐标上的偏移量的方式
  5. privateintmToYType=ABSOLUTE;//计算移动终止点基于左上角在Y坐标上的偏移量的方式
  6. privatefloatmFromXValue=0.0f;//基于移动起始点在X轴方向移动mFromXValue为起始移动状态
  7. privatefloatmToXValue=0.0f;//基于移动终止点在X轴方向移动mToXValue为终止移动状态
  8. privatefloatmFromYValue=0.0f;//基于移动起始点在Y轴方向移动mFromYValue为起始移动状态
  9. privatefloatmToYValue=0.0f;//基于移动终止点在Y轴方向移动mToYValue为终止移动状态
  10. }
TranslateAnimation类的构造函数的定义 RotateAnimation、 ScaleAnimation 类似,不再列举 示例: [java] view plain copy
  1. TranslateAnimationmTranslateAnimation=newTranslateAnimation(0,50,0,50);
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. TranslateAnimationmTranslateAnimation=(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. *RepresentsagroupofAnimationsthatshouldbeplayedtogether.Thetransformationofeachindividualanimationarecomposed
  3. *togetherintoasingletransform.IfAnimationSetsetsanypropertiesthatitschildrenalsoset(forexample,durationorfillBefore),
  4. *thevaluesofAnimationSetoverridethechildvalues.
  5. *代表一组动画,这些动画应该被一起执行,如果AnimationSet设置了一些他的children也设置的属性,
  6. *比如duration或fillBefore,那么,将会产生值被覆盖的操作
  7. *ThewaythatAnimationSetinheritsbehaviorfromAnimationisimportanttounderstand.SomeoftheAnimationattributesapplied
  8. *toAnimationSetaffecttheAnimationSetitself,somearepusheddowntothechildren,andsomeareignored,asfollows:
  9. *duration,repeatMode,fillBefore,fillAfter:Theseproperties,whensetonanAnimationSetobject,willbepusheddowntoallchild
  10. *animations.
  11. *一些动画属性应用到AnimationSet,将会覆盖children的值,
  12. *比如:duration,repeatMode,fillBefore,fillAfter
  13. *repeatCount,fillEnabled:ThesepropertiesareignoredforAnimationSet.
  14. *repeatCount,fillEnabled:这些值会被AnimationSet忽略
  15. *startOffset,shareInterpolator:ThesepropertiesapplytotheAnimationSetitself.
  16. *startOffset,shareInterpolator:这些值只会作用于AnimationSet本身
  17. */
  18. //以上是部分注释及简单翻译
  19. publicclassAnimationSetextendsAnimation{}
AnimationSet类的主要作用是实现各种动画效果的组合,和上边四种补间动画的使用一样,AnimationSet的使用步骤也分两步: 第一步,获取AnimationSet实例,方式也有两种: A、通过Java代码来创建 示例 [java] view plain copy
  1. mAnimationSet=newAnimationSet(true);
  2. mRotateAnimation=newRotateAnimation(90,180);
  3. mTranslateAnimation=newTranslateAnimation(0,20,0,20);
  4. mScaleAnimation=newScaleAnimation(1,2,2,1);
  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. AnimationSetmAnimationSet=(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代表此帧持续的时间,单位为毫秒。



更多相关文章

  1. android屏幕旋转在framework中的修改。
  2. Android中的动画
  3. Android(安卓)控件抖动效果
  4. Android中使用Animation实现控件的动画效果以及Interpolator和An
  5. Android强制设置横屏或竖屏
  6. Android(安卓)Animation --- 无限360度旋转
  7. android字体闪烁动画(线程)
  8. android字体闪烁动画(线程)
  9. Android(安卓)- Animation(一)

随机推荐

  1. Android(安卓)Studio 单元测试(instrument
  2. Android生命周期和启动模式
  3. 2012.06.28(4)——— android 应用移到sd
  4. Android串口通信:串口读写实例
  5. Android(安卓)渗透测试学习手册(二)准备实
  6. Android(安卓)控件系列: GridView 的简单
  7. LeAndroid招聘汇总
  8. Android利用已有控件实现自定义控件
  9. android:windowSoftInputMode属性使用
  10. Android(安卓)Activity和Intent机制 学习