編輯:關於 Android 程式設計
這篇要動手做兩件事:先實作「空心愛心 → 實心愛心」的過渡動畫,再把同一套素材反過來播成「實心 → 空心」。

幀動畫的原理和老式電影膠卷一樣:把一連串圖片快速換過,一「幀」就是一張圖。所以第一步永遠是準備圖片序列。
素材有兩條路可走:用 XML drawable(例如 shape)畫出來,或者直接用圖片檔。這裡為了省事,我準備了一組 PNG:





正式專案裡還得考慮不同屏幕密度的适配,多套 drawable-mdpi/hdpi/xhdpi 跑不掉;這裡偷懶全丟進 res/drawable-mdpi。
命名建議用自描述的方式,例如 ic_heart_0.png、ic_heart_1.png,光看檔名就知道順序,不必開圖確認。我則是按填充百分比命名,也算一種自描述。
有了圖片,接著定義一個 XML drawable 來驅動動畫。animation-list 和 animated-selector,二選一。
animation-list 是幀動畫的預設答案:API 1 就有,寫法也最直觀——依序列出圖片與各自的持續時間。
先寫「填充到實心」的版本,放在 res/drawable/animation_list_filling.xml:
<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/ic_heart_0" android:duration="500" />
<item android:drawable="@drawable/ic_heart_25" android:duration="500" />
<item android:drawable="@drawable/ic_heart_50" android:duration="500" />
<item android:drawable="@drawable/ic_heart_75" android:duration="500" />
<item android:drawable="@drawable/ic_heart_100" android:duration="500" />
</animation-list>
每個 item 指向序列中的一張圖,duration 以毫秒為單位。反過來播就成了「變空心」,放在 res/drawable/animation_list_emptying.xml:
<animation-list xmlns:android="https://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/ic_heart_100" android:duration="500" />
<item android:drawable="@drawable/ic_heart_75" android:duration="500" />
<item android:drawable="@drawable/ic_heart_50" android:duration="500" />
<item android:drawable="@drawable/ic_heart_25" android:duration="500" />
<item android:drawable="@drawable/ic_heart_0" android:duration="500" />
</animation-list>
android:oneshot="true" 表示播完一次就停;設成 false 則會無限循環。
順帶一提,500ms 在真實產品裡太慢了,這裡是刻意誇大方便觀察。另外 5 幀要做出順滑過渡其實偏少,幀數與每幀時長沒有標準答案,我的經驗是 15 幀、每幀約 15ms 就很順了。
animated-selector 稍複雜,因為它是狀態驅動的:根據 View 的狀態(選中、啟用……)套用對應的 transition。它只在 Lollipop(API 21)以上有效,所以要在 drawable-v21 裡另外準備一份。
res/drawable-v21/selector.xml:
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:id="@+id/on" android:state_activated="true">
<bitmap android:src="@drawable/ic_heart_100" />
</item>
<item android:id="@+id/off">
<bitmap android:src="@drawable/ic_heart_0" />
</item>
<transition android:fromId="@+id/off" android:toId="@+id/on">
<animation-list>
<item android:drawable="@drawable/ic_heart_25" android:duration="50" />
<item android:drawable="@drawable/ic_heart_50" android:duration="50" />
<item android:drawable="@drawable/ic_heart_75" android:duration="50" />
<item android:drawable="@drawable/ic_heart_100" android:duration="50" />
</animation-list>
</transition>
</selector>
注意 transition 是直接引用前面寫好的 animation-list。
為了兼顧舊機型,在 res/drawable/selector.xml 再放一份沒有動畫的版本:
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_activated="true">
<bitmap android:src="@drawable/ic_heart_100" />
</item>
<item android:state_activated="false">
<bitmap android:src="@drawable/ic_heart_0" />
</item>
</selector>
這樣任何裝置都能跑:Lollipop 以下會直接跳到結束狀態、略過過渡;Lollipop 以上則會播放我們定義的動畫。
到目前為止手上有四個 XML:填充用、清空用,以及兩份狀態切換用的 selector。
版面裡放三個 ImageView,分別對應這三份 drawable:
<ImageView
android:id="@+id/imageview_animation_list_filling"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/animation_list_filling" />
<ImageView
android:id="@+id/imageview_animation_list_emptying"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/animation_list_emptying" />
<ImageView
android:id="@+id/imageview_animated_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector" />
拿到 ImageView 參照,把 background 轉成 AnimationDrawable 再 start:
ImageView mImageViewFilling = (ImageView) findViewById(R.id.imageview_animation_list_filling);
((AnimationDrawable) mImageViewFilling.getBackground()).start();

反向版本程式碼一模一樣,只換 id:
ImageView mImageViewEmptying = (ImageView) findViewById(R.id.imageview_animation_list_emptying);
((AnimationDrawable) mImageViewEmptying.getBackground()).start();

這些呼叫放進 onCreate 就會進頁面自動播,掛到 OnClickListener 上則是用戶點擊才播,看你要哪一種。
改用 selector 之後,動畫由狀態變化觸發,不需要手動 start。替 ImageView 掛個 click listener,在點擊時切換 activated 狀態就好:
mImageViewSelector.setOnClickListener(v ->
mImageViewSelector.setActivated(!mImageViewSelector.isActivated()));
使用者每點一次愛心,它就依當前狀態在實心與空心之間來回切換:

兩種做法的差別值得記一下:animation-list 是時間驅動、由程式碼決定何時播放;animated-selector 是狀態驅動、由 View 狀態決定播放哪一段過渡,並且天然具備向下相容的降級路徑。
SharedPreferences 的正確使用:App 設置界面的數據保存與讀取
歷經兩個多月的努力,項目即將在 11 月交付使用,基礎功能都已完成,只剩下系統設置界面還沒做完,如下圖: 這個界面的實現要點是:多個界面需要顯示大量數據,並依據某一項進
解決 strings.xml 格式化佔位符錯誤:Multiple substitutions specified in non-positional format
今天有位 Android 新手在使用 strings.xml 進行格式化時,遇到了佔位符報錯:Multiple substitutions specified in no
榮耀Note8怎麼裝SIM卡 雙卡與單卡安裝圖解
華為榮耀於8月1日下午發表了6.6吋大屏的榮耀Note8。剛拿到新機的朋友可能會疑惑:SIM卡該怎麼裝?下面就隨小編一起看示範。 華為榮耀Note8裝SIM卡步驟 雙SI
手把手實現 Android 文件管理器:源碼全解析
一、項目背景 如今不少移動設備都自帶資源管理器,但它們並非 Android 系統原生,而是手機廠商與 app 開發商合作的結果,通過第三方軟件預裝在出廠手機中,還不能自行