編輯:關於 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);
這幾行涵蓋了日常八成的需求。基點(pivot)這件事容易被忽略:不設定的話,縮放與旋轉都會繞著左上角跑,而不是物件中心。
遇到需要多個動畫同時執行的場合,用 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。
EasyLikeArea:快速做出朋友圈、QQ 空間風格的「讚」區域
EasyLikeArea:快速做出朋友圈、QQ 空間風格的「讚」區域 幾乎每一款社群類應用都會遇到同一個需求:在內容下方排出一排頭像,表示「哪些人按了讚」,頭像多到一定
Android 之意圖(Intent)的使用詳解
Android 之意圖(Intent)的使用詳解 Intent 常見的動作有:MAIN_ACTION(主視圖)、VIEW_ACTION(查看)、EDIT_ACTION(修
GUI 系統之 View 體系(二):setContentView 源碼剖析
本文從 Activity 的 setContentView 方法講起,逐層剖析 Android GUI 系統中 View 體系的構建過程。 1. 從 setContent
Android 文字漸變與歌詞進度效果實現
文字漸變效果Android 可透過 LinearGradient 為文字加上線性漸變,也能利用自定義 View 或 ProgressBar 做出歌詞進度效果。Linear