編輯:關於 Android 程式設計
動畫能讓 UI 更有活力、更具時尚感。Android 中的動畫主要分為兩類:補間動畫(Tween Animation)只需定義開始與結束狀態,中間幀由系統運算補齊;逐幀動畫(Frame Animation)則是把一張張圖片連起來播放。本文聚焦於属性動畫(Animator)體系下的 ObjectAnimator,示範平移、透明、旋轉、縮放四種效果如何用程式碼實現。
透過 translationX 與 translationY 改變位置,再用 AnimatorSet 串接多段動畫,讓目標「從哪裡出發、最後回到哪裡」:
ObjectAnimator anim = ObjectAnimator.ofFloat(phone, "translationX", -500f, 0f); // X:從 -500 回到原位 ObjectAnimator anim3 = ObjectAnimator.ofFloat(phone, "translationX", 0f, -500f); // X:從原位到 -500 ObjectAnimator anim2 = ObjectAnimator.ofFloat(phone, "translationY", 0f, -500f); // Y:從原位到 -500 ObjectAnimator anim4 = ObjectAnimator.ofFloat(phone, "translationY", -500f, 0f); // Y:從 -500 回到原位 anim.setDuration(2000); anim2.setDuration(2000); anim3.setDuration(2000); anim4.setDuration(2000); AnimatorSet set3 = new AnimatorSet(); set3.play(anim4).before(anim3); AnimatorSet set2 = new AnimatorSet(); set2.play(anim2).before(set3); AnimatorSet set = new AnimatorSet(); set.play(anim).before(set2); set.start();
透過 before() 把多段動畫串成一條時間線,就能讓目標沿著 X、Y 軸移動一整圈後回到原點。
ObjectAnimator anim = ObjectAnimator.ofFloat(phone, "alpha", 1f, 0f); // 由可見到不可見 ObjectAnimator anim2 = ObjectAnimator.ofFloat(phone, "alpha", 0f, 1f); // 由不可見到可見 anim.setDuration(2000); anim2.setDuration(2000); AnimatorSet set = new AnimatorSet(); set.play(anim).before(anim2); set.start();
ObjectAnimator anim = ObjectAnimator.ofFloat(phone, "rotationX", 0f, 180f); // X:0→180 ObjectAnimator anim2 = ObjectAnimator.ofFloat(phone, "rotationX", 180f, 0f); // X:180→0 ObjectAnimator anim3 = ObjectAnimator.ofFloat(phone, "rotationY", 0f, 180f); // Y:0→180 ObjectAnimator anim4 = ObjectAnimator.ofFloat(phone, "rotationY", 180f, 0f); // Y:180→0 anim.setDuration(2000); anim2.setDuration(2000); anim3.setDuration(2000); anim4.setDuration(2000); AnimatorSet set = new AnimatorSet(); set.play(anim).before(anim2); set.play(anim3).before(anim4); // 這樣寫可讓 X、Y 同時旋轉 set.start();
ObjectAnimator anim = ObjectAnimator.ofFloat(phone, "scaleX", 1f); // 縮小 X ObjectAnimator anim2 = ObjectAnimator.ofFloat(phone, "scaleX", 0.5f); ObjectAnimator anim3 = ObjectAnimator.ofFloat(phone, "scaleY", 1f); // 縮小 Y ObjectAnimator anim4 = ObjectAnimator.ofFloat(phone, "scaleY", 0.5f); ObjectAnimator anim5 = ObjectAnimator.ofFloat(phone, "scaleX", 0.5f); // 放大 X ObjectAnimator anim6 = ObjectAnimator.ofFloat(phone, "scaleX", 1f); ObjectAnimator anim7 = ObjectAnimator.ofFloat(phone, "scaleY", 0.5f); // 放大 Y ObjectAnimator anim8 = ObjectAnimator.ofFloat(phone, "scaleY", 1f); // 依序設定 duration 後,用 AnimatorSet 串接:先縮小、再放大
這四種效果的共同套路是:用 ObjectAnimator.ofFloat() 指定要動的屬性(translationX、alpha、rotationX、scaleX…)與起訖值,設定 duration,再用 AnimatorSet 的 play().before() 或 with() 編排先後與並行關係,最後 start()。熟悉這套組合,就能變化出豐富流暢的動態效果。
打造絢麗的字體大小控制項 FontSliderBar:仿 iOS UISliderBar 的 Android 自訂 View
熟悉 iOS 的人應該知道有個 UISliderBar 控制項,iPhone 的「調整文字大小」就用了它。最近產品提出一個需求:讓使用者能調整 App 內部分字體的大小。
手機QQ怎麼截圖?QQ瀏覽器網頁長截圖教學
想把某段對話存成圖片,或保存網頁上的一段重要資訊,都需要截圖。手機QQ內建了截屏功能,而QQ瀏覽器則有另一套更完整的網頁擷取工具——兩者的操作邏輯不太一樣,分開說明比較清
Android 練功專案:動手做一個 MP3 播放器(上篇)
Android 要掌握的知識點實在不少,唯有持續動手才能內化。前陣子我決定拿 MP3 播放器來練功——這個題目幾乎涵蓋了 Android 開發中相當一部分核心概念:Lis
實戰:如何把 React Native 嵌入原生 Android 應用
原生應用並非只能整屏跳轉到 RN 頁面,也可以把 RN 組件當作一個普通控件嵌進原生界面,成為其中的一部分。本文記錄完整的接入過程,以及一路上遇到的各類報錯與解法。 第