編輯:關於 Android 程式設計
上一篇談的是比較早期的 Tween 動畫(Animation),這次換到 Android 3.0 之後才加入的新家族——屬性動畫(Animator),主角是其中的 ObjectAnimator。
兩者最大的差別在於:Tween 動畫只是「把畫面畫成那個樣子」,View 的真實屬性並沒有改變,所以點擊事件的位置可能對不上;屬性動畫則是真正去改物件的屬性值,動畫結束後狀態會確實留下來。這是為什麼新專案幾乎都該選 ObjectAnimator。
// 第一個參數是 View 物件,第二個是要動的屬性名稱, // 後兩個參數依序為起始透明度與結束透明度。 ObjectAnimator alpha = ObjectAnimator.ofFloat(text, "alpha", 0f, 1f); alpha.setDuration(2000); // 動畫時長 alpha.setInterpolator(new DecelerateInterpolator()); // 減速插入器 alpha.setRepeatCount(-1); // -1 代表無限重複 alpha.setRepeatMode(Animation.REVERSE); // 往返循環 alpha.start();
X、Y 軸各自一條 ObjectAnimator,再用 AnimatorSet 綁在一起同時播放:
AnimatorSet animatorSet = new AnimatorSet(); ObjectAnimator scaleX = ObjectAnimator.ofFloat(text, "scaleX", 1f, 0f); ObjectAnimator scaleY = ObjectAnimator.ofFloat(text, "scaleY", 1f, 0f); animatorSet.setDuration(2000); animatorSet.setInterpolator(new DecelerateInterpolator()); animatorSet.play(scaleX).with(scaleY); // 兩條動畫同時開始 animatorSet.start();
ObjectAnimator translationUp = ObjectAnimator.ofFloat(button, "Y", button.getY(), 0); translationUp.setInterpolator(new DecelerateInterpolator()); translationUp.setDuration(1500); translationUp.start();
這裡直接讀 button.getY() 當作起點,比寫死數字更可靠,版面調整時也不用回頭改參數。
用 before() 排出先後順序,做出翻牌式的效果:
AnimatorSet set = new AnimatorSet(); ObjectAnimator anim = ObjectAnimator.ofFloat(phone, "rotationX", 0f, 180f); anim.setDuration(2000); ObjectAnimator anim2 = ObjectAnimator.ofFloat(phone, "rotationX", 180f, 0f); anim2.setDuration(2000); ObjectAnimator anim3 = ObjectAnimator.ofFloat(phone, "rotationY", 0f, 180f); anim3.setDuration(2000); ObjectAnimator anim4 = ObjectAnimator.ofFloat(phone, "rotationY", 180f, 0f); anim4.setDuration(2000); set.play(anim).before(anim2); // 先跑 anim,再跑 anim2 set.play(anim3).before(anim4); set.start();
除了 before(),AnimatorSet 還提供 after()、with(),足以應付絕大多數的序列與並行組合需求。
ObjectAnimator translationUp = ObjectAnimator.ofInt(button, "backgroundColor",
Color.RED, Color.BLUE, Color.GRAY, Color.GREEN);
translationUp.setInterpolator(new DecelerateInterpolator());
translationUp.setDuration(1500);
translationUp.setRepeatCount(-1);
translationUp.setRepeatMode(Animation.REVERSE);
// 因為要對 backgroundColor 做插值,所以指定 ArgbEvaluator
translationUp.setEvaluator(new ArgbEvaluator());
translationUp.start();
這裡的關鍵是 Evaluator。ObjectAnimator 本身只負責「在時間軸上推進」,至於中間每一幀該長什麼樣子,交給 Evaluator 決定。常見的幾種:
ArgbEvaluator:對代表 ARGB 顏色的整數做插值,專門處理顏色。FloatEvaluator:對浮點數做插值。IntEvaluator:對 int 型別做插值。RectEvaluator:對矩形值做插值。如果自訂屬性時預設的 Evaluator 不合用,也可以自行繼承 TypeEvaluator 實作屬於自己的插值邏輯。
SpannableString 完全指南:讓 TextView 的文字千變萬化
一個人需要隱藏多少秘密,才能巧妙地度過一生 —— 倉央嘉措 前言 前陣子看到一款學習類 App 有個很有意思的功能:一段英文文本裡,點擊任意單詞就能朗讀出來。語音暫且不
Android 自定義 SubmitView:提交動畫、進度圈與成功狀態
SubmitView 的互動情境 在提交表單、下單或上傳檔案時,使用者常需要一個能表達等待與完成的元件。SubmitView 可用來呈現提交按鈕、載入進度、成功勾選與重置
Android 7.0 PowerManagerService 解析:WakeLock 的使用與流程
在行動裝置上,電量是有限資源。Android 為了節省電力,會在空閒時讓系統進入休眠。但系統中有許多執行緒與服務可能仍需要執行重要工作,因此需要一套機制來表達「目前仍有任
Android 滑動菜單的簡潔實作(SlidingPaneLayout 入門)
SlidingPaneLayout 是 Android 團隊在 2013 年 Google I/O 大會上隨 Support V4 庫一同推出的進階佈局控制項。它能讓開發