TheSkyToRain's Note


  • 首页

  • 归档

Animator

发表于 2018-12-25

属性动画

写在前面的话

在属性动画之前,Andoid提供了帧动画和View动画;View动画包括AlphaAnimation、RotateAnimation、TranslateAnimation、ScaleAnimation;并提供了AnimationSet动画集合来混合使用多种动画。

1.ObjectAnimator

创建一个 ObjcetAnimator 只需要通过其静态工厂类直接返回一个ObjectAnimator对象即可。参数包括一个对象和对象的属性名字(这个属性必须要有set和get方法),其内部会通过反射机制去修改对象的属性值。例如:

1
2
3
ObjectAnimator anim = ObjectAnimator.ofFloat(mView,"translationX",200);
anim.setDuration(1000);
anim.start();

最后一个参数是一个可变的float类型数组。这跟View动画一样,可以设置时长、插值器等属性。

常用的可以直接使用属性动画的属性名称:
  • translationX 和 translationY:沿着X、Y轴进行平移。
  • rotation、rotationX、rotationY:用来围绕View的支点进行旋转。
  • PrivotX 和 PrivotY:控制View对象的支点位置,围绕这个支点进行旋转和缩进变化。
  • alpha:透明度,默认是1(不透明),0代表完全透明。
  • x 和 y:描述View对象在其容器中的最终位置。

2.ValueAnimator

ValueAnimator 不提供任何动画效果,它像是一个数值发生器,用来产生一定规律的数值,从而使调用者控制动画的实现过程。
通常情况下,在 ValueAnimator 的 AnimatorUpdateListener 中监听数值的变化。如下所示:

1
2
3
4
5
6
7
8
9
ValueAnimator valueAnim = ValueAnimator.ofFloat(0,100);
valueAnim.setTarget(view);
valueAnim.setDuration(1000).start();
valueAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Float f = animation.getAnimatedFraction();
}
});

3.动画的监听

动画总共分为start、repeat、end、cancel 四个过程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
ObjectAnimator anim = ObjectAnimator.ofFloat(view,"alpha",1.5f);
anim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {

}

@Override
public void onAnimationEnd(Animator animation) {

}

@Override
public void onAnimationCancel(Animator animation) {

}

@Override
public void onAnimationRepeat(Animator animation) {

}
});

多数情况,我们只需要关注 onAnimationEnd 方法,所以Android也提供了 AnimatorListenerAdapter() ,只需要对必要的事件进行监听。

1
2
3
4
5
6
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});

4.组合动画-AnimatorSet

AnimatorSet.Builder 包括以下4个方法:

  • after(Animator anim):将现有动画插入到传入的动画之后执行。
  • after(long delay):将现有动画延迟指定毫秒后执行。
  • before(Animator anim):将现有动画插入到传入的动画之前执行。
  • with(Animator anim):将现有动画和传入的动画同时执行。
    举个栗子:
    1
    2
    3
    4
    5
    6
    7
    ObjectAnimator anim1 = ObjectAnimator.ofFloat(view,"alpha",1.5f);
    ObjectAnimator anim2 = ObjectAnimator.ofFloat(view,"translationX",0.0f,200.0f,0.0f);
    ObjectAnimator anim3 = ObjectAnimator.ofFloat(view,"scaleX",1.0f,2.0f);
    AnimatorSet set = new AnimatorSet();
    set.setDuration(2000);
    set.play(anim1).with(anim2).after(anim3);
    set.start();

5.组合动画-PropertyValuesHolder

除了以上的 AnimatorSet ,还可以用 PropertyValuesHolder 来实现组合动画,只是说没有上面的那么丰富。因为 PropertyValuesHolder 只能是多个动画同时一起执行。
具体代码如下所示:

1
2
3
4
PropertyValuesHolder holder1 = PropertyValuesHolder.ofFloat("scaleX",1.0f,1.5f);
PropertyValuesHolder holder2 = PropertyValuesHolder.ofFloat("rotationX",0.0f,90.0f,0.0f);
ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(view,holder1,holder2);
anim.setDuration(2000).start();

6.在XML中使用属性动画

在 res 文件中新建一个 animator 文件,在里面新建一个 scale.xml ,如下:

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:propertyName="scaleX"
android:valueFrom="1.0"
android:valueTo="2.0"
android:valueType="floatType">
</objectAnimator>

在程序中引用上述定义的属性动画:

1
2
3
Animator animator = AnimatorInflater.loadAnimator(this,R.animator.scale);
animator.setTarget(view);
animator.start();

属性动画的学习到此结束…

ebaryice@gmail.com

talk is cheap, show me the code.

1 日志
1 标签
RSS
© 2018 ebaryice@gmail.com
Visitors Total times