Android 动画(anim)详解

来源:互联网 发布:ds数据精灵好用吗 编辑:程序博客网 时间:2024/05/29 16:16

转载于: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配置:

<!--?xml version="1.0"encoding="utf-8"?-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
  
<!-- 透明度控制动画效果 alpha
        浮点型值:
            fromAlpha 属性为动画起始时透明度
            toAlpha   属性为动画结束时透明度
            说明:
                0.0表示完全透明
                1.0表示完全不透明
            以上值取0.0-1.0之间的float数据类型的数字
 
        长整型值:
            duration  属性为动画持续时间
            说明:    
                时间以毫秒为单位
-->
</alpha></set>


  • Java代码:

AlphaAnimation(floatfromAlpha,floattoAlpha)

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

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




?

Scale

  • xml配置:

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

</scale></set>

<!--尺寸伸缩动画效果 scale

       属性:interpolator 指定一个动画的插入器

        基本的有三种动画插入器:

            accelerate_decelerate_interpolator:加速-减速 动画插入器

            accelerate_interpolator :加速-动画插入器

            decelerate_interpolator :减速- 动画插入器

        其他的属性单独解释

      浮点型值:  

            fromXScale:属性为动画起始时 X坐标上的伸缩尺寸   

            toXScale:属性为动画结束时 X坐标上的伸缩尺寸    

            fromYScale:属性为动画起始时Y坐标上的伸缩尺寸   

            toYScale:属性为动画结束时Y坐标上的伸缩尺寸   

      说明:

           以上四种属性值   

                    0.0表示收缩到没有

                    1.0表示正常无伸缩    

                    值小于1.0表示收缩 

                    值大于1.0表示放大

         

            pivotX:属性为动画相对于物件的X坐标的开始位置

            pivotY:属性为动画相对于物件的Y坐标的开始位置

         

       说明:

            以上两个属性值 从0%-100%中取值

            50%为物件的X或Y方向坐标上的中点位置

         

        长整型值:

            duration  属性为动画持续时间

            说明:   时间以毫秒为单位

 

        布尔型值:

            fillAfter 属性 当设置为true,该动画转化在动画结束后被应用

-->

  • 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配置:
<!--?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">

<!-- translate 位置转移动画效果
        整型值:
            fromXDelta:属性为动画起始时 X坐标上的位置   
            toXDelta:属性为动画结束时 X坐标上的位置
            fromYDelta:属性为动画起始时 Y坐标上的位置
            toYDelta:属性为动画结束时 Y坐标上的位置
        注意:
            没有指定fromXType toXType fromYType toYType 时候,默认是以自己为相对参照物            
        长整型值:
            duration  属性为动画持续时间
            说明:   时间以毫秒为单位
-->
</translate></set>

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




?

Rorate

  • xml配置:
<!--?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"

<!-- rotate 旋转动画效果
       属性:interpolator 指定一个动画的插入器
             基本有三种动画插入器:
                accelerate_decelerate_interpolator:加速-减速 动画插入器
                accelerate_interpolator:加速-动画插入器
                decelerate_interpolator :减速- 动画插入器
            <span style="font-family: Arial, Helvetica, sans-serif;">其他的属性单独解释</span>
 
       浮点数型值:
            fromDegrees:属性为动画起始时物件的角度   
            toDegrees:属性为动画结束时物件旋转的角度 可以大于360度  
 
 
            说明:
                     当角度为负数——表示逆时针旋转
                     当角度为正数——表示顺时针旋转             
                     (负数from——to正数:顺时针旋转)  
                     (负数from——to负数:逆时针旋转)
                     (正数from——to正数:顺时针旋转)
                     (正数from——to负数:逆时针旋转)      
 
            pivotX:属性为动画相对于物件的X坐标的开始位置
            pivotY:属性为动画相对于物件的Y坐标的开始位置
 
            说明:        以上两个属性值 从0%-100%中取值
                         50%为物件的X或Y方向坐标上的中点位置
 
        长整型值:
            duration  属性为动画持续时间
            说明:       时间以毫秒为单位
-->
</rotate></set>


  • 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中的定义方式如下:

 <item android:drawable="@drawable/rocket_thrust1"android:duration="200">
    <item android:drawable="@drawable/rocket_thrust2"android:duration="200">
    <item android:drawable="@drawable/rocket_thrust3"android:duration="200">
</item></item></item></animation-list>

java代码:
protectedvoidonCreate(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();
    }
 
    publicbooleanonTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            anim.stop();
            anim.start();
            returntrue;
        }
        returnsuper.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
        <bounceinterpolator> 无
        <cycleinterplolator> android:cycles 整数值,循环的个数,默认为1
        <decelerateinterpolator> android:factor 浮点值,减速的速率,默认为1
        <linearinterpolator> 无
        <overshootinterpolator> 浮点值,超出终点后的张力、拉力,默认为2

      • </overshootinterpolator>
      • </linearinterpolator>
      • </decelerateinterpolator>
      • </cycleinterplolator>
      • </bounceinterpolator>
      • </anticipateovershootinterpolator>
      • </anticipateinterploator>
      • </accelerateinterpolator>
      • </acceleratedecelerateinterpolator>


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

        ?
        1
        2
        <!--?xml version="1.0"encoding="utf-8"?-->
        <overshootinterpolator xmlns:android="http://schemas.android.com/apk/res/android"android:tension="7.0"></overshootinterpolator>

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


        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.
         *
         */
        publicclassAccelerateDecelerateInterpolator implementsInterpolator {
            publicAccelerateDecelerateInterpolator() {
            }
             
            @SuppressWarnings({"UnusedDeclaration"})
            publicAccelerateDecelerateInterpolator(Context context, AttributeSet attrs) {
            }
             
            publicfloatgetInterpolation(floatinput) {
                return(float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
            }
        }


        \

        <喎�"/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); } } }

        \

        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.
         */
        publicclassAnticipateInterpolator implementsInterpolator {
            privatefinalfloat mTension;
         
            publicAnticipateInterpolator() {
                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.
             */
            publicAnticipateInterpolator(floattension) {
                mTension = tension;
            }
         
            publicAnticipateInterpolator(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();
            }
         
            publicfloatgetInterpolation(floatt) {
                // a(t) = t * t * ((tension + 1) * t - tension)
                returnt * t * ((mTension + 1) * t - mTension);
            }
        }


        \

        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.
         */
        publicclassAnticipateOvershootInterpolator implementsInterpolator {
            privatefinalfloat mTension;
         
            publicAnticipateOvershootInterpolator() {
                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.
             */
            publicAnticipateOvershootInterpolator(floattension) {
                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.
             */
            publicAnticipateOvershootInterpolator(floattension,floatextraTension) {
                mTension = tension * extraTension;
            }
         
            publicAnticipateOvershootInterpolator(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();
            }
         
            privatestaticfloat a(floatt,floats) {
                returnt * t * ((s + 1) * t - s);
            }
         
            privatestaticfloat o(floatt,floats) {
                returnt * t * ((s + 1) * t + s);
            }
         
            publicfloatgetInterpolation(floatt) {
                // 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)return0.5f * a(t * 2.0f, mTension);
                elsereturn0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);
            }
        }


        \

        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.
         */
        publicclassBounceInterpolator implementsInterpolator {
            publicBounceInterpolator() {
            }
         
            @SuppressWarnings({"UnusedDeclaration"})
            publicBounceInterpolator(Context context, AttributeSet attrs) {
            }
         
            privatestaticfloat bounce(floatt) {
                returnt * t * 8.0f;
            }
         
            publicfloatgetInterpolation(floatt) {
                // _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)returnbounce(t);
                elseif(t < 0.7408f)returnbounce(t - 0.54719f) + 0.7f;
                elseif(t < 0.9644f)returnbounce(t - 0.8526f) + 0.9f;
                elsereturnbounce(t - 1.0435f) + 0.95f;
            }
        }


        \

        CycleInterpolator

        ?
        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
        /**
         * Repeats the animation for a specified number of cycles. The
         * rate of change follows a sinusoidal pattern.
         *
         */
        publicclassCycleInterpolator implementsInterpolator {
            publicCycleInterpolator(floatcycles) {
                mCycles = cycles;
            }
             
            publicCycleInterpolator(Context context, AttributeSet attrs) {
                TypedArray a =
                    context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.CycleInterpolator);
                 
                mCycles = a.getFloat(com.android.internal.R.styleable.CycleInterpolator_cycles, 1.0f);
                 
                a.recycle();
            }
             
            publicfloatgetInterpolation(floatinput) {
                return(float)(Math.sin(2* mCycles * Math.PI * input));
            }
             
            privatefloatmCycles;
        }


          参数为2时的曲线:

        \

        DecelerateInterpolator


        ?
        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
        /**
         * An interpolator where the rate of change starts out quickly and
         * and then decelerates.
         *
         */
        publicclassDecelerateInterpolator implementsInterpolator {
            publicDecelerateInterpolator() {
            }
         
            /**
             * Constructor
             *
             * @param factor Degree to which the animation should be eased. Setting factor to 1.0f produces
             *        an upside-down y=x^2 parabola. Increasing factor above 1.0f makes exaggerates the
             *        ease-out effect (i.e., it starts even faster and ends evens slower)
             */
            publicDecelerateInterpolator(floatfactor) {
                mFactor = factor;
            }
             
            publicDecelerateInterpolator(Context context, AttributeSet attrs) {
                TypedArray a =
                    context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.DecelerateInterpolator);
                 
                mFactor = a.getFloat(com.android.internal.R.styleable.DecelerateInterpolator_factor, 1.0f);
                 
                a.recycle();
            }
             
            publicfloatgetInterpolation(floatinput) {
                floatresult;
                if(mFactor == 1.0f) {
                    result = (float)(1.0f - (1.0f - input) * (1.0f - input));
                }else{
                    result = (float)(1.0f - Math.pow((1.0f - input), 2* mFactor));
                }
                returnresult;
            }
             
            privatefloatmFactor = 1.0f;
        }

        \

        LinearInterpolator

        ?
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        /**
         * An interpolator where the rate of change is constant
         *
         */
        publicclassLinearInterpolator implementsInterpolator {
         
            publicLinearInterpolator() {
            }
             
            publicLinearInterpolator(Context context, AttributeSet attrs) {
            }
             
            publicfloatgetInterpolation(floatinput) {
                returninput;
            }
        }


        \

        OvershootInterpolator

        ?
        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
        /**
         * An interpolator where the change flings forward and overshoots the last value
         * then comes back.
         */
        publicclassOvershootInterpolator implementsInterpolator {
            privatefinalfloat mTension;
         
            publicOvershootInterpolator() {
                mTension = 2.0f;
            }
         
            /**
             * @param tension Amount of overshoot. When tension equals 0.0f, there is
             *                no overshoot and the interpolator becomes a simple
             *                deceleration interpolator.
             */
            publicOvershootInterpolator(floattension) {
                mTension = tension;
            }
         
            publicOvershootInterpolator(Context context, AttributeSet attrs) {
                TypedArray a = context.obtainStyledAttributes(attrs,
                        com.android.internal.R.styleable.OvershootInterpolator);
         
                mTension =
                        a.getFloat(com.android.internal.R.styleable.OvershootInterpolator_tension,2.0f);
         
                a.recycle();
            }
         
            publicfloatgetInterpolation(floatt) {
                // _o(t) = t * t * ((tension + 1) * t + tension)
                // o(t) = _o(t - 1) + 1
                t -= 1.0f;
                returnt * t * ((mTension + 1) * t + mTension) + 1.0f;
            }
        }





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