Interpolator——各种插入器效果图

转载:http://blog.csdn.net/farmer_cc/article/details/18259117

3. 插值器

从上面的介绍可以看到,Interpolator的关键是getInterpolation();

在ValueAnimator.animationFrame()中可以看到, 传递给Interpolator 的fraction是在[0,1] 值域范围。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. float fraction = mDuration > 0 ? (float)(currentTime - mStartTime) / mDuration : 1f;  
  2. if (fraction >= 1f) {  
  3.     if (mCurrentIteration < mRepeatCount || mRepeatCount == INFINITE) {  
  4.         // Time to repeat  
  5.         if (mListeners != null) {  
  6.             int numListeners = mListeners.size();  
  7.             for (int i = 0; i < numListeners; ++i) {  
  8.                 mListeners.get(i).onAnimationRepeat(this);  
  9.             }  
  10.         }  
  11.         if (mRepeatMode == REVERSE) {  
  12.             mPlayingBackwards = !mPlayingBackwards;  
  13.         }  
  14.         mCurrentIteration += (int)fraction;  
  15.         fraction = fraction % 1f;  
  16.         mStartTime += mDuration;  
  17.     } else {  
  18.         done = true;  
  19.         fraction = Math.min(fraction, 1.0f);  
  20.     }  
  21. }  
  22. if (mPlayingBackwards) {  
  23.     fraction = 1f - fraction;  
  24. }  

所以设计Interpolator 就是设计一个输入[0,1] 的函数。

先参观一下系统的几个Interpolator。

3.1 AccelerateDecelerateInterpolator 

cos(t+1)Pi /2 +0.5f

从图可以看到,先加速后减速,病最终到达结束位置。


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class AccelerateDecelerateInterpolator implements Interpolator {  
  2.     public float getInterpolation(float input) {  
  3.         return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;  
  4.     }  
  5. }  


3.2 AccelerateInterpolator 

如果factor=1 则函数为x^2

否则函数为x^a (a 是参数)

默认函数式x^2

如图示,逐渐加速到结束位置。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class AccelerateInterpolator implements Interpolator {  
  2.     private final float mFactor;  
  3.     private final double mDoubleFactor;  
  4.   
  5.     public AccelerateInterpolator() {  
  6.         mFactor = 1.0f;  
  7.         mDoubleFactor = 2.0;  
  8.     }  
  9.       
  10.     /** 
  11.      * Constructor 
  12.      *  
  13.      * @param factor Degree to which the animation should be eased. Seting 
  14.      *        factor to 1.0f produces a y=x^2 parabola. Increasing factor above 
  15.      *        1.0f  exaggerates the ease-in effect (i.e., it starts even 
  16.      *        slower and ends evens faster) 
  17.      */  
  18.     public AccelerateInterpolator(float factor) {  
  19.         mFactor = factor;  
  20.         mDoubleFactor = 2 * mFactor;  
  21.     }  
  22.   
  23.      public float getInterpolation(float input) {  
  24.         if (mFactor == 1.0f) {  
  25.             return input * input;  
  26.         } else {  
  27.             return (float)Math.pow(input, mDoubleFactor);  
  28.         }  
  29.     }  
  30. }  



3.3 LinearInterpolator 

线性的就是Y=X 没啥说的。



[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class LinearInterpolator implements Interpolator {  
  2.     public float getInterpolation(float input) {  
  3.         return input;  
  4.     }  
  5. }  


3.4 anticipateInterpolator  

函数是:x^2((a+1)x-a) 默认参数a=2 默认函数为x^2(3x-1)

如图示, 会先反方向执行一段,然后正向一直加速至结束位置。


[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class AnticipateInterpolator implements Interpolator {  
  2.     private final float mTension;  
  3.   
  4.     public AnticipateInterpolator() {  
  5.         mTension = 2.0f;  
  6.     }  
  7.   
  8.     /** 
  9.      * @param tension Amount of anticipation. When tension equals 0.0f, there is 
  10.      *                no anticipation and the interpolator becomes a simple 
  11.      *                acceleration interpolator. 
  12.      */  
  13.     public AnticipateInterpolator(float tension) {  
  14.         mTension = tension;  
  15.     }  
  16.   
  17.     public float getInterpolation(float t) {  
  18.         // a(t) = t * t * ((tension + 1) * t - tension)  
  19.         return t * t * ((mTension + 1) * t - mTension);  
  20.     }  
  21. }  



3.5 aniticipateOvershoot 

是一个分段函数,默认参数a=3

2x*x[(2x*(a+1)-a)]     0<=x<=0.5

2(x-1)(x-1)[(2x-1)(a+1)+a]    0.5<x<=1

通过下图可以看到,动画会先反方向执行,然后向正方向逐渐加速,在快结束时逐渐减速,并超过预设的值,最后回到结束位置。


2x*x[(2x*(a+1)-a)]     0<=x<=0.5 的函数图



2(x-1)(x-1)[(2x-1)(a+1)+a]    0.5<x<=1的函数图

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class AnticipateOvershootInterpolator implements Interpolator {  
  2.     private final float mTension;  
  3.   
  4.     public AnticipateOvershootInterpolator() {  
  5.         mTension = 2.0f * 1.5f;  
  6.     }  
  7.   
  8.     /** 
  9.      * @param tension Amount of anticipation/overshoot. When tension equals 0.0f, 
  10.      *                there is no anticipation/overshoot and the interpolator becomes 
  11.      *                a simple acceleration/deceleration interpolator. 
  12.      */  
  13.     public AnticipateOvershootInterpolator(float tension) {  
  14.         mTension = tension * 1.5f;  
  15.     }  
  16.   
  17.     /** 
  18.      * @param tension Amount of anticipation/overshoot. When tension equals 0.0f, 
  19.      *                there is no anticipation/overshoot and the interpolator becomes 
  20.      *                a simple acceleration/deceleration interpolator. 
  21.      * @param extraTension Amount by which to multiply the tension. For instance, 
  22.      *                     to get the same overshoot as an OvershootInterpolator with 
  23.      *                     a tension of 2.0f, you would use an extraTension of 1.5f. 
  24.      */  
  25.     public AnticipateOvershootInterpolator(float tension, float extraTension) {  
  26.         mTension = tension * extraTension;  
  27.     }  
  28.   
  29.     private static float a(float t, float s) {  
  30.         return t * t * ((s + 1) * t - s);  
  31.     }  
  32.   
  33.     private static float o(float t, float s) {  
  34.         return t * t * ((s + 1) * t + s);  
  35.     }  
  36.   
  37.     public float getInterpolation(float t) {  
  38.         // a(t, s) = t * t * ((s + 1) * t - s)  
  39.         // o(t, s) = t * t * ((s + 1) * t + s)  
  40.         // f(t) = 0.5 * a(t * 2, tension * extraTension), when t < 0.5  
  41.         // f(t) = 0.5 * (o(t * 2 - 2, tension * extraTension) + 2), when t <= 1.0  
  42.         if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension);  
  43.         else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);  
  44.     }  
  45. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值