首页 > 系统 > Android > 正文

android 动画的Interpolator插值器

2019-11-09 15:22:07
字体:
来源:转载
供稿:网友

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

常用继承类

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

几个常用插值器源码

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

[java] view plain copy PRint?在CODE上查看代码片/**  * An interpolator where the rate of change is constant  *  */  @HasNativeInterpolator  public class LinearInterpolator implements Interpolator, NativeInterpolatorFactory {        public LinearInterpolator() {      }            public LinearInterpolator(Context context, AttributeSet attrs) {      }            public float getInterpolation(float input) {          return input;      }        /** @hide */      @Override      public long createNativeInterpolator() {          return NativeInterpolatorFactoryHelper.createLinearInterpolator();      }  }  

DecelerateInterpolator

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

[java] view%20plain copy print?/**  * An interpolator where the rate of change starts out quickly and  * and then decelerates.  *  */  @HasNativeInterpolator  public class DecelerateInterpolator implements Interpolator, NativeInterpolatorFactory {      public DecelerateInterpolator() {      }        /**      * Constructor      *      * @param factor Degree to which the animation should be eased. Setting factor to 1.0f produces      *        an upside-down y=x^2 parabola. Increasing factor above 1.0f makes exaggerates the      *        ease-out effect (i.e., it starts even faster and ends evens slower)      */      public DecelerateInterpolator(float factor) {          mFactor = factor;      }        public DecelerateInterpolator(Context context, AttributeSet attrs) {          this(context.getResources(), context.getTheme(), attrs);      }        /** @hide */      public DecelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) {          TypedArray a;          if (theme != null) {              a = theme.obtainStyledAttributes(attrs, R.styleable.DecelerateInterpolator, 0, 0);          } else {              a = res.obtainAttributes(attrs, R.styleable.DecelerateInterpolator);          }            mFactor = a.getFloat(R.styleable.DecelerateInterpolator_factor, 1.0f);            a.recycle();      }        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;      }        private float mFactor = 1.0f;        /** @hide */      @Override      public long createNativeInterpolator() {          return NativeInterpolatorFactoryHelper.createDecelerateInterpolator(mFactor);      }  }  

C++%20的版本实现 :

[cpp] view%20plain copy print?float Elastic::easeIn (float t, float b , float c, float d) {   <span style="white-space:pre">   </span>if (t==0) return b;  if ((t/=d)==1) return b+c;     <span style="white-space:pre">   </span>float p=d*.3f;   <span style="white-space:pre">   </span>float a=c;    <span style="white-space:pre">   </span>float s=p/4;   <span style="white-space:pre">   </span>float postFix =a*pow(2,10*(t-=1)); // this is a fix, again, with post-increment Operators   <span style="white-space:pre">   </span>return -(postFix * sin((t*d-s)*(2*PI)/p )) + b;  }     float Elastic::eaSEOut(float t,float b , float c, float d) {   if (t==0) return b;  if ((t/=d)==1) return b+c;     float p=d*.3f;   float a=c;    float s=p/4;   return (a*pow(2,-10*t) * sin( (t*d-s)*(2*PI)/p ) + c + b);   }     float Elastic::easeInOut(float t,float b , float c, float d) {   if (t==0) return b;  if ((t/=d/2)==2) return b+c;    float p=d*(.3f*1.5f);   float a=c;    float s=p/4;      if (t < 1) {   float postFix =a*pow(2,10*(t-=1)); // postIncrement is evil   return -.5f*(postFix* sin( (t*d-s)*(2*PI)/p )) + b;   }    float postFix =  a*pow(2,-10*(t-=1)); // postIncrement is evil   return postFix * sin( (t*d-s)*(2*PI)/p )*.5f + c + b;  }  参数的意思:

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

[java] view%20plain copy print?public class Sine {            public static float  easeIn(float t,float b , float c, float d) {          return -c * (float)Math.cos(t/d * (Math.PI/2)) + c + b;      }            public static float  easeOut(float t,float b , float c, float d) {          return c * (float)Math.sin(t/d * (Math.PI/2)) + b;        }            public static float  easeInOut(float t,float b , float c, float d) {          return -c/2 * ((float)Math.cos(Math.PI*t/d) - 1) + b;      }        }  虽然%20Java%20的也有了,但是话说这个怎么用啊,跟上面的Interpolator如何联系起来啊?

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

举个Case

[java] view%20plain copy print?/**  * Created by Qiujuer on 2015/1/5.  */  public class InSineInterpolator implements Interpolator{      public static float  easeIn(float t,float b , float c, float d) {          return -c * (float)Math.cos(t/d * (Math.PI/2)) + c + b;      }        @Override      public float getInterpolation(float input) {          return easeIn(input, 0, 1, 1);      }  }  使用

[java] view%20plain copy print?//AnimatorSet  mAnimatorSet = new AnimatorSet();  mAnimatorSet.playTogether(aPaintX, aPaintY, aRadius, aBackground);  mAnimatorSet.setInterpolator(new InSineInterpolator());  mAnimatorSet.start();  可以看出使用与上面%20Android%20自带的完全一样,当然这个只是个%20Case%20,具体使用中你可以随意封装,前提是别改动了主要部分。[java] view%20plain copy print?/**  * An interpolator where the rate of change starts out quickly and  * and then decelerates.  *  */  @HasNativeInterpolator  public class DecelerateInterpolator implements Interpolator, NativeInterpolatorFactory {      public DecelerateInterpolator() {      }        /**      * Constructor      *      * @param factor Degree to which the animation should be eased. Setting factor to 1.0f produces      *        an upside-down y=x^2 parabola. Increasing factor above 1.0f makes exaggerates the      *        ease-out effect (i.e., it starts even faster and ends evens slower)      */      public DecelerateInterpolator(float factor) {          mFactor = factor;      }        public DecelerateInterpolator(Context context, AttributeSet attrs) {          this(context.getResources(), context.getTheme(), attrs);      }        /** @hide */      public DecelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) {          TypedArray a;          if (theme != null) {              a = theme.obtainStyledAttributes(attrs, R.styleable.DecelerateInterpolator, 0, 0);          } else {              a = res.obtainAttributes(attrs, R.styleable.DecelerateInterpolator);          }            mFactor = a.getFloat(R.styleable.DecelerateInterpolator_factor, 1.0f);            a.recycle();      }        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;      }        private float mFactor = 1.0f;        /** @hide */      @Override      public long createNativeInterpolator() {          return NativeInterpolatorFactoryHelper.createDecelerateInterpolator(mFactor);      }  }  
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表