android动画插值器

在android的动画中,有一个插值器,他的作用是告诉动画某个属性(比如颜色的渐变)如何随时间变化。它将以线性方式变化,还是以指数方式变化?它是否在开始时变化速度很快,然后逐渐变慢?使用方法是在需

要设置插值器的动画xml文件中增加一个interpoplator属性,如下:
android:interpoplator="@android:anim/accelerate_interpoplator"
android:fromAlpha="0.0" 
android:toAlpha="1.0"
android:duration="1000" />
 
这样就可以使用了,在从完全透明变化为不透明的过程中,你可以在res/anim/accelerate_interpoplator.xml中定义自己想要的效果,如变化速率等。



下面给出了几个插值器的源码,供参考:

AccelerateDecelerateInterpolator(); 
注解:An interpolator where the rate of change starts and ends slowly but accelerates through the middle.
源码:public float getInterpolation(float input) {

        return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;

    }





AccelerateInterpolator(float mFactor); 
注解:An interpolator where the rate of change starts out slowly and and then accelerates.
源码: public float getInterpolation(float input) {

        if (mFactor == 1.0f) {

            return input * input;

        } else {

            return (float)Math.pow(input, mDoubleFactor);

        }

    }





CycleInterpolator(float cycles); 
注解:Repeats the animation for a specified number of cycles. The rate of change follows a sinusoidal pattern.
源码:public float getInterpolation(float input) {

        return (float)(Math.sin(2 * mCycles * Math.PI * input));

    }





DecelerateInterpolator(); 
注解:An interpolator where the rate of change starts out quickly and and then decelerates.
源码: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;

    }






AnticipateInterpolator(); 
注解:An interpolator where the change starts backward then flings forward.
源码:public float getInterpolation(float t) {

        // a(t) = t * t * ((tension + 1) * t - tension)

        return t * t * ((mTension + 1) * t - mTension);

    }





AnticipateOvershootInterpolator(); 
注解:An interpolator where the change starts backward then flings forward and overshoots the target value and finally goes back to the final value.
源码: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);

    }

     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 AnticipateOvershootInterpolator(float tension) {

        mTension = tension * 1.5f;

    }




BounceInterpolator(); 
注解:An interpolator where the change bounces at the end.
源码:private static float bounce(float t) {

        return t * t * 8.0f;

    }

 

              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;

    }





OvershootInterpolator(); 
注解:An interpolator where the change flings forward and overshoots the last value then comes back.
源码: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;

    }





LinearInterpolator();
注解:An interpolator where the rate of change is constant
源码:public float getInterpolation(float input) {

        return input;

    }



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值