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

Android 屬性動畫速查筆記:位移、縮放、旋轉與組合動畫

編輯:關於 Android 開發

把常用動畫寫成備忘,省得每次翻舊程式碼

專案開發偶爾會需要動畫效果。每次都要去翻以前的專案、把片段複製過來,麻煩;但要憑記憶寫出來,又記不住。乾脆整理成一份備忘錄。

單一屬性動畫(nineoldandroids 風格)

// 位移
ObjectAnimator.ofFloat(view, "translationX", 0, 50, -50, 0).setDuration(duration).start();
ObjectAnimator.ofFloat(view, "translationY", 0, 50, -50, 0).setDuration(duration).start();

// 縮放
ObjectAnimator.ofFloat(view, "scaleX", 1, 2, 1).setDuration(duration).start();
ObjectAnimator.ofFloat(view, "scaleY", 1, 2, 1).setDuration(duration).start();

// 透明度
ObjectAnimator.ofFloat(view, "alpha", 1, 0, 1).setDuration(duration).start();

// 翻轉
ObjectAnimator.ofFloat(view, "rotationX", 0, 180, 0).setDuration(duration).start();
ObjectAnimator.ofFloat(view, "rotationY", 0, 180, 0).setDuration(duration).start();

// 旋轉
ObjectAnimator.ofFloat(view, "rotation", 0, 180, 0).setDuration(duration).start();

// 設定動畫基點(很重要,預設是左上角)
ViewHelper.setPivotX(view, view.getWidth() / 2f);
ViewHelper.setPivotY(view, view.getHeight() / 2f);

這幾行涵蓋了日常八成的需求。基點(pivot)這件事容易被忽略:不設定的話,縮放與旋轉都會繞著左上角跑,而不是物件中心。

組合動畫:AnimatorSet

遇到需要多個動畫同時執行的場合,用 AnimatorSet 會省事很多:

AnimatorSet animator = new AnimatorSet();
animator.playTogether(
    ObjectAnimator.ofFloat(image, "rotation", 0, 1080),
    ObjectAnimator.ofFloat(image, "translationX", 0, 180),
    ObjectAnimator.ofFloat(image, "translationY", 0, -180),
    ObjectAnimator.ofFloat(image, "scaleX", 1, 0),
    ObjectAnimator.ofFloat(image, "scaleY", 1, 0),
    ObjectAnimator.ofFloat(image, "alpha", 1, 0.25f, 1)
);
animator.setDuration(5 * 1000).start();

playTogether 是同時播放;若想改成先後序列執行,則換成 playSequentially。

實戰:加入購物車的飛入動畫

需求是點擊「加入購物車」時,商品圖片先旋轉、再縮小,同時位移到購物車圖示的位置。這裡改用傳統的 AnimationSet 寫法。要注意動畫設定的順序如果弄亂,位移軌跡會跑掉。

int[] endLocation = new int[2];
shopCart.getLocationInWindow(endLocation);   // 取得購物車圖示在視窗中的座標

// 計算需要位移的距離
int endX = endLocation[0] - startLocation[0];
int endY = endLocation[1] - startLocation[1];

RotateAnimation rotateAnimation = new RotateAnimation(
        0, 1080, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
ScaleAnimation scaleAnimation = new ScaleAnimation(
        1, 0, 1, 0, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
TranslateAnimation translateAnimation = new TranslateAnimation(0, endX, 0, endY);
// translateAnimation.setInterpolator(new LinearInterpolator());   // 加速/減速曲線
// translateAnimation.setRepeatCount(0);                          // 重複次數
// translateAnimation.setFillAfter(true);                         // 結束後停在末位置

AnimationSet set = new AnimationSet(false);
set.setFillAfter(true);
set.addAnimation(rotateAnimation);    // 旋轉
set.addAnimation(scaleAnimation);     // 縮小
set.addAnimation(translateAnimation); // 位移
set.setDuration(1000);
view.startAnimation(set);

set.setAnimationListener(new AnimationListener() {
    @Override public void onAnimationStart(Animation animation) { }
    @Override public void onAnimationRepeat(Animation animation) { }
    @Override public void onAnimationEnd(Animation animation) { }
});

幾個實務上的提醒:位移量一定要用「目標座標減去起始座標」算出相對值,因為 TranslateAnimation 的參數是位移量而非絕對座標;RELATIVE_TO_SELF 搭配 0.5f 表示以自身中心為基準;而 setFillAfter(true) 決定了動畫結束後元素留在終點還是彈回原位——做飛入購物車這種效果時,通常要設為 true。

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