android 动画的Interpolator插值器

本文详细介绍了Android中Interpolator时间插值器的概念及其多种常用子类的功能特性,包括加速、减速等效果实现方式,并提供了部分插值器的具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Interpolator 时间插值类,定义动画变换的速度。能够实现alpha/scale/translate/rotate动画的加速、减速和重复等。Interpolator类其实是一个空接口,继承自TimeInterpolator,TimeInterpolator时间插值器允许动画进行非线性运动变换,如加速和限速等,该接口中只有接口中有一个方法 float getInterpolation(float input)这个方法。传入的值是一个0.0~1.0的值,返回值可以小于0.0也可以大于1.0。






常用继承类


  1. AccelerateDecelerateInterpolator============动画开始与结束的地方速率改变比较慢,在中间的时候加速。
  2. AccelerateInterpolator===================动画开始的地方速率改变比较慢,然后开始加速。 
  3. AnticipateInterpolator ==================开始的时候向后然后向前甩。
  4. AnticipateOvershootInterpolator=============开始的时候向后然后向前甩一定值后返回最后的值。
  5. BounceInterpolator=====================动画结束的时候弹起。
  6. CycleInterpolator======================动画循环播放特定的次数,速率改变沿着正弦曲线。
  7. DecelerateInterpolator===================在动画开始的地方快然后慢。
  8. LinearInterpolator======================以常量速率改变。
  9. OvershootInterpolator====================向前甩一定值后再回到原来位置。
  10. PathInterpolator========================新增的,就是可以定义路径坐标,然后可以按照路径坐标来跑动;注意其坐标并不是 XY,而是单方向,也就是我可以从0~1,然后弹回0.5 然后又弹到0.7 有到0.3,直到最后时间结束。

几个常用插值器源码:

LinearInterpolator线性插值器,直接返回输入值。

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * An interpolator where the rate of change is constant 
  3.  * 
  4.  */  
  5. @HasNativeInterpolator  
  6. public class LinearInterpolator implements Interpolator, NativeInterpolatorFactory {  
  7.   
  8.     public LinearInterpolator() {  
  9.     }  
  10.       
  11.     public LinearInterpolator(Context context, AttributeSet attrs) {  
  12.     }  
  13.       
  14.     public float getInterpolation(float input) {  
  15.         return input;  
  16.     }  
  17.   
  18.     /** @hide */  
  19.     @Override  
  20.     public long createNativeInterpolator() {  
  21.         return NativeInterpolatorFactoryHelper.createLinearInterpolator();  
  22.     }  
  23. }  




DecelerateInterpolator

可以通过 XML 进行动画属性设置,通过 XML 可以设置其中的 mFactor 变量,其值默认是1.0;值越大其变化越快;得到的结果就是,开始的时候更加的快,其结果就是更加的慢。getInterpolation(float)描述的是一个初中学的抛物方程。



[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * An interpolator where the rate of change starts out quickly and 
  3.  * and then decelerates. 
  4.  * 
  5.  */  
  6. @HasNativeInterpolator  
  7. public class DecelerateInterpolator implements Interpolator, NativeInterpolatorFactory {  
  8.     public DecelerateInterpolator() {  
  9.     }  
  10.   
  11.     /** 
  12.      * Constructor 
  13.      * 
  14.      * @param factor Degree to which the animation should be eased. Setting factor to 1.0f produces 
  15.      *        an upside-down y=x^2 parabola. Increasing factor above 1.0f makes exaggerates the 
  16.      *        ease-out effect (i.e., it starts even faster and ends evens slower) 
  17.      */  
  18.     public DecelerateInterpolator(float factor) {  
  19.         mFactor = factor;  
  20.     }  
  21.   
  22.     public DecelerateInterpolator(Context context, AttributeSet attrs) {  
  23.         this(context.getResources(), context.getTheme(), attrs);  
  24.     }  
  25.   
  26.     /** @hide */  
  27.     public DecelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) {  
  28.         TypedArray a;  
  29.         if (theme != null) {  
  30.             a = theme.obtainStyledAttributes(attrs, R.styleable.DecelerateInterpolator, 00);  
  31.         } else {  
  32.             a = res.obtainAttributes(attrs, R.styleable.DecelerateInterpolator);  
  33.         }  
  34.   
  35.         mFactor = a.getFloat(R.styleable.DecelerateInterpolator_factor, 1.0f);  
  36.   
  37.         a.recycle();  
  38.     }  
  39.   
  40.     public float getInterpolation(float input) {  
  41.         float result;  
  42.         if (mFactor == 1.0f) {  
  43.             result = (float)(1.0f - (1.0f - input) * (1.0f - input));  
  44.         } else {  
  45.             result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor));  
  46.         }  
  47.         return result;  
  48.     }  
  49.   
  50.     private float mFactor = 1.0f;  
  51.   
  52.     /** @hide */  
  53.     @Override  
  54.     public long createNativeInterpolator() {  
  55.         return NativeInterpolatorFactoryHelper.createDecelerateInterpolator(mFactor);  
  56.     }  
  57. }  






C++ 的版本实现 :

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. float Elastic::easeIn (float t, float b , float c, float d) {  
  2.  <span style="white-space:pre">   </span>if (t==0) return b;  if ((t/=d)==1) return b+c;    
  3.  <span style="white-space:pre">   </span>float p=d*.3f;  
  4.  <span style="white-space:pre">   </span>float a=c;   
  5.  <span style="white-space:pre">   </span>float s=p/4;  
  6.  <span style="white-space:pre">   </span>float postFix =a*pow(2,10*(t-=1)); // this is a fix, again, with post-increment operators  
  7.  <span style="white-space:pre">   </span>return -(postFix * sin((t*d-s)*(2*PI)/p )) + b;  
  8. }  
  9.    
  10. float Elastic::easeOut(float t,float b , float c, float d) {  
  11.  if (t==0) return b;  if ((t/=d)==1) return b+c;    
  12.  float p=d*.3f;  
  13.  float a=c;   
  14.  float s=p/4;  
  15.  return (a*pow(2,-10*t) * sin( (t*d-s)*(2*PI)/p ) + c + b);   
  16. }  
  17.    
  18. float Elastic::easeInOut(float t,float b , float c, float d) {  
  19.  if (t==0) return b;  if ((t/=d/2)==2) return b+c;   
  20.  float p=d*(.3f*1.5f);  
  21.  float a=c;   
  22.  float s=p/4;  
  23.    
  24.  if (t < 1) {  
  25.  float postFix =a*pow(2,10*(t-=1)); // postIncrement is evil  
  26.  return -.5f*(postFix* sin( (t*d-s)*(2*PI)/p )) + b;  
  27.  }   
  28.  float postFix =  a*pow(2,-10*(t-=1)); // postIncrement is evil  
  29.  return postFix * sin( (t*d-s)*(2*PI)/p )*.5f + c + b;  
  30. }  
参数的意思:

  • t – 动画中当前的时间
  • b – 开始值
  • c – 结束值
  • d – 动画的总时间
看下Java的第一行前三个的:

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. public class Sine {  
  2.       
  3.     public static float  easeIn(float t,float b , float c, float d) {  
  4.         return -c * (float)Math.cos(t/d * (Math.PI/2)) + c + b;  
  5.     }  
  6.       
  7.     public static float  easeOut(float t,float b , float c, float d) {  
  8.         return c * (float)Math.sin(t/d * (Math.PI/2)) + b;    
  9.     }  
  10.       
  11.     public static float  easeInOut(float t,float b , float c, float d) {  
  12.         return -c/2 * ((float)Math.cos(Math.PI*t/d) - 1) + b;  
  13.     }  
  14.       
  15. }  
虽然 Java 的也有了,但是话说这个怎么用啊,跟上面的Interpolator如何联系起来啊?

一个简单的方法:首先把 d 总时间设置为固定值 1.0 ,把 b 开始值设置为 0.0 把结束值设置为1.0,然后把 t 当作上面 Interpolator 中的 float getInterpolation(float input);传入值,此时不就能用上了。

举个Case

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Created by Qiujuer on 2015/1/5. 
  3.  */  
  4. public class InSineInterpolator implements Interpolator{  
  5.     public static float  easeIn(float t,float b , float c, float d) {  
  6.         return -c * (float)Math.cos(t/d * (Math.PI/2)) + c + b;  
  7.     }  
  8.   
  9.     @Override  
  10.     public float getInterpolation(float input) {  
  11.         return easeIn(input, 011);  
  12.     }  
  13. }  

使用

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. //AnimatorSet  
  2. mAnimatorSet = new AnimatorSet();  
  3. mAnimatorSet.playTogether(aPaintX, aPaintY, aRadius, aBackground);  
  4. mAnimatorSet.setInterpolator(new InSineInterpolator());  
  5. mAnimatorSet.start();  
可以看出使用与上面 Android 自带的完全一样,当然这个只是个 Case ,具体使用中你可以随意封装,前提是别改动了主要部分。

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * An interpolator where the rate of change starts out quickly and 
  3.  * and then decelerates. 
  4.  * 
  5.  */  
  6. @HasNativeInterpolator  
  7. public class DecelerateInterpolator implements Interpolator, NativeInterpolatorFactory {  
  8.     public DecelerateInterpolator() {  
  9.     }  
  10.   
  11.     /** 
  12.      * Constructor 
  13.      * 
  14.      * @param factor Degree to which the animation should be eased. Setting factor to 1.0f produces 
  15.      *        an upside-down y=x^2 parabola. Increasing factor above 1.0f makes exaggerates the 
  16.      *        ease-out effect (i.e., it starts even faster and ends evens slower) 
  17.      */  
  18.     public DecelerateInterpolator(float factor) {  
  19.         mFactor = factor;  
  20.     }  
  21.   
  22.     public DecelerateInterpolator(Context context, AttributeSet attrs) {  
  23.         this(context.getResources(), context.getTheme(), attrs);  
  24.     }  
  25.   
  26.     /** @hide */  
  27.     public DecelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) {  
  28.         TypedArray a;  
  29.         if (theme != null) {  
  30.             a = theme.obtainStyledAttributes(attrs, R.styleable.DecelerateInterpolator, 00);  
  31.         } else {  
  32.             a = res.obtainAttributes(attrs, R.styleable.DecelerateInterpolator);  
  33.         }  
  34.   
  35.         mFactor = a.getFloat(R.styleable.DecelerateInterpolator_factor, 1.0f);  
  36.   
  37.         a.recycle();  
  38.     }  
  39.   
  40.     public float getInterpolation(float input) {  
  41.         float result;  
  42.         if (mFactor == 1.0f) {  
  43.             result = (float)(1.0f - (1.0f - input) * (1.0f - input));  
  44.         } else {  
  45.             result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor));  
  46.         }  
  47.         return result;  
  48.     }  
  49.   
  50.     private float mFactor = 1.0f;  
  51.   
  52.     /** @hide */  
  53.     @Override  
  54.     public long createNativeInterpolator() {  
  55.         return NativeInterpolatorFactoryHelper.createDecelerateInterpolator(mFactor);  
  56.     }  
  57. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值