萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

Android 動畫效果完整指南:View 動畫、屬性動畫、Drawable 動畫與 Compose

前言

Android 提供三代動畫體系:**View 動畫 (舊)**、**屬性動畫 (Property Animation, API 12+)**、**Jetpack Compose 動畫 (現代宣告式)**,另含 Drawable Animation (幀動畫)。本文系統比較四類動畫適用場景與核心 API。

動畫體系對照表

體系API 等級核心類別特點適用場景
View Animation1+Animation, AlphaAnimation, TranslateAnimation, ScaleAnimation, RotateAnimation, AnimationSet僅改變繪製矩陣,不改變實際屬性;輕量、簡單簡單淡入淡出、平移、縮放、旋轉;舊版相容
Property Animation12+ObjectAnimator, ValueAnimator, AnimatorSet, TypeEvaluator, Interpolator真正修改物件屬性 (x, y, alpha, rotation, 自訂屬性);功能最強複雜編排、互動動畫、狀態同步、RecyclerView Item 動畫
Drawable Animation1+AnimationDrawable, AnimatedVectorDrawable, AnimatedImageDrawable逐幀播放 PNG/WebP 序列或 Vector 路徑插值Loading Spinner、表情貼圖、複雜矢量動畫
Compose AnimationCompose 1.0+animate*AsState, updateTransition, AnimatedVisibility, Crossfade, Animatable宣告式、狀態驅動、自動取消/重組、與 Compose UI 無縫整合全新 Compose 專案、混合專案逐步遷移

View 動畫 XML 範例 (res/anim/fade_scale.xml)

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

屬性動畫核心用法 (Kotlin)

// 簡單單一屬性
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()
}

Drawable 幀動畫 XML (res/drawable/loading.xml)

<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() }

Compose 動畫入門

@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 打造流暢體驗。

copyright © 萬盛學電腦網 all rights reserved