編輯:關於 Android 開發
Android 的動畫體系分為三大類,原理和適用場景各不相同。理清它們的關係,能少走很多彎路。
原理很樸素:一張張圖片不斷切換,形成動畫效果。
在 drawable 目錄下定義描述文件,子節點為 animation-list,在其中列出要顯示的圖片和每張圖的顯示時長:
<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/g1" android:duration="200" />
<item android:drawable="@drawable/g2" android:duration="200" />
<item android:drawable="@drawable/g3" android:duration="200" />
</animation-list>
oneshot="false" 表示循環播放,設為 true 則只播放一次。
在代碼中播放:
ImageView iv = (ImageView) findViewById(R.id.iv); iv.setBackgroundResource(R.drawable.animations); // 作為背景設置 AnimationDrawable ad = (AnimationDrawable) iv.getBackground(); ad.start();
從原形態過渡到新形態的過程中生成的動畫,就叫補間動畫。它只改變「顯示效果」,並不改變組件的真實位置與大小——這點非常重要,點擊事件仍然觸發在原來的位置上。
// 構造:起始 X、結束 X、起始 Y、結束 Y TranslateAnimation ta = new TranslateAnimation(10, 150, 20, 140);
這裡的 10 指的是 X 起點坐標,不是屏幕坐標 10,而是 imageView 真實 X + 10;150 則是 真實 X + 150。Y 軸同理。
更常用的寫法是傳入相對比例:
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 2,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 2);
此時起點 X = 真實 X + 0.5 × 自身寬度,終點 X = 真實 X + 2 × 自身寬度;Y 軸同理使用自身高度計算。
ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4);
0.1f 表示動畫起始寬度為真實寬度的 0.1 倍,4 表示結束時為 4 倍。默認縮放中心在左上角,可以顯式指定:
ScaleAnimation sa = new ScaleAnimation(
0.1f, 4, 0.1f, 4,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
後兩個 0.5f 把縮放中心移動到自身中心(真實 X + 0.5 × 寬,真實 Y + 0.5 × 高)。
AlphaAnimation aa = new AlphaAnimation(0, 0.5f);
0 為完全透明,1 為完全不透明。
RotateAnimation ra = new RotateAnimation(20, 360);
20 是起始角度,360 是結束角度,默認圓心在左上角。指定圓心:
RotateAnimation ra = new RotateAnimation(
20, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
ta.setDuration(2000); // 持續時間(毫秒) ta.setRepeatCount(1); // 重複次數 ta.setRepeatMode(Animation.REVERSE); // 重複模式:REVERSE 或 RESTART ta.setFillAfter(true); // 播放完畢停留在結束位置 iv.startAnimation(ta);
AnimationSet set = new AnimationSet(false); set.addAnimation(aa); set.addAnimation(sa); set.addAnimation(ra); iv.startAnimation(set);
補間動畫只是視覺效果,組件的真實 xy 並未改變;屬性動畫則是真正去修改對象的屬性值,動畫結束後組件的位置、大小確實發生了變化。
使用前提是該屬性必須有配套的 getter 與 setter——凡是具備 get/set 方法的成員變量,就稱為屬性。
// 參數:目標對象、屬性名、可變參數(新值) ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 100);
傳入 0 表示 x 起點為「當前 x + 0」,傳入 100 表示終點為「當前 x + 100」。
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "scaleY", 0.1f, 2);
從原高度的 0.1 倍放大到 2 倍。
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "alpha", 0.1f, 1);
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotation", 20, 270); // 順時針 ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotationY", 20, 180); // 水平翻轉
rotationX 是豎直翻轉,rotationY 是水平翻轉。
ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 70, 30, 100);
傳入多組值即可讓屬性在這些關鍵點之間依次過渡,从而实现「出去再回來」之類的效果。
AnimatorSet set = new AnimatorSet(); set.setTarget(bt); // 有序播放:一個接一個 // set.playSequentially(oa, oa2, oa3, oa4); // 同時播放 set.playTogether(oa, oa2, oa3, oa4); set.start();
此外 Android 5.0 起提供了 場景轉場動畫(Activity Transitions / Transition Framework),適合 Activity 或 Fragment 之間的整體切換效果;Jetpack Compose 則用 animate*AsState 與 Transition 提供聲明式的動畫能力,思路與命令式的 ObjectAnimator 完全不同,值得後續了解。
告別 0 寬高:onCreate 中獲取 View 尺寸的四種可靠方案
「在 onCreate 裡拿到的 View 寬高是 0」這個問題,幾乎每個 Android 開發者都遇到過。它其實不難解決,但我們追求的往往不是「怎麼解決」,而是「有幾種
Linux Netlink 從入門到實戰:核心與應用程式的雙向通訊完全解析
Netlink 是 Linux 提供的一套機制,主要用於核心(kernel)與使用者態行程之間的通訊。本文將由淺入深,帶你完整掌握 Netlink 的概念、資料結構與實作
Android Activity 之間返回數據:startActivityForResult 完整實作
Android Activity 之間返回數據:startActivityForResult 完整實作 在Android開發中,我們常常需要從一個Activity打開另一
Android Studio 系列教學(一):下載、安裝與打造第一個 Hello World
Android Studio 系列教學(一):下載、安裝與打造第一個 Hello World 本系列文章大致涵蓋以下主題,本文為第一篇: 系列一:下載與安裝、背景介紹、