美文网首页
interpolator

interpolator

作者: 有点健忘 | 来源:发表于2018-06-04 14:23 被阅读20次

效果看这里,有动画展示
https://blog.csdn.net/cjh_android/article/details/51508634

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

AccelerateInterpolator
@android:anim/accelerate_interpolator
在动画开始的地方速率改变比较慢,然后开始加速

AnticipateInterpolator
@android:anim/anticipate_interpolator
开始的时候向后然后向前甩

AnticipateOvershootInterpolator
@android:anim/anticipate_overshoot_interpolator
开始的时候向后然后向前甩一定值后返回最后的值

BounceInterpolator
@android:anim/bounce_interpolator
动画结束的时候弹起

CycleInterpolator
@android:anim/cycle_interpolator
动画循环播放特定的次数,速率改变沿着正弦曲线

DecelerateInterpolator
@android:anim/decelerate_interpolator
在动画开始的地方快然后慢

LinearInterpolator
@android:anim/linear_interpolator
以常量速率改变

OvershootInterpolator
@android:anim/overshoot_interpolator
向前甩一定值后再回到原来位置
各插值器的源码

    /**
     * Maps a value representing the elapsed fraction of an animation to a value that represents
     * the interpolated fraction. This interpolated value is then multiplied by the change in
     * value of an animation to derive the animated value at the current elapsed animation time.
     *
     * @param input A value between 0 and 1.0 indicating our current point
     *        in the animation where 0 represents the start and 1.0 represents
     *        the end
     * @return The interpolation value. This value can be more than 1.0 for
     *         interpolators which overshoot their targets, or less than 0 for
     *         interpolators that undershoot their targets.
     */
    float getInterpolation(float input);

AccelerateDecelerateInterpolator

/**
 * An interpolator where the rate of change starts and ends slowly but
 * accelerates through the middle.
 */
    public float getInterpolation(float input) {
        return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
    }

AccelerateInterpolator

/**
 * An interpolator where the rate of change starts out slowly and
 * and then accelerates.
 */
    public AccelerateInterpolator() {
        mFactor = 1.0f;
        mDoubleFactor = 2.0;
    }
    public float getInterpolation(float input) {
        if (mFactor == 1.0f) {
            return input * input;
        } else {
            return (float)Math.pow(input, mDoubleFactor);
        }
    }

AnticipateInterpolator

/**
 * An interpolator where the change starts backward then flings forward.
 */
    public AnticipateInterpolator() {
        mTension = 2.0f;
    }
    public float getInterpolation(float t) {
        // a(t) = t * t * ((tension + 1) * t - tension)
        return t * t * ((mTension + 1) * t - mTension);
    }

AnticipateOvershootInterpolator

/**
 * An interpolator where the change starts backward then flings forward and overshoots
 * the target value and finally goes back to the final value.
 */
   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;
    }
    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 < 0.5
        // f(t) = 0.5 * (o(t * 2 - 2, tension * extraTension) + 2), when t <= 1.0
        if (t < 0.5f) return 0.5f * a(t * 2.0f, mTension);
        else return 0.5f * (o(t * 2.0f - 2.0f, mTension) + 2.0f);
    }

BounceInterpolator

/**
 * An interpolator where the change bounces at the end.
 */
   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;
    }

CycleInterpolator

/**
 * Repeats the animation for a specified number of cycles. The
 * rate of change follows a sinusoidal pattern.
 */
  public CycleInterpolator(float cycles) {
        mCycles = cycles;
    }
    public float getInterpolation(float input) {
        return (float)(Math.sin(2 * mCycles * Math.PI * input));
    }

DecelerateInterpolator

/**
 * An interpolator where the rate of change starts out quickly and
 * and then decelerates.
 */
    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 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;

OvershootInterpolator

/**
 * An interpolator where the change flings forward and overshoots the last value
 * then comes back.
 */
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 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;
    }

相关文章

网友评论

      本文标题:interpolator

      本文链接:https://www.haomeiwen.com/subject/akaesftx.html