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

Android 屬性動畫備忘:常用寫法與組合動畫

編輯:關於 Android 程式設計

項目開發中偶爾會用到動畫,每次都得翻出舊代碼複製,既麻煩又記不住,索性整理在這裡備查。

常用單一屬性動畫

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);

組合動畫更省事

偶爾會遇到需要多個動畫同時播放的場景,用 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();

實戰:加入購物車的複合動畫

前幾天做了一個「加入商品到購物車」的效果:商品圖片先旋轉、縮小,再位移到購物車圖標的位置。這裡用的是傳統動畫,比較繁瑣,而且動畫的執行順序一旦弄亂,位移軌跡就會出問題。順手記錄一下,免得日後遺忘:

int[] endLocation = new int[2];
topSpcart.getLocationInWindow(endLocation);// shopCart 是那個購物車
// 計算位移
int endX = endLocation[0] - startLocation[0];// 動畫位移的 X 坐標
int endY = endLocation[1] - startLocation[1];// 動畫位移的 Y 坐標

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, 0, Animation.RELATIVE_TO_SELF, 0);
TranslateAnimation translateAnimation = new TranslateAnimation(0, endX, 0, endY);
// translateAnimation.setInterpolator(new LinearInterpolator());// 動畫加速器
// translateAnimation.setInterpolator(new AccelerateInterpolator());
// 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) {
    }
});
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved