interpolator(插值器)详解二

来源:互联网 发布:西西软件下载手机软件 编辑:程序博客网 时间:2024/06/05 03:52

6.AnticipateInterpolator 回荡秋千插值器

这个插值器的值变化过程,可以想像成荡秋千时的一个段过程。(此时秋千已经在比较上面的位置了,一放手就可以荡下来)。你开始用力推向更上面,然后秋千终将荡回下面。

tension值就好比推力的大小。

源代码如下:

01package android.view.animation;
02 
03import android.content.Context;
04import android.content.res.TypedArray;
05import android.util.AttributeSet;
06 
07/**
08 * 一个开始向后荡,然后向前荡的插值器。
09 * <hr/>
10 * An interpolator where the change starts backward then flings forward.
11 */
12public class AnticipateInterpolator implements Interpolator {
13    private final float mTension;
14 
15    public AnticipateInterpolator() {
16        mTension = 2.0f;
17    }
18 
19    /**
20     * @param tension
21     *  绷紧程度,当绷紧程序为0.0f时,也就没有了反向作用力。插值器将退化成一个y=x^3的加速插值器。
22     * <br/>
23     * Amount of anticipation. When tension equals 0.0f, there is
24     *                no anticipation and the interpolator becomes a simple
25     *                acceleration interpolator.
26     */
27    public AnticipateInterpolator(float tension) {
28        mTension = tension;
29    }
30 
31    public AnticipateInterpolator(Context context, AttributeSet attrs) {
32        TypedArray a = context.obtainStyledAttributes(attrs,
33                com.android.internal.R.styleable.AnticipateInterpolator);
34 
35        mTension =
36                a.getFloat(com.android.internal.R.styleable.AnticipateInterpolator_tension, 2.0f);
37 
38        a.recycle();
39    }
40 
41    @Override
42    public float getInterpolation(float t) {
43        // a(t) = t * t * ((tension + 1) * t - tension)
44        return t * t * (((mTension + 1) * t) - mTension);
45    }
46}

根据getInterpolation()方法。

当tension为默认值2.0f时,曲线图如下:

当tension值为4.0f时,曲线图如下:

7. AnticipateOvershootInterpolator

源代码如下:

01package android.view.animation;
02 
03import android.content.Context;
04import android.content.res.TypedArray;
05import android.util.AttributeSet;
06 
07/**
08 * 一个插值器它开始向上推,然后向下荡,荡过最低线。然后再回到最低线。
09 * <hr/>
10 * An interpolator where the change starts backward then flings forward and overshoots
11 * the target value and finally goes back to the final value.
12 */
13public class AnticipateOvershootInterpolator implements Interpolator {
14    private final float mTension;
15 
16    public AnticipateOvershootInterpolator() {
17        mTension = 2.0f * 1.5f;
18    }
19 
20    /**
21     * @param tension
22     *  anticipation/overshoot的比值。当和tension值为0.0f时,
23     *  也就没有anticipation/overshoot的比值了,插值器退化为一个加速/减速插值器。
24     *  <br/>
25     * Amount of anticipation/overshoot. When tension equals 0.0f,
26     *                there is no anticipation/overshoot and the interpolator becomes
27     *                a simple acceleration/deceleration interpolator.
28     */
29    public AnticipateOvershootInterpolator(float tension) {
30        mTension = tension * 1.5f;
31    }
32 
33    /**
34     * @param tension Amount of anticipation/overshoot. When tension equals 0.0f,
35     *                there is no anticipation/overshoot and the interpolator becomes
36     *                a simple acceleration/deceleration interpolator.
37     * @param extraTension
38     * 乘以tension的值。例如,在上面构造函数中extraTension的值为1.5f
39     * <br/>
40     * Amount by which to multiply the tension. For instance,
41     *                     to get the same overshoot as an OvershootInterpolator with
42     *                     a tension of 2.0f, you would use an extraTension of 1.5f.
43     */
44    public AnticipateOvershootInterpolator(float tension, float extraTension) {
45        mTension = tension * extraTension;
46    }
47 
48    public AnticipateOvershootInterpolator(Context context, AttributeSet attrs) {
49        TypedArray a = context.obtainStyledAttributes(attrs, AnticipateOvershootInterpolator);
50 
51        mTension = a.getFloat(AnticipateOvershootInterpolator_tension, 2.0f) *
52                a.getFloat(AnticipateOvershootInterpolator_extraTension, 1.5f);
53 
54        a.recycle();
55    }
56 
57    private static float a(float t, float s) {
58        return t * t * (((s + 1) * t) - s);
59    }
60 
61    private static float o(float t, float s) {
62        return t * t * (((s + 1) * t) + s);
63    }
64 
65    @Override
66    public float getInterpolation(float t) {
67        // a(t, s) = t * t * ((s + 1) * t - s)
68                // o(t, s) = t * t * ((s + 1) * t + s)
69        // f(t) = 0.5 * a(t * 2, tension * extraTension), when t < 0.5
70        // f(t) = 0.5 * (o(t * 2 - 2, tension * extraTension) + 2), when t <= 1.0
71        if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension);
72        else return 0.5f * (o((t * 2.0f) - 2.0f, mTension) + 2.0f);
73    }
74}


根据getInterpolation()方法,

可以得到当tension为默认值时,曲线图为:

plot Piecewise[{{0.5((2x)*(2x)*((2+1)*2x-2)), 0<x<0.5}, {0.5*(((2x-2)*(2x-2)*((2+1)*(2x-2)+2))+2),0.5<=x<=1}}]

(不知道我的plot函数写对了没?)

 

8. CycleInterpolator 正弦周期变化插值器

源代码:

01package android.view.animation;
02 
03import android.content.Context;
04import android.content.res.TypedArray;
05import android.util.AttributeSet;
06 
07/**
08 *
09 * 以指定的周期重复动画。变化率曲线为正弦。
10 * <hr/>
11 * Repeats the animation for a specified number of cycles(周期). The
12 * rate of change follows a sinusoidal(正弦) pattern.
13 *
14 */
15public class CycleInterpolator implements Interpolator {
16    /**
17     *
18     * @param cycles 要重复的周期数
19     */
20    public CycleInterpolator(float cycles) {
21        mCycles = cycles;
22    }
23 
24    public CycleInterpolator(Context context, AttributeSet attrs) {
25        TypedArray a =
26                context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.CycleInterpolator);
27 
28        mCycles = a.getFloat(com.android.internal.R.styleable.CycleInterpolator_cycles, 1.0f);
29 
30        a.recycle();
31    }
32 
33    @Override
34    public float getInterpolation(float input) {
35        return (float)(Math.sin(2 * mCycles * Math.PI * input));
36    }
37 
38    private float mCycles;
39}
当cycle时为1时,即变化一周时,曲线图如下:


9. OvershootInterpolator 

源代码:

01package android.view.animation;
02 
03import android.content.Context;
04import android.content.res.TypedArray;
05import android.util.AttributeSet;
06 
07/**
08 * An interpolator where the change flings forward and overshoots the last value
09 * then comes back.
10 */
11public class OvershootInterpolator implements Interpolator {
12    private final float mTension;
13 
14    public OvershootInterpolator() {
15        mTension = 2.0f;
16    }
17 
18    /**
19     * @param tension Amount of overshoot. When tension equals 0.0f, there is
20     *                no overshoot and the interpolator becomes a simple
21     *                deceleration interpolator.
22     */
23    public OvershootInterpolator(float tension) {
24        mTension = tension;
25    }
26 
27    public OvershootInterpolator(Context context, AttributeSet attrs) {
28        TypedArray a = context.obtainStyledAttributes(attrs,
29                com.android.internal.R.styleable.OvershootInterpolator);
30 
31        mTension =
32                a.getFloat(com.android.internal.R.styleable.OvershootInterpolator_tension, 2.0f);
33 
34        a.recycle();
35    }
36 
37    @Override
38    public float getInterpolation(float t) {
39        // _o(t) = t * t * ((tension + 1) * t + tension)
40        // o(t) = _o(t - 1) + 1
41        t -= 1.0f;
42        return (t * t * (((mTension + 1) * t) + mTension)) + 1.0f;
43        //plot {(x-1)(x-1)((tension+1)(x-1)+tension)+1,(0<x<=1)}
44    }
45}

当tension为默认值2时,曲线图如下:

当tension的值为4时,曲线图如下:

通过学习了解Android自带的这些Interpolator,我们可以很好的根据自己的使用场景使用这些Interpolator了。也可以很容易的写出我们自己的Interpolator。

0 0