自定义插值器TypeEvaluator

TypeEvaluator的使用方法

首先定义一个具有x,y,radius的类,帮助我们动态改变对应的x,y,radus的值

public class Particle {

    public float x;
    public float y;
    public float radius;

    public Particle() {
    }

    public Particle(float x, float y, float radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
}

自定义插值器 需要继承TypeEvaluator,我们从源码介绍中来看其中的意思


/**
 * 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)
 */
 //注释告诉我们开发者,可以通过setEvaluator()方法将自己定义的插值器应用于动画中(达到自定义动画效果)
public interface TypeEvaluator {

    /**
     * This function returns the result of linearly interpolating the start and end values, with
     * fraction representing the proportion between the start and end values. The
     * calculation is a simple parametric calculation: result = x0 + t * (x1 - x0),
     * where x0 is startValue, x1 is endValue,
     * and t is fraction.
     *
     * @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
     *         fraction parameter.
     */
     //这里告诉开发者 插值器的计算方式是:结果=startValue + fraction * (endValue - startValue). 通俗的讲就是变化量是介于startValue到endValue区间的值,我们动画效果的呈现顺序,比如先加速后减速的效果 只是前面一段 数值变化大 后面数值变化小。
    public T evaluate(float fraction, T startValue, T endValue);

}

接下来我们来从下面的LineEvaluator中找到思路。可以看出分别是对x,y,radius 三个数值进行改变。

public class LineEvaluator implements TypeEvaluator<Particle> {

    @Override
    public Particle evaluate(float fraction, Particle startValue, Particle endValue) {
        Particle particle = new Particle();
        particle.x = startValue.x + (endValue.x - startValue.x) * fraction;
        particle.y = startValue.y + (endValue.y - startValue.y) * fraction;
        particle.radius = startValue.radius + (endValue.radius - startValue.radius) * fraction;
        return particle;
    }
}

你可能感兴趣的:(Android--自定义控件)