Android动画总结(2)属性动画

感觉郭神这三篇文章讲的非常有道理,就不做搬运工了,直接贴链接

  1. Android属性动画完全解析(上),初识属性动画的基本用法
  2. Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
  3. Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法

属性动画有以下几个需要注意的点:

  1. ValueAnimator仅仅是对一个值的变化, 具体利用这个值做什么,还要在监听里面实现。
  2. ObjectAnimator 是对某一个对象的某一个属性调用,而能不能用这个xx属性,是直接使用这个对象的setxx方法和getxx方法,所以只要提供get和set方法,就可以使用ObjectAnimator
  3. ObjectAnimator如果要使用ofObject,最重要的编写实现TypeEvaluator接口的类,这个接口接受一个泛型,就是你要进行动画的ofObject的对象的类型。
  4. TypeEvaluator有个函数evaluate(float fraction, Integer startValue, Integer endValue) ,有三个参数,其中两个可以看出是初始值和结束值,中间的fraction是一个0-1的值,我理解为当前时间过去了多少,这个值我们可以通过interceptor来改变,从而达到控制速度的目的。
  5. Interpolator也是一个接口
/**
 * A time interpolator defines the rate of change of an animation. This allows animations
 * to have non-linear motion, such as acceleration and deceleration.
 */
public interface TimeInterpolator {

    /**
     * Maps a value representing the elapsed fraction of an animation to a value that represents
     * the interpolated fraction. This interpolated value is then multiplied by the change in
     * value of an animation to derive the animated value at the current elapsed animation time.
     *
     * @param input A value between 0 and 1.0 indicating our current point
     *        in the animation where 0 represents the start and 1.0 represents
     *        the end
     * @return The interpolation value. This value can be more than 1.0 for
     *         interpolators which overshoot their targets, or less than 0 for
     *         interpolators that undershoot their targets.
     */
    float getInterpolation(float input);
}

  • 其中我们可以看到getInterpolation这个方法,这个方法就是把一个线性变化的0-1的值按照我们编写的函数映射成另一个值,这个值就是Evaluator接受的fraction参数

  • 我们需要明白其背后的原理和概念。动画的基本原理是从开始时间到结束时间一帧一帧地播放静态图像,如果我们用0.0表示动画的开始时间点,用1.0表示动画的结束时间点,则动画时间轴上的每个点都可以转换成0.0到1.0之间的一个浮点数,本文中我们称为Time Index。比如,动画中间的时间点可以用0.5来表示。对于使用了LinearInterpolator(线性插值器)的平移动画来讲,在0.3这个时间点视图则刚好移动了整个动画的30%。

我们一般不会将Time Index直接带入到动画计算公式,这也是Interpolator的存在的意义。

Android动画总结(2)属性动画_第1张图片
image.png

从本质上来讲,Interpolator是一种数学函数,参数是0.0到1.0之间的浮点数,输出也是一个浮点数。如上图所示,这是一个AccelerateInterpolator(加速插值器)的图。目标对象的起始速度是0,然后逐渐增大(译者注:曲线的斜率是速度)。默认情况下,可以用下面的表达式来表示:
y = t^2
这里的y就是Interpolator的输出,t是Time Index。我们可以增加一个参数来对AccelerateInterpolator的行为进行自定义:
y = t ^(2f)
这里的f越大,起始速度越慢,但是后期加速度会更大;如果f=0.5,则和LinearInterpolator的行为一模一样了。

ViewPropertyAnimator

ViewPropertyAnimator提供了一种更接近于面向对象的方法,同时有一定的性能优化.

在Android API 12时,View中添加了animate方法,具体如下:

public class View implements Drawable.Callback, KeyEvent.Callback,
        AccessibilityEventSource {
     ......
     /**
     * This method returns a ViewPropertyAnimator object, which can be used to animate
     * specific properties on this View.
     *
     * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
     */
    public ViewPropertyAnimator animate() {
        if (mAnimator == null) {
            mAnimator = new ViewPropertyAnimator(this);
        }
        return mAnimator;
    }
    ......
}
  • 可以看见通过View的animate()方法可以得到一个ViewPropertyAnimator的属性动画(有人说他没有继承Animator类,是的,他是成员关系,不是之前那种继承关系)。

  • ViewPropertyAnimator提供了一种非常方便的方法为View的部分属性设置动画(切记,是部分属性),它可以直接使用一个Animator对象设置多个属性的动画;在多属性设置动画时,它比 上面的ObjectAnimator更加牛逼、高效,因为他会管理多个属性的invalidate方法统一调运触发,而不像上面分别调用,所以还会有一些性能优化。如下就是一个例子:
    myView.animate().x(0f).y(100f).start();

你可能感兴趣的:(Android动画总结(2)属性动画)