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

Android 動畫總整理:Tween、Frame、LayoutAnimation 與監聽器

編輯:關於 Android 程式設計

Android 動畫系統涵蓋多種形式,常見的有 Tweened Animation、Frame-by-frame Animation、LayoutAnimationController,以及搭配使用的 AnimationListener。

Tweened Animation

Tween 動畫提供 Alpha、Scale、Rotate、Translate 四種基本效果,可單獨使用,也可放入 AnimationSet 中組合播放。

XML 與程式碼

可在 res/anim 建立 set 資源,加入 alpha、rotate、scale、translate 等標籤;程式碼中則可用 AnimationUtils.loadAnimation 載入。若完全用程式碼實作,可直接建立對應的 Animation 物件,再設定 duration、interpolator 等參數。

常用設定

setDuration 控制時間,setFillAfter 與 setFillBefore 控制動畫結束後是否保留狀態,setStartOffset 設定延遲,setRepeatCount 設定重複次數。

AnimationSet 與 Interpolator

AnimationSet 可將多個動畫疊加播放。android:shareInterpolator 可決定是否共用同一個插值器。常見插值器包含 AccelerateInterpolator、DecelerateInterpolator、AccelerateDecelerateInterpolator、LinearInterpolator 與 CycleInterpolator。

LayoutAnimationController

LayoutAnimationController 可讓 ListView 或 ViewGroup 中的子元件依序出現,常用於列表進場動畫。可設定 delay、animationOrder 與要套用的動畫資源。

AnimationListener

AnimationListener 可監聽動畫的開始、重複與結束。實務上常用於動畫結束後移除 View,或開始後動態加入新元件。

整體而言,XML 方式易於維護,程式碼方式則更適合動態控制與除錯。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved