Android动画的Interpolator

interpolation的意思就是插值,Interpolator实际上是一个用于计算动画插值的接口,其作用是把一个float(从0.0到1.0)映射为另一个float,

只要满足: f(0.0)=0.0,  f(1.0) = 1.0

接口原型应该是比较简单的:

public interface TimeInterpolator{
   public abstract float getInterpolation(float input);
}

举一个插值器作为例子:

/**
 * An interpolator where the rate of change starts out slowly and 
 * and then accelerates.
 *
 */
public class AccelerateInterpolator implements Interpolator {
    private final float mFactor;
    private final double mDoubleFactor;

    public AccelerateInterpolator() {
        mFactor = 1.0f;
        mDoubleFactor = 2.0;
    }
    
    /**
     * Constructor
     * 
     * @param factor Degree to which the animation should be eased. Seting
     *        factor to 1.0f produces a y=x^2 parabola. Increasing factor above
     *        1.0f  exaggerates the ease-in effect (i.e., it starts even
     *        slower and ends evens faster)
     */
    public AccelerateInterpolator(float factor) {
        mFactor = factor;
        mDoubleFactor = 2 * mFactor;
    }
    
    public AccelerateInterpolator(Context context, AttributeSet attrs) {
        TypedArray a =
            context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.AccelerateInterpolator);
        
        mFactor = a.getFloat(com.android.internal.R.styleable.AccelerateInterpolator_factor, 1.0f);
        mDoubleFactor = 2 * mFactor;

        a.recycle();
    }
    
    public float getInterpolation(float input) {
        if (mFactor == 1.0f) {
            return input * input;
        } else {
            return (float)Math.pow(input, mDoubleFactor);
        }
    }
}

这个Interpolation的效果是一个匀加速。

 

posted @ 2015-05-08 17:11  myjhaha  阅读(199)  评论(0编辑  收藏  举报