AnimationEasingFunctions类库的使用

首先感谢类库作者。

这个是一个定义了各种TypeEvaluator的类库,并通过TypeEvaluator来生成一个值动画对象。

Glider.class

public static ValueAnimator glide(Skill skill, float duration, ValueAnimator animator) {
        return Glider.glide(skill, duration, animator, null);
    }

    public static ValueAnimator glide(Skill skill, float duration, ValueAnimator animator, BaseEasingMethod.EasingListener... listeners) {
        BaseEasingMethod t = skill.getMethod(duration);

        if (listeners != null)
            t.addEasingListeners(listeners);

        animator.setEvaluator(t);
        return animator;
    }

    public static PropertyValuesHolder glide(Skill skill, float duration, PropertyValuesHolder propertyValuesHolder) {
        propertyValuesHolder.setEvaluator(skill.getMethod(duration));
        return propertyValuesHolder;
    }
这个就是使用的关键类,由上面的方法可以看出该类可以生成ValueAnimator和PropertyValuesHolder对象,在方法的参数总涉及到了一个Skill对象,这是一个枚举类,定义了所有重写的TypeEvaluator对象,并可以通过方法来获得对应的实例。

Skill.class

public enum  Skill {

    BackEaseIn(BackEaseIn.class),
    BackEaseOut(BackEaseOut.class),
    BackEaseInOut(BackEaseInOut.class),

    BounceEaseIn(BounceEaseIn.class),
    BounceEaseOut(BounceEaseOut.class),
    BounceEaseInOut(BounceEaseInOut.class),

    CircEaseIn(CircEaseIn.class),
    CircEaseOut(CircEaseOut.class),
    CircEaseInOut(CircEaseInOut.class),

    CubicEaseIn(CubicEaseIn.class),
    CubicEaseOut(CubicEaseOut.class),
    CubicEaseInOut(CubicEaseInOut.class),

    ElasticEaseIn(ElasticEaseIn.class),
    ElasticEaseOut(ElasticEaseOut.class),

    ExpoEaseIn(ExpoEaseIn.class),
    ExpoEaseOut(ExpoEaseOut.class),
    ExpoEaseInOut(ExpoEaseInOut.class),

    QuadEaseIn(QuadEaseIn.class),
    QuadEaseOut(QuadEaseOut.class),
    QuadEaseInOut(QuadEaseInOut.class),

    QuintEaseIn(QuintEaseIn.class),
    QuintEaseOut(QuintEaseOut.class),
    QuintEaseInOut(QuintEaseInOut.class),

    SineEaseIn(SineEaseIn.class),
    SineEaseOut(SineEaseOut.class),
    SineEaseInOut(SineEaseInOut.class),

    Linear(Linear.class);


    private Class easingMethod;

    private Skill(Class clazz) {
        easingMethod = clazz;
    }

    public BaseEasingMethod getMethod(float duration) {
        try {
            return (BaseEasingMethod)easingMethod.getConstructor(float.class).newInstance(duration);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Error("Can not init easingMethod instance");
        }
    }
}
上面所有的属性值的计算对象都是继承自BaseEasingMethod类,该类实现了TypeEvaluator接口:

BaseEasingMethod.class

public abstract class BaseEasingMethod implements TypeEvaluator<Number> {
    protected float mDuration;
    private ArrayList<EasingListener> mListeners = new ArrayList<EasingListener>();

    public interface EasingListener {
        public void on(float time, float value, float start, float end, float duration);
    }

    public void addEasingListener(EasingListener l) {
        mListeners.add(l);
    }

    public void addEasingListeners(EasingListener... ls) {
        for (EasingListener l : ls) {
            mListeners.add(l);
        }
    }

    public void removeEasingListener(EasingListener l) {
        mListeners.remove(l);
    }

    public void clearEasingListeners() {
        mListeners.clear();
    }

    public BaseEasingMethod(float duration) {
        mDuration = duration;
    }

    public void setDuration(float duration) {
        mDuration = duration;
    }


    @Override
    public final Float evaluate(float fraction, Number startValue, Number endValue) {
        //计算出当前所处的时间
        float t = mDuration * fraction;
        //起始属性值
        float b = startValue.floatValue();
        //属性值的最大值
        float c = endValue.floatValue() - startValue.floatValue();
        //时间
        float d = mDuration;
        //传入抽象方法
        float result = calculate(t, b, c, d);
        //遍历监听器集合,调用方法
        for (EasingListener l : mListeners) {
            l.on(t, result, b, c, d);
        }
        return result;
    }

    public abstract Float calculate(float t, float b, float c, float d);

}

该类中定义了一个持续时间,一个存放监听器的集合,evaluate方法主要是计算出各种值后把参数传入calculate抽象方法,具体看各个子类中calculate方法的实现。具体的效果可以看应用中的呈现。




阅读更多

没有更多推荐了,返回首页