Android属性动画基础:TypeEvaluator

  本文简单介绍一下TypeEvaluator,来了解一下它的用途。TypeEvaluator是一个接口,我们可以自定义该接口实例,并通过ValueAnimator的setEvaluator(TypeEvaluator)方法来控制动画的更新计算表达式。如果您只是想利用属性动画操纵单一数值变化(如控制View的X或Y方向旋转、X或Y平移等,或仅仅是操纵与对象无关的数值),那么你完全不必关心TypeEvaluator,通过插值器控制就好了,杀鸡焉用牛刀啊,牙签足以。但是如果您要同时操纵对象的多个属性怎么办,比如您想模拟小球运动,要控制X和Y两个方向的坐标,而且由于两个方向的加速度不一样导致X和Y坐标的计算方式也不一样,难道您想通过动画组合的方式来分别处理X、Y吗,您当然可以这么做,不过并不好,此外如果还要求您记录运动轨迹,这种X、Y组合动画就无能为力了。这种相对复杂的场景,TypeEvaluator就可以大展身手了,好了,先看一下源码及api介绍

/**
 * Interface for use with the {@link ValueAnimator#setEvaluator(TypeEvaluator)} function. Evaluators
 * allow developers to create animations on arbitrary property types, by allowing them to supply
 * custom evaluators for types that are not automatically understood and used by the animation
 * system.
 *
 * @see ValueAnimator#setEvaluator(TypeEvaluator)
 */
public interface TypeEvaluator<T> {

    /**
     * This function returns the result of linearly interpolating the start and end values, with
     * <code>fraction</code> representing the proportion between the start and end values. The
     * calculation is a simple parametric calculation: <code>result = x0 + t * (x1 - x0)</code>,
     * where <code>x0</code> is <code>startValue</code>, <code>x1</code> is <code>endValue</code>,
     * and <code>t</code> is <code>fraction</code>.
     *
     * @param fraction   The fraction from the starting to the ending values
     * @param startValue The start value.
     * @param endValue   The end value.
     * @return A linear interpolation between the start and end values, given the
     *         <code>fraction</code> parameter.
     */
    public T evaluate(float fraction, T startValue, T endValue);
}

  这个接口代码看起来是相当简单,就一个方法,通过接口描述,我们可以知道,TypeEvaluator能够支持我们创建支持任意属性类型的动画。如果您有看过谷歌对属性动画的描述,就该知道,属性动画能够操纵任类型意属性,如果操纵的是对象,系统只内置了数值型计算方式,那么对象属性如何更新计算?TypeEvaluator就是解决这个问题的,当然,您也可以使用TypeEvaluator来计算基础数值型数据,这不是不可以的。evaluate方法就是更新计算方法,通过该方法计算出来的属性值来更新动画,参数fraction是插值器getInterpolation方法计算出来的时间因子(请参考Android属性动画基础:你是否真的了解插值器(TimeInterpolator));startValue和endValue分别表示每个动画区间段的起始值和终点值。
  以我们之前说的模拟小球运动轨迹为示例,写段简单的使用方法(下一篇文章会给出完整示例及另一种模拟方式)

 // 描述小球运动轨迹坐标
    private class Point {
        private float pointX;
        private float pointY;

        private Point(float pointX, float pointY) {
            this.pointX = pointX;
            this.pointY = pointY;
        }

        private float getPointX() {
            return pointX;
        }

        private float getPointY() {
            return pointY;
        }
    }

 // 模拟平抛运动轨迹的估值器
    private class MyTypeEvaluator implements TypeEvaluator<Point> {
        @Override
        public Point evaluate(float fraction, Point startValue, Point endValue) {
            float pointX = fraction * endValue.getPointX() + startValue.getPointX();
            float pointY = fraction * fraction * endValue.getPointY() + startValue.getPointY();
            Point point  = new Point(pointX, pointY);
            // ToDo 您可以在此记录或做相应操作
            return point;
        }
    }

    ValueAnimator animator = ValueAnimator.ofObject(new MyTypeEvaluator(), new Point(0, 0), new Point(300, 600)).setDuration(1000);
    animator.setInterpolator(new LinearInterpolator());
    animator .addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Point point = (Point) animation.getAnimatedValue();
               // ToDo 您也可以在此记录或做相应操作
            }
        });
    animator.start();

  这篇文章比较简单,就到这,对于复杂属性动画,请不要忘记TypeEvaluator

    原文作者:乱世白衣
    原文地址: https://www.jianshu.com/p/d40fb4f08bf3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞