美文网首页
属性动画-插值器

属性动画-插值器

作者: MrLgc | 来源:发表于2018-11-09 16:48 被阅读0次

    TimeInterpolator
    时间插值器,一个接口类
    float getInterpolation(float input);
    系统会根据动画当前时间和动画总时长,计算时间进度fraction,fraction为float类型,值范围为0f~1f,该值是随着时间流逝匀速变化的。我们将 fraction 作为参数input传入getInterpolation(float input)方法:

    float getInterpolation(float input);
    interface Interpolator 继承 TimeInterpolator
    BaseInterpolator implements Interpolator
    形成了BaseInterpolator
    BaseInterpolator 这里面会有
    private @Config int mChangingConfiguration;
    这个干嘛的呢?不清楚

    然后我们会拿getInterpolation(float input)的input做插值器处理

    1,以下是系统提供的插值器
    1,LinearInterpolator 线性插值器

     public float getInterpolation(float input) {
            return input;
        }
    

    2,AccelerateInterpolator 加速插值器
    y = t^2f

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

    3,DecelerateInterpolator 减速插值器
    y = 1 - (1 - t)^2f

     public DecelerateInterpolator(float factor) {
            mFactor = factor;
        }
    
    public float getInterpolation(float input) {
            float result;
            if (mFactor == 1.0f) {
                result = (float)(1.0f - (1.0f - input) * (1.0f - input));
            } else {
                result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor));
            }
            return result;
        }
    

    4,AccelerateDecelerateInterpolator 加速减速插值器
    y = cos((t + 1)π)/2 + 0.5

     public AccelerateDecelerateInterpolator() {
        }
    
    public float getInterpolation(float input) {
            return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
        }
    

    5,BounceInterpolator 弹跳插值器

     public BounceInterpolator(Context context, AttributeSet attrs) {
        }
    
     public float getInterpolation(float t) {
            // _b(t) = t * t * 8
            // bs(t) = _b(t) for t < 0.3535
            // bs(t) = _b(t - 0.54719) + 0.7 for t < 0.7408
            // bs(t) = _b(t - 0.8526) + 0.9 for t < 0.9644
            // bs(t) = _b(t - 1.0435) + 0.95 for t <= 1.0
            // b(t) = bs(t * 1.1226)
            t *= 1.1226f;
            if (t < 0.3535f) return bounce(t);
            else if (t < 0.7408f) return bounce(t - 0.54719f) + 0.7f;
            else if (t < 0.9644f) return bounce(t - 0.8526f) + 0.9f;
            else return bounce(t - 1.0435f) + 0.95f;
        }
    

    6,AnticipateInterpolator 回荡秋千插值器
    y = (T + 1) * t^3 - T * t^2

     public AnticipateInterpolator(float tension) {
            mTension = tension;
        }
    
     public float getInterpolation(float t) {
            // a(t) = t * t * ((tension + 1) * t - tension)
            return t * t * ((mTension + 1) * t - mTension);
        }
    

    7,AnticipateOvershootInterpolator 回荡秋千加超过末尾插值器

     public AnticipateOvershootInterpolator(float tension) {
            mTension = tension * 1.5f;
        }
    
        /**
         * @param tension Amount of anticipation/overshoot. When tension equals 0.0f,
         *                there is no anticipation/overshoot and the interpolator becomes
         *                a simple acceleration/deceleration interpolator.
         * @param extraTension Amount by which to multiply the tension. For instance,
         *                     to get the same overshoot as an OvershootInterpolator with
         *                     a tension of 2.0f, you would use an extraTension of 1.5f.
         */
        public AnticipateOvershootInterpolator(float tension, float extraTension) {
            mTension = tension * extraTension;
        }
    
        private static float a(float t, float s) {
            return t * t * ((s + 1) * t - s);
        }
    
        private static float o(float t, float s) {
            return t * t * ((s + 1) * t + s);
        }
    
        public float getInterpolation(float t) {
               // a(t, s) = t * t * ((s + 1) * t - s)
            // o(t, s) = t * t * ((s + 1) * t + s)
            // f(t) = 0.5 * a(t * 2, tension * extraTension), when t < 0.5
            // f(t) = 0.5 * (o(t * 2 - 2, tension * extraTension) + 2), when t <= 1.0
            if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension);
            else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);
         }
    

    8,CycleInterpolator 正弦周期变化插值器
    y = sin(2π * c)

     public CycleInterpolator(float cycles) {
            mCycles = cycles;
        }
    
     public float getInterpolation(float input) {
            return (float)(Math.sin(2 * mCycles * Math.PI * input));
        }
    

    9,OvershootInterpolator 超过末尾拦截器

    public OvershootInterpolator(float tension) {
            mTension = tension;
        }
    
    public float getInterpolation(float t) {
            // _o(t) = t * t * ((tension + 1) * t + tension)
            // o(t) = _o(t - 1) + 1
            t -= 1.0f;
            return t * t * ((mTension + 1) * t + mTension) + 1.0f;
        }
    

    2,自定义插值器

     private class CusInterInterpolator implements Interpolator{
    
            @Override
            public float getInterpolation(float input) {
                //你想要的处理
                if (input <= 0.5) {
                    return (float) (Math.sin(Math.PI * input)) / 2;
                } else {
                    return (float) (2 - Math.sin(Math.PI * input)) / 2;
                }
            }
        }
    

    3,使用插值器

    objectAnimator.setInterpolator()
    

    相关文章

      网友评论

          本文标题:属性动画-插值器

          本文链接:https://www.haomeiwen.com/subject/szpnxqtx.html