編輯:關於 Android 程式設計
LayoutAnimationController 能讓一個容器中的子控制項「依序」出現,而不是整批同時現身。清單逐項淡入、網格依序彈出,都是靠它。實作有 XML 與程式碼兩條路。
第一步,在 res 下建立 anim 目錄,新增 alpha.xml 定義單項的動畫:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="3000" />
</set>
第二步,在同一目錄新增 layoutanimation.xml,定義「怎麼排隊」:
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
xmlns:android="https://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/alpha" />
其中 delay 是每個子項之間的延遲比例(0.5 表示每項延後前一项動畫時長的 0.5 倍),animationOrder 決定播放順序。
第三步,在 ListView 上掛上它:
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layoutAnimation="@anim/layoutanimation" />
AlphaAnimation alpha = new AlphaAnimation(0, 1); alpha.setDuration(3000); LayoutAnimationController lac = new LayoutAnimationController(alpha); lac.setOrder(LayoutAnimationController.ORDER_NORMAL); lv.setLayoutAnimation(lac);
LayoutAnimationController.ORDER_NORMAL; // 順序顯示 LayoutAnimationController.ORDER_REVERSE; // 反向顯示 LayoutAnimationController.ORDER_RANDOM; // 隨機顯示
LayoutAnimationController 只在容器首次配置子檢視時生效。因此如果是 ListView 這種會回收重用的檢視,捲動時新項目的進場動畫不會自動重跑;反之,若你每次 notifyDataSetChanged 都重新觸發,捲動過程可能會變得卡頓。想要「捲動時每項都有進場效果」,通常得在 getView 中自行控制,或改用 ItemAnimator/自訂 LayoutTransition。
另外,delay 是「相對比例」而非絕對毫秒。動畫時長設 3000ms、delay 設 0.5,十個項目就要排到數秒之久,實務上通常會把單項 duration 壓到兩三百毫秒,delay 取 0.1~0.3,整體節奏才不會拖沓。
Android 常見 UI 元件之 ListView(二):客製化清單與多選、篩選功能
Android 常見 UI 元件之 ListView(二)——客製化 ListView這一篇承接上一篇,示範如何在 ListView 中一次選取多個項目,並實作搜尋篩選的
Android 記憶體檢視方法與常用 adb 指令整理
1. 從 IDE 的 Log 中觀察 每次虛擬機執行垃圾回收時,都會打印一條日誌,格式如下: D/dalvikvm: <GC_Reason>, <Amo
Android 界面開發配色參考:官方色板與常用色值表
好的配色是界面設計的基礎。以下先列出 Android 幫助文件中關於界面開發的推薦色值,再補上一份可直接複製使用的常用顏色對照表。 一、官方推薦色值 Material
Android 四大基本元件:Activity、BroadcastReceiver、ContentProvider 與 Service
Android 應用程式由多個基本元件組成,最常見的四個是 Activity、BroadcastReceiver、ContentProvider 與 Service。Ac