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

為 Android Layout 加入動畫的兩種做法

編輯:關於 Android 程式設計

方法一:LayoutAnimationController

替 Layout 加入動畫,第一種常見做法是使用 LayoutAnimationController。它會針對容器內的子元件逐一套用動畫,適合製作清單進場、按鈕依序出現等效果。

實作時,可先建立一個 ScaleAnimation 或 TranslateAnimation,設定 duration,再用它建立 LayoutAnimationController,最後呼叫 layout.setLayoutAnimation(controller)。若要控制子元件出現順序,可設定 ORDER_NORMAL、ORDER_REVERSE 等模式。

方法二:直接啟動動畫

第二種方式是直接對 Layout 呼叫 startAnimation()。這類動畫會套用在整個容器上,而不是逐一處理子元件。適合需要整塊 view 淡入、縮放或位移的情境。

兩種方式的差異在於:LayoutAnimationController 更適合控制群組元件的進場節奏;直接 startAnimation 則更適合單一容器效果。實際開發時,可依照介面需求選擇。

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