插值器InterPolator & Evaluator

1、自定义Interpolator

     这是LinearInterpolator的源码

  1. public class LinearInterpolator implements Interpolator {  
  2.   
  3.     public LinearInterpolator() {  
  4.     }  
  5.   
  6.     public LinearInterpolator(Context context, AttributeSet attrs) {  
  7.     }  
  8.   
  9.     public float getInterpolation(float input) {  
  10.         return input;  
  11.     }  
  12. }  
  13. public interface Interpolator extends TimeInterpolator {  

      最终实现了TimeInterPolator接口

    

public interface TimeInterpolator {
    float getInterpolation(float var1);
}
float getInterpolation(float percent)这个方法的参数为当前动画的进度,范围0~1,这个进度只于时间有关,和其它没有关系。返回值表示当前实际想要显示的进度 取值可以超过1也可以小于0,超过1表示已经超过目标值,小于0表示小于开始位置

  1. ublic class LinearInterpolator implements Interpolator {  
  2.   
  3.     …………  
  4.   
  5.     public float getInterpolation(float input) {  
  6.         return input;  
  7.     }  
  8. }  、

  1. public class MyInterploator implements TimeInterpolator {  
  2.     @Override  
  3.     public float getInterpolation(float input) {  
  4.         return 1-input;  
  5.     }  

2、Evaluator

     我们先不讲什么是Evaluator,我们先来看一张图:


这幅图讲述了从定义动画的数字区间到通过AnimatorUpdateListener中得到当前动画所对应数值的整个过程。下面我们对这四个步骤具体讲解一下:
(1)、ofInt(0,400)表示指定动画的数字区间,是从0运动到400;
(2)、加速器:上面我们讲了,在动画开始后,通过加速器会返回当前动画进度所对应的数字进度,但这个数字进度是百分制的,以小数表示,如0.2
(3)、Evaluator:我们知道我们通过监听器拿到的是当前动画所对应的具体数值,而不是百分制的进度。那么就必须有一个地方会根据当前的数字进度,将其转化为对应的数值,这个地方就是Evaluator;Evaluator就是将从加速器返回的数字进度转成对应的数字值。所以上部分中,我们讲到的公式:

  1. 当前的值 = 100 + (400 - 100)* 显示进度  
这个公式就是在Evaluator计算的;在拿到当前数字进度所对应的值以后,将其返回
(4)、监听器:我们通过在AnimatorUpdateListener监听器使用animation.getAnimatedValue()函数拿到Evaluator中返回的数字值。

讲了这么多,Evaluator其实就是一个转换器,他能把小数进度转换成对应的数值位置

  1. /** 
  2.  * This evaluator can be used to perform type interpolation between <code>int</code> values. 
  3.  */  
  4. public class IntEvaluator implements TypeEvaluator<Integer> {  
  5.   
  6.     /** 
  7.      * This function returns the result of linearly interpolating the start and end values, with 
  8.      * <code>fraction</code> representing the proportion between the start and end values. The 
  9.      * calculation is a simple parametric calculation: <code>result = x0 + t * (v1 - v0)</code>, 
  10.      * where <code>x0</code> is <code>startValue</code>, <code>x1</code> is <code>endValue</code>, 
  11.      * and <code>t</code> is <code>fraction</code>. 
  12.      * 
  13.      * @param fraction   The fraction from the starting to the ending values 
  14.      * @param startValue The start value; should be of type <code>int</code> or 
  15.      *                   <code>Integer</code> 
  16.      * @param endValue   The end value; should be of type <code>int</code> or <code>Integer</code> 
  17.      * @return A linear interpolation between the start and end values, given the 
  18.      *         <code>fraction</code> parameter. 
  19.      */  
  20.     public Integer evaluate(float fraction, Integer startValue, Integer endValue) {  
  21.         int startInt = startValue;  
  22.         return (int)(startInt + fraction * (endValue - startInt));  
  23.     }  

可以看到在IntEvaluator中只有一个函数(float fraction, Integer startValue, Integer endValue) ;
其中fraction就是加速器中的返回值,表示当前动画的数值进度,百分制的小数表示。
startValue和endValue分别对应ofInt(int start,int end)中的start和end的数值;
比如我们假设当我们定义的动画ofInt(100,400)进行到数值进度20%的时候,那么此时在evaluate函数中,fraction的值就是0.2,startValue的值是100,endValue的值是400;理解上应该没什么难度。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值