Android 提供三代動畫體系:**View 動畫 (舊)**、**屬性動畫 (Property Animation, API 12+)**、**Jetpack Compose 動畫 (現代宣告式)**,另含 Drawable Animation (幀動畫)。本文系統比較四類動畫適用場景與核心 API。
| 體系 | API 等級 | 核心類別 | 特點 | 適用場景 |
|---|---|---|---|---|
| View Animation | 1+ | Animation, AlphaAnimation, TranslateAnimation, ScaleAnimation, RotateAnimation, AnimationSet | 僅改變繪製矩陣,不改變實際屬性;輕量、簡單 | 簡單淡入淡出、平移、縮放、旋轉;舊版相容 |
| Property Animation | 12+ | ObjectAnimator, ValueAnimator, AnimatorSet, TypeEvaluator, Interpolator | 真正修改物件屬性 (x, y, alpha, rotation, 自訂屬性);功能最強 | 複雜編排、互動動畫、狀態同步、RecyclerView Item 動畫 |
| Drawable Animation | 1+ | AnimationDrawable, AnimatedVectorDrawable, AnimatedImageDrawable | 逐幀播放 PNG/WebP 序列或 Vector 路徑插值 | Loading Spinner、表情貼圖、複雜矢量動畫 |
| Compose Animation | Compose 1.0+ | animate*AsState, updateTransition, AnimatedVisibility, Crossfade, Animatable | 宣告式、狀態驅動、自動取消/重組、與 Compose UI 無縫整合 | 全新 Compose 專案、混合專案逐步遷移 |
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" />
<scale
android:fromXScale="0.8" android:toXScale="1.0"
android:fromYScale="0.8" android:toYScale="1.0"
android:pivotX="50%" android:pivotY="50%"
android:duration="300" />
</set>
// 簡單單一屬性
ObjectAnimator.ofFloat(view, "translationY", 0f, -200f)
.setDuration(500)
.start()
// 多屬性並行
AnimatorSet().apply {
playTogether(
ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.2f, 1f),
ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.2f, 1f),
ObjectAnimator.ofFloat(view, "alpha", 1f, 0.5f, 1f)
).setDuration(800).start()
}
// 自訂屬性 (需提供 getter/setter)
val progressAnimator = ValueAnimator.ofInt(0, 100).apply {
addUpdateListener { progressText.text = "${it.animatedValue}%" }
setDuration(2000)
start()
}
<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/frame_0" android:duration="100" />
<item android:drawable="@drawable/frame_1" android:duration="100" />
<item android:drawable="@drawable/frame_2" android:duration="100" />
<!-- ... -->
</animation-list>
// 使用
val img = findViewById(R.id.loading)
img.setBackgroundResource(R.drawable.loading)
val anim = (img.background as AnimationDrawable).apply { start() }
@Composable
fun ExpandableCard() {
var expanded by remember { mutableStateOf(false) }
val height by animateDpAsState(if (expanded) 200.dp else 80.dp, label = "height")
val alpha by animateFloatAsState(if (expanded) 1f else 0.5f, label = "alpha")
Card(modifier = Modifier
.fillMaxWidth()
.height(height)
.alpha(alpha)
.animateContentSize() // 自動添加尺寸變化動畫
.clickable { expanded = !expanded }
) {
Text("點擊展開/收合", style = MaterialTheme.typography.h6, modifier = Modifier.padding(16.dp))
}
}
android:hardwareAccelerated="true" (預設開啟),複雜 Path 可關閉圖層類型。requestLayout(),改用 translationX/Y、scaleX/Y。LayoutTransition / RecyclerView.ItemAnimator:自動處理增刪項目動畫。onDestroy/onStop 取消 Animator,避免持有 View 參考。依專案架構 (傳統 View / Compose) 選擇對應動畫體系,善用屬性動畫與 Compose 宣告式 API 打造流暢體驗。