編輯:關於 Android 程式設計
本文參考〈Android 屬性動畫完全解析(上)——初識屬性動畫的基本用法〉整理而成。
Android 3.0 之前只有兩種動畫:Frame 動畫與Tween 動畫(補間動畫)。Frame 動畫是把多張圖連續播放,效果近似 GIF;Tween 動畫則是對圖形做平移、縮放、旋轉、透明度變化。
問題在於,Tween 動畫又被稱為 View Animation——顧名思義,它只能作用在 View 上。如果你想讓一個自定義 View 的某個屬性(例如背景顏色)產生動畫,Tween 是做不到的。它並沒有真的改變物件的屬性,只是在繪製時做了一層視覺變換,因此動畫結束後 View 的位置、大小其實都沒變,事件分發區域也還在原地。
Android 3.0 引入的屬性動畫(Property Animation)解決了這個問題:它直接改變物件的屬性值,動畫結束後狀態是真的變了。
實務上最常用的是 ObjectAnimator。往上追,它繼承自 ValueAnimator——後者能產生連續變化的數值,但你不必直接操作它,因為 ObjectAnimator 已經幫你把數值套到指定屬性上了。
透明度變化:
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "alpha", 1f, 0.5f); animator.setDuration(5000); animator.start();
看看 ofFloat 的原始碼:
public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
ObjectAnimator anim = new ObjectAnimator(target, propertyName);
anim.setFloatValues(values);
return anim;
}
參數依序是:要做動畫的物件、屬性名稱、以及可變長度的數值序列。上面那行的意思是讓 tv 的 alpha 從 1 變到 0.5。
後面的參數可以傳任意多個,例如:
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "alpha", 1f, 0.5f, 1f, 0f);
這表示:不透明 → 半透明 → 不透明 → 全透明,一氣呵成。理解這個模式之後,其他 Tween 效果都能改寫出來。
旋轉:
ObjectAnimator animator2 = ObjectAnimator.ofFloat(tv2, "rotation", 0f, 360f); animator2.setDuration(5000); animator2.start();
平移:
float curTranslationX = tv.getTranslationX();
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationX",
curTranslationX, -1000f, curTranslationX);
animator.setDuration(3000);
animator.start();
這裡先讀取目前位置作為起點與終點,讓 View 從原位飛出再飛回,是很實用的寫法。
ObjectAnimator moveIn = ObjectAnimator.ofFloat(tv, "translationX", -500f, 0f); ObjectAnimator rotate = ObjectAnimator.ofFloat(tv, "rotation", 0f, 360f); ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(tv, "alpha", 1f, 0f, 1f); AnimatorSet animSet = new AnimatorSet(); animSet.play(rotate).with(fadeInOut).after(moveIn); animSet.setDuration(5000); animSet.start();
AnimatorSet 的編排方法:
// after(Animator anim) 在傳入的動畫之後執行 // after(long delay) 延遲指定毫秒後執行 // before(Animator anim) 在傳入的動畫之前執行 // with(Animator anim) 與傳入的動畫同時執行
因此上面那段的意思是:先做 moveIn(由左飛入),完成後 rotate 與 fadeInOut 同時進行(旋轉並閃一下)。
屬性動畫同樣可以寫在 XML 中,放在 res/animator/ 目錄下,再用 AnimatorInflater.loadAnimator() 載入:
<set xmlns:android="https://schemas.android.com/apk/res/android"
android:ordering="sequentially">
<set android:ordering="together">
<objectAnimator
android:propertyName="translationX"
android:valueFrom="-500"
android:valueTo="0"
android:duration="1000" />
<objectAnimator
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360"
android:duration="1000" />
</set>
</set>
android:ordering 有兩個值:together(各動畫同時發生,也是預設值)與 sequentially(依序發生)。<set> 可以嵌套,因此能做到「先 A、然後(B 與 C 同時)」這種混合編排。
這是最容易踩的坑。ObjectAnimator 靠反射尋找 setXxx() 方法,因此屬性名稱必須符合 JavaBean 命名慣例:
"alpha" → 需要 setAlpha(float)"translationX" → 需要 setTranslationX(float)"backgroundColor" → 需要 setBackgroundColor(int)(注意型別要是 int,此時該用 ofInt())如果自定義 View 沒有對應的 setter,動畫會在執行時靜默失敗——畫面沒有任何變化,也不報錯。這種問題很難查,記得先確認方法存在。
translationX / translationY:位移rotation / rotationX / rotationY:旋轉(後兩者為三維翻轉)scaleX / scaleY:縮放alpha:透明度x / y:相對於父容器的絕對位置插值器(Interpolator)。用 setInterpolator(new AccelerateDecelerateInterpolator()) 可以改變速度曲線。Android 也支援在 XML 中透過 android:interpolator 指定,並可用 <keyframe> 為單一動畫設定不同的插值器。
Evaluator。若屬性不是 float 或 int(例如 Color 或直接操作自定義物件),需要搭配自定義 TypeEvaluator 或使用內建的 ArgbEvaluator。這也是屬性動畫比補間動畫強大的地方——它能操作任意型別的屬性。
ViewPropertyAnimator 更簡潔。如果目標只是 View 的常見屬性,view.animate().translationX(100).rotation(360).setDuration(300).start() 這種寫法比 ObjectAnimator 更短,效能也更好(它會批次提交屬性更新)。單一屬性動畫建議優先用它。
RecyclerView 多種 Item 布局加載實作
RecyclerView 要實現多種 Item 布局,關鍵在於 Adapter 的 getItemViewType() 與 onCreateViewHolder()。開發
Android 簡易實戰教程 第七話:內部儲存實作「記住帳號密碼」
本話要實作一個常見的功能:勾選「記住用戶名和密碼」後,將資料寫入內部儲存空間,下次開啟時自動回顯。先來看版面配置檔: 接著是 Activity 的程式碼:
Android 定位策略實踐:在精度、電量與用戶體驗之間找平衡
說明:本文討論的策略,是針對 Android 平台上 android.location 套件中的定位 API。它與 Google Play Services 提供的 Go
使用 LocalBroadcastManager 實現 Android 組件通信
在 Android 中,組件之間的通信方式有很多種,例如接口回調、EventBus、LiveData、Handler 以及廣播。其中,LocalBroadcastMana