Animation 之 Interpolator 插補器理解

一、描述:

我們在設計動畫時,通常都會用到一些Interpolator,而它的作用就是控制動畫的速度,即可以理解爲:

Interpolator是一個速度控制器,控制速度變化。

Interpolator藉口只有一個抽象方法getInterpolation(float input),而系統也自帶了幾個Interpolater供我們使用:

1. AccelerateInterpolator:  動畫從開始到結束,變化率是一個加速的過程。
2. DecelerateInterpolator: 動畫從開始到結束,變化率是一個減速的過程。
3. CycleInterpolator:           動畫從開始到結束,變化率是循環給定次數的正弦曲線。
4. AccelerateDecelerateInterpolator: 動畫從開始到結束,變化率是先加速後減速的過程。
5. LinearInterpolator: 動畫從開始到結束,變化率是線性變化。

除了以上幾種,我們也可以繼承Interpolater來定義自己的插補器:

import android.view.animation.Interpolator;

public class MyInterpolator implements Interpolator {
	private float mFactor;
	private int iFactor;

	public MyInterpolator(int factor) {
		this.iFactor = factor;
	}

	@Override
	public float getInterpolation(float input) {
		switch (iFactor) {
		case 1:
			mFactor = input;  // 性線勻速
			break;
		case 2:
			mFactor = input * input * input;  // 加速
			break;
		}
		return mFactor;
	}
}

二、Interpolater — 策略模式:

2.1 接口類:TimeInterpolator

package android.animation;

/**
 * A time interpolator defines the rate of change of an animation. This allows animations
 * to have non-linear motion, such as acceleration and deceleration.
 */
public interface TimeInterpolator {

    /**
     * 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);
}

講的很清楚:TimeInterpolator定義了一個可以改變動畫速率的時間插補器,它允許你的動畫可以是非線性行爲:如加速和減速。

2.2 Interpolator:

package android.view.animation;

import android.animation.TimeInterpolator;

/**
 * An interpolator defines the rate of change of an animation. This allows
 * the basic animation effects (alpha, scale, translate, rotate) to be 
 * accelerated, decelerated, repeated, etc.
 */
public interface Interpolator extends TimeInterpolator {
    // A new interface, TimeInterpolator, was introduced for the new android.animation
    // package. This older Interpolator interface extends TimeInterpolator so that users of
    // the new Animator-based animations can use either the old Interpolator implementations or
    // new classes that implement TimeInterpolator directly.
}

我擦!想必大家都無語,只是告訴我們Interpolater擴展了TimeInterpolator,其它什麼都沒。

2.3 插補器

開頭已經介紹了幾種系統自帶的,也舉了個自定義的類,這些都沒有任何關係,只不過提供給開發者,我有幾種策略給你用,而你只需要在Animation中,set一下就行,其它的就交給Animation幫你去完成你想要的動畫效果。

 

三、總結:

getInterpolation(float input)中的input是從哪裏來的?

它是由Animation類在getTransformation中會調用getInterpolater,input = (當前時間-開始時間)/總時長;

即在TimeInterpolator中定義input = [0.0f, 1.0f]區間。

關於Animation,我會抽空講解它。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章