转载于:http://www.2cto.com/kf/201412/358203.html


1.Android的animation由四种类型组成

   alpha(透明度)、scale(缩放)、translate(位移)、rotate(旋转)


2.XML配置文件中

android:alpha 渐变透明度动画效果
android:scale 渐变缩放动画效果
android:translate 画面转换位置移动动画效果
android:rotate 画面转移旋转动画效果

3.Java Code代码中 

AlphaAnimation 渐变透明度动画效果
ScaleAnimation 渐变缩放动画效果
TranslateAnimation 画面转换位置移动动画效果
RotateAnimation 画面转移旋转动画效果

Alpha

  • xml配置:

"http://schemas.android.com/apk/res/android">   

  • Java代码:

AlphaAnimation(float fromAlpha, float toAlpha)

//第一个参数fromAlpha为 动画开始时候透明度

//第二个参数toAlpha为 动画结束时候透明度




?

Scale

  • xml配置:

"http://schemas.android.com/apk/res/android">

   

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">



  • Java代码:

ScaleAnimation(floatfromX, floattoX, floatfromY, floattoY,            intpivotXType, floatpivotXValue, intpivotYType, floatpivotYValue)

//第一个参数fromX为动画起始时 X坐标上的伸缩尺寸   

//第二个参数toX为动画结束时 X坐标上的伸缩尺寸    

//第三个参数fromY为动画起始时Y坐标上的伸缩尺寸   

//第四个参数toY为动画结束时Y坐标上的伸缩尺寸 

/*说明:

                    以上四种属性值   

                    0.0表示收缩到没有

                    1.0表示正常无伸缩    

                    值小于1.0表示收缩 

                    值大于1.0表示放大

*/

//第五个参数pivotXType为动画在X轴相对于物件位置类型 

//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置

//第七个参数pivotXType为动画在Y轴相对于物件位置类型  

//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置

myAnimation_Scale = newScaleAnimation(0.0f,1.4f,0.0f,1.4f,              Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF, 0.5f);


Translate

  • xml配置:
"http://schemas.android.com/apk/res/android" >
android:fromxdelta= "30" android:toxdelta= "-80" android:fromydelta= "30" android:toydelta= "300" android:duration= "2000" >

  • Java代码:
TranslateAnimation(floatfromXDelta, floattoXDelta,                        floatfromYDelta, floattoYDelta)
//第一个参数fromXDelta为动画起始时 X坐标上的位置    //第二个参数toXDelta为动画结束时 X坐标上的位置      //第三个参数fromYDelta为动画起始时Y坐标上的位置     //第四个参数toYDelta为动画结束时Y坐标上的位置
myAnimation_Translate = newTranslateAnimation(10f, 100f, 10f, 100f);



?

Rorate

  • xml配置:
"http://schemas.android.com/apk/res/android">
android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromdegrees="0" android:todegrees="+350" android:pivotx="50%" android:pivoty="50%" android:duration="3000"


  • Java代码:
RotateAnimation(floatfromDegrees, floattoDegrees,             intpivotXType, floatpivotXValue, intpivotYType, floatpivotYValue)
//第一个参数fromDegrees为动画起始时的旋转角度    //第二个参数toDegrees为动画旋转到的角度   //第三个参数pivotXType为动画在X轴相对于物件位置类型  //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置 //第五个参数pivotXType为动画在Y轴相对于物件位置类型   //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
myAnimation_Rotate = newRotateAnimation(0.0f, +350.0f,                Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
? Android有三种动画模式 :Tween Animation(补间动画)、Frame Animation(逐帧动画)、Property Animation(属性动画)

1. View Animation(Tween Animation)

View Animation(Tween Animation):补间动画,给出两个关键帧,通过一些算法将给定属性值在给定的时间内在两个关键帧间渐变。

View animation只能应用于View对象,而且只支持一部分属性,如支持缩放旋转而不支持背景颜色的改变。

而且对于View animation,它只是改变了View对象绘制的位置,而没有改变View对象本身,比如,你有一个Button,坐标(100,100),Width:200,Height:50,而你有一个动画使其变为Width:100,Height:100,你会发现动画过程中触发按钮点击的区域仍是(100,100)-(300,150)。

View Animation就是一系列View形状的变换,如大小的缩放,透明度的改变,位置的改变,动画的定义既可以用代码定义也可以用XML定义,当然,建议用XML定义。

可以给一个View同时设置多个动画,比如从透明至不透明的淡入效果,与从小到大的放大效果,这些动画可以同时进行,也可以在一个完成之后开始另一个。

用XML定义的动画放在/res/anim/文件夹内,XML文件的根元素可以为,,,,interpolator元素或(表示以上几个动画的集合,set可以嵌套)。默认情况下,所有动画是同时进行的,可以通过startOffset属性设置各个动画的开始偏移(开始时间)来达到动画顺序播放的效果。

可以通过设置interpolator属性改变动画渐变的方式,如AccelerateInterpolator,开始时慢,然后逐渐加快。默认为AccelerateDecelerateInterpolator。

定义好动画的XML文件后,可以通过类似下面的代码对指定View应用动画。

ImageView spaceshipImage = (ImageView)findViewById(R.id.spaceshipImage); Animation hyperspaceJumpAnimation=AnimationUtils.loadAnimation( this , R.anim.hyperspace_jump; spaceshipImage.startAnimation(hyperspaceJumpAnimation);


2. Drawable Animation(Frame Animation)

Drawable Animation(Frame Animation):帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果。在XML中的定义方式如下:

  "@drawable/rocket_thrust1" android:duration= "200" >      "@drawable/rocket_thrust2" android:duration= "200" >      "@drawable/rocket_thrust3" android:duration= "200" >
java代码: protected void onCreate(Bundle savedInstanceState) {          // TODO Auto-generated method stub          super .onCreate(savedInstanceState);          setContentView(R.layout.main);          imageView = (ImageView) findViewById(R.id.imageView1);          imageView.setBackgroundResource(R.drawable.drawable_anim);          anim = (AnimationDrawable) imageView.getBackground();      }        public boolean onTouchEvent(MotionEvent event) {          if (event.getAction() == MotionEvent.ACTION_DOWN) {              anim.stop();              anim.start();              return true ;          }          return super .onTouchEvent(event);      }

? ? 注意:
  • 要在代码中调用Imageview的setBackgroundResource方法,如果直接在XML布局文件中设置其src属性当触发动画时会FC。
  • 在动画start()之前要先stop(),不然在第一次动画之后会停在最后一帧,这样动画就只会触发一次。
  • 最后一点是SDK中提到的,不要在onCreate中调用start,因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start()。

    3. Property Animation

    属性动画,这个是在Android 3.0中才引进的,它更改的是对象的实际属性,在View Animation(Tween Animation)中,其改变的是View的绘制效果,真正的View的属性保持不变,比如无论你在对话中如何缩放Button的大小,Button的有效点击区域还是没有应用动画时的区域,其位置与大小都不变。而在Property Animation中,改变的是对象的实际属性,如Button的缩放,Button的位置与大小属性值都改变了。而且Property Animation不止可以应用于View,还可以应用于任何对象。Property Animation只是表示一个值在一段时间内的改变,当值改变时要做什么事情完全是你自己决定的。

    在Property Animation中,可以对动画应用以下属性:

      • Duration:动画的持续时间
      • TimeInterpolation:属性值的计算方式,如先快后慢
      • TypeEvaluator:根据属性的开始、结束值与TimeInterpolation计算出的因子计算出当前时间的属性值
      • Repeat Count and behavoir:重复次数与方式,如播放3次、5次、无限循环,可以此动画一直重复,或播放完时再反向播放
      • Animation sets:动画集合,即可以同时对一个对象应用几个动画,这些动画可以同时播放也可以对不同动画设置不同开始偏移
      • Frame refreash delay:多少时间刷新一次,即每隔多少时间计算一次属性值,默认为10ms,最终刷新时间还受系统进程调度与硬件的影响


        Interpolator


        首先要了解为什么需要插值器,因为在补间动画中,我们一般只定义关键帧(首帧或尾帧),然后由系统自动生成中间帧,生成中间帧的这个过程可以成为“插值”。插值器定义了动画变化的速率,提供不同的函数定义变化值相对于时间的变化规则,可以定义各种各样的非线性变化函数,比如加速、减速等。下面是几种常见的插值器:

        Interpolator对象 资源ID 功能作用
        AccelerateDecelerateInterpolator @android:anim/accelerate_decelerate_interpolator 先加速再减速
        AccelerateInterpolator @android:anim/accelerate_interpolator 加速
        AnticipateInterpolator @android:anim/anticipate_interpolator 先回退一小步然后加速前进
        AnticipateOvershootInterpolator @android:anim/anticipate_overshoot_interpolator 在上一个基础上超出终点一小步再回到终点
        BounceInterpolator @android:anim/bounce_interpolator 最后阶段弹球效果
        CycleInterpolator @android:anim/cycle_interpolator 周期运动
        DecelerateInterpolator @android:anim/decelerate_interpolator 减速
        LinearInterpolator @android:anim/linear_interpolator 匀速
        OvershootInterpolator @android:anim/overshoot_interpolator 快速到达终点并超出一小步最后回到终点

        如果只简单地引用这些插值器还不能满足需要的话,我们要考虑一下个性化插值器。我们可以创建一个插值器资源修改插值器的属性,比如修改AnticipateInterpolator的加速速率,调整CycleInterpolator的循环次数等。为了完成这种需求,我们需要创建XML资源文件,然后将其放于/res/anim下,然后再动画元素中引用即可。我们先来看一下几种常见的插值器可调整的属性:

        ?
      •   android:tension 浮点值,起始点后退的张力、拉力数,默认为2  android:tension 同上 android:extraTension 浮点值,拉力的倍数,默认为1.52 * 1.5 android:cycles 整数值,循环的个数,默认为1 android:factor 浮点值,减速的速率,默认为1 浮点值,超出终点后的张力、拉力,默认为2


      • 下面我们就拿最后一个插值器来举例:

        ?
        1 2 "http://schemas.android.com/apk/res/android" android:tension= "7.0" >

        上面的代码中,我们把张力改为7.0,然后将此文件命名为my_overshoot_interpolator.xml,放置于/res/anim下,我们就可以引用到自定义的插值器了: 
        ?
        1 "http://schemas.android.com/apk/res/android" android:interpolator= "@anim/my_overshoot_interpolator" ...= "" >


        AccelerateDecelerateInterpolator

        ?
        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /**   * An interpolator where the rate of change starts and ends slowly but   * accelerates through the middle.   *   */ public class AccelerateDecelerateInterpolator implements Interpolator {      public AccelerateDecelerateInterpolator() {      }            @SuppressWarnings ({ "UnusedDeclaration" })      public AccelerateDecelerateInterpolator(Context context, AttributeSet attrs) {      }            public float getInterpolation( float input) {          return ( float )(Math.cos((input + 1 ) * Math.PI) / 2 .0f) + 0 .5f;      } }


        Android 动画(anim)详解_第1张图片

        <喎�"/kf/ware/vc/" target="_blank" class="keylink">vcD4KPGgyPgpBY2NlbGVyYXRlSW50ZXJwb2xhdG9yPC9oMj4KPHByZSBjbGFzcz0="brush:java;">/** * An interpolator where the rate of change starts out slowly and * and then accelerates. * */ public class AccelerateInterpolator implements Interpolator { private final float mFactor; private final double mDoubleFactor; public AccelerateInterpolator() { mFactor = 1.0f; mDoubleFactor = 2.0; } /** * Constructor * * @param factor Degree to which the animation should be eased. Seting * factor to 1.0f produces a y=x^2 parabola. Increasing factor above * 1.0f exaggerates the ease-in effect (i.e., it starts even * slower and ends evens faster) */ public AccelerateInterpolator(float factor) { mFactor = factor; mDoubleFactor = 2 * mFactor; } public AccelerateInterpolator(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.AccelerateInterpolator); mFactor = a.getFloat(com.android.internal.R.styleable.AccelerateInterpolator_factor, 1.0f); mDoubleFactor = 2 * mFactor; a.recycle(); } public float getInterpolation(float input) { if (mFactor == 1.0f) { return input * input; } else { return (float)Math.pow(input, mDoubleFactor); } } }

        Android 动画(anim)详解_第2张图片

        AnticipateInterpolator

        ?
        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 /**   * An interpolator where the change starts backward then flings forward.   */ public class AnticipateInterpolator implements Interpolator {      private final float mTension;        public AnticipateInterpolator() {          mTension = 2 .0f;      }        /**       * @param tension Amount of anticipation. When tension equals 0.0f, there is       *                no anticipation and the interpolator becomes a simple       *                acceleration interpolator.       */      public AnticipateInterpolator( float tension) {          mTension = tension;      }        public AnticipateInterpolator(Context context, AttributeSet attrs) {          TypedArray a = context.obtainStyledAttributes(attrs,                  com.android.internal.R.styleable.AnticipateInterpolator);            mTension =                  a.getFloat(com.android.internal.R.styleable.AnticipateInterpolator_tension, 2 .0f);            a.recycle();      }        public float getInterpolation( float t) {          // a(t) = t * t * ((tension + 1) * t - tension)          return t * t * ((mTension + 1 ) * t - mTension);      } }


        Android 动画(anim)详解_第3张图片

        AnticipateOvershootInterpolator

        ?
        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 /**   * An interpolator where the change starts backward then flings forward and overshoots   * the target value and finally goes back to the final value.   */ public class AnticipateOvershootInterpolator implements Interpolator {      private final float mTension;        public AnticipateOvershootInterpolator() {          mTension = 2 .0f * 1 .5f;      }        /**       * @param tension Amount of anticipation/overshoot. When tension equals 0.0f,       *                there is no anticipation/overshoot and the interpolator becomes       *                a simple acceleration/deceleration interpolator.       */      public AnticipateOvershootInterpolator( float tension) {          mTension = tension * 1 .5f;      }        /**       * @param tension Amount of anticipation/overshoot. When tension equals 0.0f,       *                there is no anticipation/overshoot and the interpolator becomes       *                a simple acceleration/deceleration interpolator.       * @param extraTension Amount by which to multiply the tension. For instance,       *                     to get the same overshoot as an OvershootInterpolator with       *                     a tension of 2.0f, you would use an extraTension of 1.5f.       */      public AnticipateOvershootInterpolator( float tension, float extraTension) {          mTension = tension * extraTension;      }        public AnticipateOvershootInterpolator(Context context, AttributeSet attrs) {          TypedArray a = context.obtainStyledAttributes(attrs, AnticipateOvershootInterpolator);            mTension = a.getFloat(AnticipateOvershootInterpolator_tension, 2 .0f) *                  a.getFloat(AnticipateOvershootInterpolator_extraTension, 1 .5f);            a.recycle();      }        private static float a( float t, float s) {          return t * t * ((s + 1 ) * t - s);      }        private static float o( float t, float s) {          return t * t * ((s + 1 ) * t + s);      }        public float getInterpolation( float t) {          // a(t, s) = t * t * ((s + 1) * t - s)          // o(t, s) = t * t * ((s + 1) * t + s)          // f(t) = 0.5 * a(t * 2, tension * extraTension), when t < 0.5          // f(t) = 0.5 * (o(t * 2 - 2, tension * extraTension) + 2), when t <= 1.0          if (t < 0 .5f) return 0 .5f * a(t * 2 .0f, mTension);          else return 0 .5f * (o(t * 2 .0f - 2 .0f, mTension) + 2 .0f);      } }


        Android 动画(anim)详解_第4张图片

        BounceInterpolator

        ?
        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 /**   * An interpolator where the change bounces at the end.   */ public class BounceInterpolator implements Interpolator {      public BounceInterpolator() {      }        @SuppressWarnings ({ "UnusedDeclaration" })      public BounceInterpolator(Context context, AttributeSet attrs) {      }        private static float bounce( float t) {          return t * t * 8 .0f;      }        public float getInterpolation( float t) {          // _b(t) = t * t * 8          // bs(t) = _b(t) for t < 0.3535          // bs(t) = _b(t - 0.54719) + 0.7 for t < 0.7408          // bs(t) = _b(t - 0.8526) + 0.9 for t < 0.9644          // bs(t) = _b(t - 1.0435) + 0.95 for t <= 1.0          // b(t) = bs(t * 1.1226)          t *= 1 .1226f;          if (t < 0 .3535f) return bounce(t);          else if (t < 0 .7408f) return bounce(t - 0 .54719f) + 0 .7f;          else if (t < 0 .9644f) return bounce(t - 0 .8526f) + 0 .9f;          else return bounce(t - 1 .0435f) + 0 .95f;      } }


        Android 动画(anim)详解_第5张图片

        CycleInterpolator

        ?
        1 2 3 4 5 6 7 8 9 10 11
  • 更多相关文章

    1. RelativeLayout的各种属性整理
    2. android之layout布局和ListView中的一些属性介绍
    3. android 控件属性大全
    4. Android:常用属性整理
    5. Android中animation方面知识: Android:interpolator 属性
    6. Android常用布局属性
    7. 我的Android进阶之旅------>Android 众多的布局属性详解

    随机推荐

    1. SQLServer2000 报1053错误(服务没有及时
    2. SQL Server中删除重复数据的几个方法
    3. sql 语句练习与答案
    4. 解析如何用SQL语句在指定字段前面插入新
    5. sql删除重复数据的详细方法
    6. 深入C++ string.find()函数的用法总结
    7. SQL SERVER 2000安装教程图文详解
    8. SQLServer日志清空语句(sql2000,sql2005,
    9. 获取SQL Server表字段的各种属性实例代码
    10. where条件顺序不同、性能不同示例探讨