动画分类

动画总结:

  1. 补间动画:指的是TranslationAnimation,AlpahAnimation,ScaleAnimation,RotateAnimation; 缺点:并没有真正持久改变View的属性,就是说它内部没有一个去记录动画行为的机制;

  2. 帧动画:指的是一帧一帧播放的动画 实现:通过animation-list来实现,写法如下:

    1
    2
    3
    4
    5
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:duration="200" android:drawable="@drawable/ic_launcher"/>
    <item android:duration="200" android:drawable="@drawable/ic_launcher"/>
    <item android:duration="200" android:drawable="@drawable/ic_launcher"/>
    </animation-list>
  1. 属性动画(为了解决补间动画的缺点)

    1. 属性动画内部实现:3.0之后view类增加新的用来记录动画行为的属性,如:

      • translationX,translationY;
      • scaleX,scaleY;
      • rotationX,rotationY,rotation;
      • alpha;

      具体实现的类:ObjectAnimator;

      问题是:ObjectAnimator只能是3.0之后才有,那么我们如果想让属性动画兼容低版本,那么一般 使用NineOldAnidroid.jar来实现属性动画

    2. NineOldAnidroid.jar:主要封装了属性动画和View相关的操作类,该类库是JackWharton来写的;

      用法:

      1
      2
      3
      4
      ViewPropertyAnimator.animate(text)
      .rotationBy(180)
      .setDuration(500)
      .start();
    3. 直接操作view的属性来更改view的形态:

      • view.setTranslationX();
      • view.setRotationX();
      • view.setAlpha();
      • view.setScaleX();

      //如果想在低版本直接操作view的属性,则用如下方法

      1
      ViewHelper.setScaleX(text, 0);
    4. 改变动画的运动轨迹:速度插值器

      • OvershootInterpolator:超过一点再回来
      • BounceInterpolator:像球落地一样的感觉
    5. 自定义动画逻辑:ValueAnimator(值动画)

      ValueAnimator: 它只是帮我们定义了动画的执行流程,但是没有帮我们实行具体的动画逻辑,我们需要监听动画的进度,然后在回调方法中进行自定义的动画逻辑; 用法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    ValueAnimator animator = ValueAnimator.ofInt(100,1000); 
    //监听动画执行的进度
    animator.addUpdateListener(new AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animator) {
    int animatedValue = (Integer) animator.getAnimatedValue();
    //Log.e("tag", "animatorValue:"+animatedValue);
    //根据动画值的变化进行我们的动画逻辑 //
    LayoutParams params = text.getLayoutParams();
    //params.height = animatedValue;
    //text.setLayoutParams(params);
    text.setText(animatedValue+"");
    }
    });
    animator.setDuration(1500);
    animator.setStartDelay(1000);
    animator.start();