# Android动画笔记

标签: Android开发艺术探索笔记


  • View动画
  • 帧动画
  • 属性动画

View动画

 View动画的作用对象时View,有4种动画效果,分别是平移动画、缩放动画、旋转动画、和透明度动画。
此类动画通常使用` XML `来定义。

动画的定义方式如下:

    <set 
        android:interpolatoer="@[package:]anim/interpolator_resource"
        android:shareInterpolator=["true"|"false"]
    >
    
    <alpha
        android:fromAlpha="float"
        android:toAlpha="float"
        />
    
    <scale
        android:fromScaleX="float"
        android:toScaleX="float"
        android:fromScaleY="float"
        android:toScaleY="float"
        android:pivotX="float"
        android:pivotY="float"
        />
        
    <rotate
        android:fromDegrees="float"
        android:toDegrees="float"
        android:pivotX="float"
        android:pivotY="float"
        />
        
    <translate
        android:fromXDelta="float"
        android:toXDelta="float"
        android:fromYDelta="float"
        android:toYDelta="float"
        />
    
    </set>


动画的使用:

Button mButton = (Button) findViewById(R.id.button);
Animation animation = AnimationUtils.loadAnimation(this,R.anim.animation_test);
mButton.startAnimation(animation);


动画的监听:

通过Animation的setAnimationListener方法可以给View动画添加过程监听,接口如下所示:
public static interface AnimationListener{
    void onAnimationStart(Animation animation);
    void onAnimationEnd(Animation animation);
    void onAnimationRepeat(Animation animation);
}


帧动画

帧动画是顺序播放一组预先定义的图片,类似于电影播放。使用比较简单,但是容易引起OOM,所以一般应该尽量避免使用。

View动画的使用场景

1. LayoutAnimation
LayoutAnimation作用于ViewGroup,更经常的是作用于ListView,使得每个子Item都具有此动画
  • 定义

//res/anim/anim_layout.xml
<layoutAnimation
     android:delay="0.5"
     android:animationOrder="normal|reverse|random"
     android:animation="@anim/anim_item"
     />  
     
  • 使用

    • xml使用

    <ListView
        ...
        android:layoutAnimation="@anim/anim_layout"
        />
    
     - java使用
    ListView listView = ...;
    Animation animation = AnimationUtils.loadAnimation(this,R.anim.anim_item);
    LayoutAnimationController controller = new LayoutAnimationController(animation);
    controller.setDelay(0.5f);
    controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
    listView.setLayoutAnimation(controller);
    
2. Activity的切换效果
Activity的切换效果主要用到`overridePendingTransition(int enterAnim, int exitAnim)`这个方法,需要注意的是,**这个方法需要在`startActivity(Intent intent)`或者`finish()`之后被调用才能生效!**

属性动画

属性动画是API11新加入的特性,它可以对任何对象做动画,比较重要的概念是ValueAnimatiorObjectAnimatorAnimatorSet这几个概念。


注意ObjectAnimator继承自ValueAnimatior
注意动画的默认间隔为300毫秒、默认帧率为10ms/帧


属性动画的使用

*属性动画也可以采用`XML`定义,但是推荐使用代码实现。*
//向上平移200dp,第一个参数为动画作用的对象,第二个参数为改变的属性,第三个参数为数组,改变的值
//注意:若第二个参数出错,即对象没有该属性,则动画不显示
//此外,若第三个参数没有变化初始值(只有一个值即目标值)时,该对象需要提供该属性的setter方法,否则会抛出异常
ObjectAnimator.ofFloat(object,"translationY",200).start();

//改变背景色
ValueAnimator colorAnim = ObjectAnimator.ofInt(this,"backgroundColor",0xFFFF8080,0xFF8080FF);
colorAnim.setDuration(3000);
colorAnim.setEvaluator(new ArgbEvalutor());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);
colorAnim.setRepeatMode(ValueAnimator.REVERSE);
colorAnim.start();

//动画集合
AnimatorSet set =  new AnimatorSet();
set.playTogether(
    animator1,
    animator2,
    animator3,
    animator4
);
set.start();


插值器与估值器

  • 插值器(TimeInterpolator):它的作用是根据时间流逝的百分比来计算出当前属性值改变的百分比,系统预置的有LinearInterpolatorAccelerateDecelerateInterpolatorDecelerateIntepolator

  • 估值器(TypeEvaluator):它的作用是根据当前属性改变的百分比来计算改变后的属性值(插值器得出的值),系统预置的有IntEvaluator(针对整型属性)、FloatEvaluator(针对浮点型属性)和ArgbEvaluator(针对Color属性)


属性动画的监听

属性动画也提供了监听器用于监听动画的过程,如下所示:

public static interface AnimatorListener{
    void onAnimationStart(Animator animation);
    void onAnimationEnd(Animator animation);
    void onAnimationCancel(Animator animation);
    void onAnimationRepeat(Animator animation);
}

此外,系统还提供了AnimatorListenerAdapter这个类,我们可以通过这个类来有选择的实现上面的接口方法,而不必实现所有的方法。

另外,还有一个AnimatorUpdateListener接口,如下所示:

public static interface AnimatorUpdateListener{
    void onAnimationUpdate(ValueAnimator animation);
}

这个监听器会监听整个动画过程,每播放一帧,onAnimationUpdate()就会被调用一次。


原文地址:https://www.cnblogs.com/caoweizhao/p/6777817.html