android:interpolator

 

Interpolator 被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果accelerated(加速),decelerated(减速),repeated(重复),bounced(弹跳)等。

android中的文档内容如下:

![](http://img.blog.csdn.net/20131117161350500?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamFzb24wNTM5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)





AccelerateDecelerateInterpolator 在动画开始与结束的地方速率改变比较慢,在中间的时候加速





AccelerateInterpolator  在动画开始的地方速率改变比较慢,然后开始加速





AnticipateInterpolator 开始的时候向后然后向前甩





AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值





BounceInterpolator   动画结束的时候弹起





CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线





DecelerateInterpolator 在动画开始的地方快然后慢





LinearInterpolator   以常量速率改变





OvershootInterpolator    向前甩一定值后再回到原来位置





如果android定义的interpolators不符合你的效果也可以自定义interpolators





 

Android中的Interpolator

nterpolator用于动画中的时间插值,其作用就是把0到1的浮点值变化映射到另一个浮点值变化。





本文列出Android API提供的Interpolator的若干种实现,列出源码,并且用一个程序绘制出其数学曲线。(项目链接附在文后)。

AccelerateDecelerateInterpolator

```

/**

  • An interpolator where the rate of change starts and ends slowly but
  • accelerates through the middle.

*/ public class AccelerateDecelerateInterpolator implements Interpolator { public AccelerateDecelerateInterpolator() { }

@SuppressWarnings({"UnusedDeclaration"})
public AccelerateDecelerateInterpolator(Context context, AttributeSet attrs) {
}

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

}

    
    <div class="cnblogs_code_toolbar">
      <span class="cnblogs_code_copy"><a title="复制代码">![复制代码](http://common.cnblogs.com/images/copycode.gif)</a></span>
    </div>
  </div>
  
  

    &nbsp;
  

  
  

    ![](http://images.cnitblog.com/blog/325852/201309/29162553-398804596d514dd795c2f19539148ca2.png)
  

  
  

    &nbsp;
  

  
  ## AccelerateInterpolator
  
  <div class="cnblogs_code">
    <div class="cnblogs_code_toolbar">
      <span class="cnblogs_code_copy"><a title="复制代码">![复制代码](http://common.cnblogs.com/images/copycode.gif)</a></span>
    </div>
    
    ```
/**
 * An interpolator where the rate of change starts out slowly and 
 * and then accelerates.
 *
 */
public class AccelerateInterpolator implements Interpolator {
    private final float mFactor;
    private final double mDoubleFactor;

    public AccelerateInterpolator() {
        mFactor = 1.0f;
        mDoubleFactor = 2.0;
    }
    
    /**
     * Constructor
     * 
     * @param factor Degree to which the animation should be eased. Seting
     *        factor to 1.0f produces a y=x^2 parabola. Increasing factor above
     *        1.0f  exaggerates the ease-in effect (i.e., it starts even
     *        slower and ends evens faster)
     */
    public AccelerateInterpolator(float factor) {
        mFactor = factor;
        mDoubleFactor = 2 * mFactor;
    }
    
    public AccelerateInterpolator(Context context, AttributeSet attrs) {
        TypedArray a =
            context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.AccelerateInterpolator);
        
        mFactor = a.getFloat(com.android.internal.R.styleable.AccelerateInterpolator_factor, 1.0f);
        mDoubleFactor = 2 * mFactor;

        a.recycle();
    }
    
    public float getInterpolation(float input) {
        if (mFactor == 1.0f) {
            return input * input;
        } else {
            return (float)Math.pow(input, mDoubleFactor);
        }
    }
}
<div class="cnblogs_code_toolbar">
  <span class="cnblogs_code_copy"><a title="复制代码">![复制代码](http://common.cnblogs.com/images/copycode.gif)</a></span>
</div>
&nbsp;





![](http://images.cnitblog.com/blog/325852/201309/29162735-9d46b2da759e4b668a5209557b6ee115.png)

AnticipateInterpolator

```

/**

  • An interpolator where the change starts backward then flings forward. */ public class AnticipateInterpolator implements Interpolator { private final float mTension;

    public AnticipateInterpolator() { mTension = 2.0f; }

    /**

    • @param tension Amount of anticipation. When tension equals 0.0f, there is
    •            no anticipation and the interpolator becomes a simple
      
    •            acceleration interpolator.
      

    */ public AnticipateInterpolator(float tension) { mTension = tension; }

    public AnticipateInterpolator(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.AnticipateInterpolator);

     mTension =
             a.getFloat(com.android.internal.R.styleable.AnticipateInterpolator_tension, 2.0f);
    
     a.recycle();
    

    }

    public float getInterpolation(float t) { // a(t) = t * t * ((tension + 1) * t - tension) return t * t * ((mTension + 1) * t - mTension); } }

    
    <div class="cnblogs_code_toolbar">
      <span class="cnblogs_code_copy"><a title="复制代码">![复制代码](http://common.cnblogs.com/images/copycode.gif)</a></span>
    </div>
  </div>
  
  

    ![](http://images.cnitblog.com/blog/325852/201309/29162936-630167d6b6514c6c9fb8f8446b5cd251.png)
  

  
  

    &nbsp;
  

  
  

    &nbsp;
  

  
  ## AnticipateOvershootInterpolator
  
  <div class="cnblogs_code">
    <div class="cnblogs_code_toolbar">
      <span class="cnblogs_code_copy"><a title="复制代码">![复制代码](http://common.cnblogs.com/images/copycode.gif)</a></span>
    </div>
    
    ```
/**
 * An interpolator where the change starts backward then flings forward and overshoots
 * the target value and finally goes back to the final value.
 */
public class AnticipateOvershootInterpolator implements Interpolator {
    private final float mTension;

    public AnticipateOvershootInterpolator() {
        mTension = 2.0f * 1.5f;
    }

    /**
     * @param tension Amount of anticipation/overshoot. When tension equals 0.0f,
     *                there is no anticipation/overshoot and the interpolator becomes
     *                a simple acceleration/deceleration interpolator.
     */
    public AnticipateOvershootInterpolator(float tension) {
        mTension = tension * 1.5f;
    }

    /**
     * @param tension Amount of anticipation/overshoot. When tension equals 0.0f,
     *                there is no anticipation/overshoot and the interpolator becomes
     *                a simple acceleration/deceleration interpolator.
     * @param extraTension Amount by which to multiply the tension. For instance,
     *                     to get the same overshoot as an OvershootInterpolator with
     *                     a tension of 2.0f, you would use an extraTension of 1.5f.
     */
    public AnticipateOvershootInterpolator(float tension, float extraTension) {
        mTension = tension * extraTension;
    }

    public AnticipateOvershootInterpolator(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, AnticipateOvershootInterpolator);

        mTension = a.getFloat(AnticipateOvershootInterpolator_tension, 2.0f) *
                a.getFloat(AnticipateOvershootInterpolator_extraTension, 1.5f);

        a.recycle();
    }

    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 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 &lt; 0.5
        // f(t) = 0.5 * (o(t * 2 - 2, tension * extraTension) + 2), when t &lt;= 1.0
        if (t &lt; 0.5f) return 0.5f * a(t * 2.0f, mTension);
        else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);
    }
}
<div class="cnblogs_code_toolbar">
  <span class="cnblogs_code_copy"><a title="复制代码">![复制代码](http://common.cnblogs.com/images/copycode.gif)</a></span>
</div>
![](http://images.cnitblog.com/blog/325852/201309/29163126-ca9aa8f0018e46ec839430f3c818a0e8.png)

BounceInterpolator

```

/**

  • An interpolator where the change bounces at the end. */ public class BounceInterpolator implements Interpolator { public BounceInterpolator() { }

    @SuppressWarnings({“UnusedDeclaration”}) public BounceInterpolator(Context context, AttributeSet attrs) { }

    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; } }

    
    <div class="cnblogs_code_toolbar">
      <span class="cnblogs_code_copy"><a title="复制代码">![复制代码](http://common.cnblogs.com/images/copycode.gif)</a></span>
    </div>
  </div>
  
  

    &nbsp;
  

  
  

    ![](http://images.cnitblog.com/blog/325852/201309/29163251-7079b1bb27c844aa8d007e6899688c2b.png)
  

  
  ## CycleInterpolator
  
  <div class="cnblogs_code">
    <div class="cnblogs_code_toolbar">
      <span class="cnblogs_code_copy"><a title="复制代码">![复制代码](http://common.cnblogs.com/images/copycode.gif)</a></span>
    </div>
    
    ```
/**
 * Repeats the animation for a specified number of cycles. The
 * rate of change follows a sinusoidal pattern.
 *
 */
public class CycleInterpolator implements Interpolator {
    public CycleInterpolator(float cycles) {
        mCycles = cycles;
    }
    
    public CycleInterpolator(Context context, AttributeSet attrs) {
        TypedArray a =
            context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.CycleInterpolator);
        
        mCycles = a.getFloat(com.android.internal.R.styleable.CycleInterpolator_cycles, 1.0f);
        
        a.recycle();
    }
    
    public float getInterpolation(float input) {
        return (float)(Math.sin(2 * mCycles * Math.PI * input));
    }
    
    private float mCycles;
}
<div class="cnblogs_code_toolbar">
  <span class="cnblogs_code_copy"><a title="复制代码">![复制代码](http://common.cnblogs.com/images/copycode.gif)</a></span>
</div>
&nbsp;





参数为2时的曲线:





![](http://images.cnitblog.com/blog/325852/201309/29163456-495a2721492946f4a4d894443491e4d6.png)





&nbsp;

DecelerateInterpolator

```

/**

  • An interpolator where the rate of change starts out quickly and
  • and then decelerates.

*/ public class DecelerateInterpolator implements Interpolator { 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) {
    TypedArray a =
        context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.DecelerateInterpolator);
    
    mFactor = a.getFloat(com.android.internal.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;

}

    
    <div class="cnblogs_code_toolbar">
      <span class="cnblogs_code_copy"><a title="复制代码">![复制代码](http://common.cnblogs.com/images/copycode.gif)</a></span>
    </div>
  </div>
  
  

    &nbsp;
  

  
  

    ![](http://images.cnitblog.com/blog/325852/201309/29163621-d9ecec56e54a4416a3cbed447f36379b.png)
  

  
  ## LinearInterpolator
  
  <div class="cnblogs_code">
    <div class="cnblogs_code_toolbar">
      <span class="cnblogs_code_copy"><a title="复制代码">![复制代码](http://common.cnblogs.com/images/copycode.gif)</a></span>
    </div>
    
    ```
/**
 * An interpolator where the rate of change is constant
 *
 */
public class LinearInterpolator implements Interpolator {

    public LinearInterpolator() {
    }
    
    public LinearInterpolator(Context context, AttributeSet attrs) {
    }
    
    public float getInterpolation(float input) {
        return input;
    }
}
<div class="cnblogs_code_toolbar">
  <span class="cnblogs_code_copy"><a title="复制代码">![复制代码](http://common.cnblogs.com/images/copycode.gif)</a></span>
</div>
&nbsp;





![](http://images.cnitblog.com/blog/325852/201309/29163734-b31e9a2dcf84435288ba9188ca81c05a.png)

OvershootInterpolator

```

/**

  • An interpolator where the change flings forward and overshoots the last value

  • then comes back. */ public class OvershootInterpolator implements Interpolator { private final float mTension;

    public OvershootInterpolator() { mTension = 2.0f; }

    /**

    • @param tension Amount of overshoot. When tension equals 0.0f, there is
    •            no overshoot and the interpolator becomes a simple
      
    •            deceleration interpolator.
      

    */ public OvershootInterpolator(float tension) { mTension = tension; }

    public OvershootInterpolator(Context context, AttributeSet attrs) { TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.OvershootInterpolator);

     mTension =
             a.getFloat(com.android.internal.R.styleable.OvershootInterpolator_tension, 2.0f);
    
     a.recycle();
    

    }

    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; } }

    
    <div class="cnblogs_code_toolbar">
      <span class="cnblogs_code_copy"><a title="复制代码">![复制代码](http://common.cnblogs.com/images/copycode.gif)</a></span>
    </div>
  </div>
  
  

    &nbsp;
  

  
  

    ![](http://images.cnitblog.com/blog/325852/201309/29163839-66d3e2c296814c20848807cd05ae63f1.png)
  

  
  

    &nbsp;
  

  
  ## 项目链接:
  
  

    [https://github.com/mengdd/HelloInterpolator.git](https://github.com/mengdd/HelloInterpolator.git)
  

💬 评论