1. 程式人生 > >Android屬性動畫用法總結

Android屬性動畫用法總結

Android 3.0提供了屬性動畫,幾乎可以代替補間動畫。屬性動畫用法更加簡潔,功能更多強大。使用屬性動畫的兩個類是ValueAnimator和ObjectAnimator。

ValueAnaimator使用示例
  ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100);//設定變化範圍0到100
        valueAnimator.setDuration(3000);//設定播放時間毫秒
        valueAnimator.setStartDelay(1000);//設定延時時間毫秒
        valueAnimator.setRepeatCount
(0);//設定重複次數 valueAnimator.setRepeatMode(ValueAnimator.RESTART);//設定重複模式 valueAnimator.start();//開始播放 valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int
value = (int) animation.getAnimatedValue(); //獲取實時數值並更新到需要的View上 } });

ObjectAnimator比ValueAnimator更高階,不用監聽資料變化,它可以直接修改指定的屬性。前提是物件必須有對應的setXXX()方法和getXXX()方法。如果沒有這兩個方法,我們可以通過繼承或者包裝(裝飾模式)來新增這兩個方法。

ObjectAnimator使用示例
 ObjectAnimator objectAnimator = ObjectAnimator.ofFloat
(view, "alpha", 1f, 0f); objectAnimator.setDuration(300); ... //設定若干屬性省略 objectAnimator.start();

以上是單一動畫,如果要使用組合動畫,可以使用PropertyValuesHolder。 ValueAnimator和ObjectAnimator組合動畫的用法一樣,下面是ObjectAnimator的組合動畫使用示例

組合動畫使用示例
    PropertyValuesHolder translateHolder = PropertyValuesHolder.ofFloat("translationY", 0, 500);
    PropertyValuesHolder alphaHolder = PropertyValuesHolder.ofFloat("alpha", 0f, 1f, 0f);
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, alphaHolder, translateHolder);
        objectAnimator.setDuration(3600);
        objectAnimator.setStartDelay(1000);
        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
          //監聽動畫結束
            }
        });
        objectAnimator.start();

如果要修改的屬性不是數值型別的,我們可以使用對應的ofObject方法: ValueAnimator.ofObject方法原始碼:

   public static ValueAnimator ofObject(TypeEvaluator evaluator, Object... values) {
        ValueAnimator anim = new ValueAnimator();
        anim.setObjectValues(values);
        anim.setEvaluator(evaluator);
        return anim;
    }

ObjectAnimator.ofObject方法原始碼:

   public static ObjectAnimator ofObject(Object target, String propertyName, TypeEvaluator evaluator, Object... values) {
        ObjectAnimator anim = new ObjectAnimator(target, propertyName);
        anim.setObjectValues(values);
        anim.setEvaluator(evaluator);
        return anim;
    }

ofObject(…)方法需要傳入一個估值器TypeEvaluator。

關於估值器

下面是自帶的IntEvaluator原始碼。我們只需要重寫evaluate這個方法即可。 其中fraction是百分比,是系統自動計算出來的,而動畫進度值是需要我們自己去算的。計算規則根據情況而定。

/**
 * This evaluator can be used to perform type interpolation between <code>int</code> values.
 */
public class IntEvaluator implements TypeEvaluator<Integer> {

    /**
     * This function returns the result of linearly interpolating the start and end values, with
     * <code>fraction</code> representing the proportion between the start and end values. The
     * calculation is a simple parametric calculation: <code>result = x0 + t * (v1 - v0)</code>,
     * where <code>x0</code> is <code>startValue</code>, <code>x1</code> is <code>endValue</code>,
     * and <code>t</code> is <code>fraction</code>.
     *
     * @param fraction   The fraction from the starting to the ending values
     * @param startValue The start value; should be of type <code>int</code> or
     *                   <code>Integer</code>
     * @param endValue   The end value; should be of type <code>int</code> or <code>Integer</code>
     * @return A linear interpolation between the start and end values, given the
     *         <code>fraction</code> parameter.
     */
    public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
        int startInt = startValue;
        return (int)(startInt + fraction * (endValue - startInt));
    }
}