Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開源元件 NineOldAndroids:讓低版本也能玩屬性動畫

編輯:關於 Android 程式設計

簡介

NineOldAndroids 是一個把 Honeycomb 之後才有的 Android 動畫 API 向下移植的框架,包含 ViewAnimator、ObjectAnimator、ViewPropertyAnimator 等一整套介面。它解決了 Tween 動畫的老問題:動畫播放時只有「位移效果」,被移動的元件本身其實沒有真的換位置,導致點擊事件仍然落在原本的座標上。改用屬性動畫之後,物件的屬性真的被改寫,互動行為才會跟視覺一致。

建立 ObjectAnimator

ObjectAnimator anim1 = ObjectAnimator.ofFloat(
        balls.get(0), "y", 0f, getHeight() - balls.get(0).getHeight())
        .setDuration(500);

// 啟動動畫
animation.start();

// 複製一份
ObjectAnimator anim2 = anim1.clone();

動畫組:AnimatorSet

ObjectAnimator animDown = ObjectAnimator.ofFloat(
        balls.get(2), "y", 0f, getHeight() - balls.get(2).getHeight())
        .setDuration(500);
ObjectAnimator animUp = ObjectAnimator.ofFloat(
        balls.get(2), "y", getHeight() - balls.get(2).getHeight(), 0f)
        .setDuration(500);

AnimatorSet s1 = new AnimatorSet();
// 依序播放,讓動畫具有連貫性
s1.playSequentially(animDown, animUp);

// 同時播放,讓多段動畫的起始時間對齊
animation.playTogether(anim1, anim2, s1);

值動畫與 XML 布局載入

ValueAnimator alphaAnimator = (ValueAnimator) AnimatorInflater
        .loadAnimator(AnimationLoading.this, R.anim.animator);
alphaAnimator.setTarget(balls.get(1));
alphaAnimator.addUpdateListener(new AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        balls.get(1).setAlpha((Float) animation.getAnimatedValue());
    }
});
// 動畫集
AnimatorSet s1 = (AnimatorSet) AnimatorInflater
        .loadAnimator(AnimationLoading.this, R.anim.animator_set);
s1.setTarget(balls.get(2));

// 物件動畫(位移/顏色變化)
ObjectAnimator s2 = (ObjectAnimator) AnimatorInflater
        .loadAnimator(AnimationLoading.this, R.anim.color_animator);
s2.setTarget(balls.get(3));

// 定義動畫順序
((AnimatorSet) animation).play(animX).before(animY);  // animX 先於 animY
((AnimatorSet) animation).play(animX).with(animY);    // animX 與 animY 同步執行

// 圓弧加速器
new CycleInterpolator(2.0f)

PropertyValuesHolder:多屬性組合

PropertyValuesHolder animY = PropertyValuesHolder.ofFloat(
        "y", balls.get(1).getY(), getHeight() - 100);
PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.5f);

ObjectAnimator pvhAlpha = ObjectAnimator.ofPropertyValuesHolder(
        balls.get(1), animY, alpha).setDuration(1000);

放大動畫的寫法,同時改變寬高與座標,讓物件以中心為基準放大:

PropertyValuesHolder widthHolder = PropertyValuesHolder.ofFloat(
        "width", ball.getWidth(), ball.getWidth() * 2);
PropertyValuesHolder heightHolder = PropertyValuesHolder.ofFloat(
        "height", ball.getHeight(), ball.getHeight() * 2);
PropertyValuesHolder xPt = PropertyValuesHolder.ofFloat(
        "x", ball.getX(), ball.getX() - BALL_SIZE / 2f);
PropertyValuesHolder yPt = PropertyValuesHolder.ofFloat(
        "y", ball.getY(), ball.getY() - BALL_SIZE / 2f);

ObjectAnimator sumAnimator = ObjectAnimator.ofPropertyValuesHolder(
        ball, widthHolder, heightHolder, xPt, yPt).setDuration(750);
sumAnimator.setRepeatMode(ValueAnimator.REVERSE);
sumAnimator.setRepeatCount(1);   // repeatCount = 1 使其恢復原樣

ObjectAnimator 常用屬性

位移(單位為像素,可指定多個關鍵位置):

ObjectAnimator.ofFloat(target, "translationX", 0, 50).setDuration(duration).start();
ObjectAnimator.ofFloat(target, "translationY", 0, 50, -50, 0).setDuration(duration).start();

縮放(1.0f 代表原始尺寸的一倍,倍數相對於最初設定的寬高):

ObjectAnimator.ofFloat(target, "scaleX", 1, 2, 1).setDuration(duration).start();
ObjectAnimator.ofFloat(target, "scaleY", 1, 2).setDuration(duration).start();

透明度(1.0f 為不透明,0 為全透明):

ObjectAnimator.ofFloat(target, "alpha", 1, 0, 1).setDuration(duration).start();

旋轉:

ObjectAnimator.ofFloat(target, "rotationX", 0, 180, 0).setDuration(duration).start();
ObjectAnimator.ofFloat(target, "rotationY", 0, 360).setDuration(duration).start();
ObjectAnimator.ofFloat(target, "rotation", 0, 180, 0).setDuration(duration).start();

設定變換中心(多個動畫組合成複合變換時特別有用):

ViewHelper.setPivotX(target, target.getWidth() / 2);
ViewHelper.setPivotY(target, target.getHeight() / 2);

ViewPropertyAnimator:更簡潔的鏈式寫法

animate(target).setDuration(2000);

animate(animatingButton).alpha(0);
animate(animatingButton).x(xValue).y(yValue);
animate(animatingButton).rotationYBy(720);

實務建議:單一屬性的簡單動畫用 ViewPropertyAnimator 最省事;需要多屬性協同、動畫序列或從 XML 載入時,改用 ObjectAnimator 搭配 AnimatorSet 會更清楚。另外提醒,NineOldAndroids 屬於相容性移植方案,若專案 minSdk 已高於 API 11,直接用系統原生的 android.animation 即可,不必額外引入。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved