Android最新动画框架完全解析(一)—— Animator(Property Animation)

来源:互联网 发布:深圳最好的java培训 编辑:程序博客网 时间:2024/05/28 23:11

Animator最早出现在Android 3.0 中,和之前的Animation框架相比,Animator更加的灵活并且具有更多的功能,官方推荐使用Animator代替Animation。
使用Animator前需要先了解几个属性:

  • Duration:动画播放时间
  • Time interpolation:属性值随着时间的改变情况,比如线性增长或者先快后慢
  • Repeat count:动画重复播放次数
  • Animator sets:动画集,可以使多个动画同时播放或者顺序播放
  • Frame refresh delay:动画每一帧的刷新时间,一般默认10ms刷新一次

Property Animation

Property Animation非常强大,他可以让你几乎在任何东西上播放动画。Property Animation的结构如下图:

ValueAnimator用来跟踪动画运行的时间和属性的值。其中TimeInterpolator指定了动画的 interpolation,如AccelerateDecelerateInterpolator。TypeEvaluator指定属性的值如何计算比如IntEvaluator.

Property Animation与View Animation的区别

  • View Animation只能对View添加动画
  • View Animation只能改变如scale、rotation等值,不能改变background color等属性
  • View Animation改变的只是View画的位置,并不是真正的View,比如一个button从左到右移动,触发onClick方法的位置还是初试的位置。
  • View Animation实现起来比Property Animation简单

Animator

Animator提供了创建动画的基本结构,通常我们不直接使用它,而是使用它的子类。

ValueAnimator

使用ofInt(), ofFloat(), 或者 ofObject()方法来获得ValueAnimator实例

ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);animation.setDuration(1000);animation.start();

当然也可以自定义类型

ValueAnimator animation = ValueAnimator.ofObject(new MyTypeEvaluator(), startPropertyValue, endPropertyValue);animation.setDuration(1000);animation.start();

当然这样动画还是不能正常运行的,需要实现Animation Listeners,具体内容请参考Animation Listeners

ObjectAnimator

ObjectAnimator是ValueAnimator 的子类。可以直接对目标属性计算

ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f);anim.setDuration(1000);anim.start();

为了使ObjectAnimator正常运行,还需要如下步骤:

  • 要修改的属性必须有set方法,如setFoo(),如果没有
    • 如果有权限,添加set方法
    • 使用这个类的wrapper class(包装类)
    • 使用ValueAnimator
  • 如果你在values…参数中只指定了一个参数,默认为这是最后一个参数。参数必须有get方法,如getFoo()
  • 有些属性需要手动刷新,所以要在onAnimationUpdate() 中调用invalidate()。
anim.addUpdateListener(new AnimatorUpdateListener()        {            @Override            public void onAnimationUpdate(ValueAnimator animation)            {                view.invalidate();            }        });

AnimatorSet

一个Set中包含多个动画,使用起来也很方便,直接上代码。

AnimatorSet bouncer = new AnimatorSet();bouncer.play(bounceAnim).before(squashAnim1);bouncer.play(squashAnim1).with(squashAnim2);bouncer.play(squashAnim1).with(stretchAnim1);bouncer.play(squashAnim1).with(stretchAnim2);bouncer.play(bounceBackAnim).after(stretchAnim2);ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);fadeAnim.setDuration(250);AnimatorSet animatorSet = new AnimatorSet();animatorSet.play(bouncer).before(fadeAnim);animatorSet.start();

在XML中声明动画

新建res/animator/目录
每种Animator对应的标签

  • ValueAnimator -
  • ObjectAnimator -
  • AnimatorSet -
<set android:ordering="sequentially">    <set>        <objectAnimator            android:propertyName="x"            android:duration="500"            android:valueTo="400"            android:valueType="intType"/>        <objectAnimator            android:propertyName="y"            android:duration="500"            android:valueTo="300"            android:valueType="intType"/>    </set>    <objectAnimator        android:propertyName="alpha"        android:duration="500"        android:valueTo="1f"/></set>

在Activity中调用

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,    R.anim.property_animator);set.setTarget(myObject);set.start();

总结

本篇文章讲解了Property Animation的一些基本知识和用法,但是所有的动画都是针对某一Object来进行的,有没有方法可以同时对多个Object进行动画呢,请看下一篇博文Android最新动画框架完全解析(二)——Transitions Framework(Transitions 框架)

0 0
原创粉丝点击