編輯:關於 Android 程式設計
幾行程式碼加幾個設定檔,就能做出流程順暢、無縫銜接的向量動畫。

範例程式碼:animated-vector-drawable
從 Android 5.0(API level 21)起,平台支援向量 drawable,可以在不失清晰度的前提下任意縮放。你只需要準備一份向量圖的資源檔,不必再為每種螢幕密度各準備一張點陣圖。做法是在 res/drawable 下建立一個 vector XML 檔,描述其中的路徑元素。
先看笑臉的 vector 檔:

在 Android 中,向量圖會被表示為 VectorDrawable 物件。初次看到 pathData 那串語法通常會有點困惑,建議直接參考 SVG Path 的規範文件。把 PathData 的語法學熟之後,幾乎什麼圖形都畫得出來——社群裡就有人用它畫出相當複雜的插畫。

看起來越是複雜的語法,往往代表越高的自由度。
AnimatedVectorDrawable 類別負責為向量資源加上動畫。通常在三個 XML 檔中完成定義:
向量動畫可以對 group 與 path 元素的屬性做動畫。group 定義了一組路徑或子群組,path 則定義要繪製的具體路徑。
撰寫動畫前,記得先在向量資源中透過 android:name 屬性為 group 與 path 指派唯一名稱,動畫定義才能引用到它們。
先想一個問題:它是怎麼在旋轉的同時改變形狀的?
小三角的 vector 檔:

接著是 animated-vector 檔:
<animated-vector
xmlns:android="https://schemas.android.com/apk/res/android"
android:drawable="@drawable/v">
<target
android:name="rotationGroup"
android:animation="@anim/rotation" />
<target
android:name="v"
android:animation="@anim/path_morph" />
</animated-vector>
可以看到它掛了兩個動畫:一個旋轉、一個改變路徑形狀。
旋轉動畫(res/anim/rotation.xml):
<set xmlns:android="https://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="6000"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360" />
</set>
形狀變化動畫(res/anim/path_morph.xml):
<set xmlns:android="https://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="3000"
android:propertyName="pathData"
android:valueFrom="M300,70l0,-70 70,70 0,-70,70z"
android:valueTo="M300,70l0,-70 70,0 0,140 -70,0z"
android:valueType="pathType" />
</set>
這裡有個關鍵:pathData 動畫要求前後兩個路徑的指令數量與型別完全一致,否則無法插值。這也是為什麼 valueFrom 與 valueTo 看起來長度相近、只是座標不同。
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ExampleActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin"
android:drawableBottom="@drawable/avd"
android:text="@string/example_from_documentation" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin"
android:drawableBottom="@drawable/avd_rotation_only"
android:text="@string/rotation_only" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/margin"
android:drawableBottom="@drawable/avd_path_morph_only"
android:text="@string/path_morph_only" />
</LinearLayout>
三個 TextView 的設定方式一樣,只看第一個就好:透過 android:drawableBottom 掛上 animated-vector 資源。
拿到 Drawable 之後判斷是否為 Animatable,是的話直接 start():
TextView textView = (TextView) view;
for (Drawable drawable : textView.getCompoundDrawables()) {
if (drawable instanceof Animatable) {
((Animatable) drawable).start();
}
}
就這幾行,快去試試。
Android Matrix 矩陣詳解與進階(一):原理推導與基礎方法全解
1. 概述 這一系列會把 Matrix 的每個方法與用法完整講一遍。Matrix 就是「矩陣」。前面談 ColorFilter 時我們見過 ColorMatrix,那是一
Android App 透過 Intent 互相呼叫的實用指南
在 Android 應用開發中,Intent 是連結不同應用與元件的重要橋樑。它讓應用可以不必自行實作地圖、電話或郵件等功能,而是把「想要完成的操作」交給系統,由系統找出
Android ListView 字母排序:實現字母擠壓、右側快速定位與關鍵字搜索
本文將講解如何自定義 EditText 實現搜索框的自定義樣式,以及字母擠壓效果的實現思路,並最終完成一個具備字母排序、右側快速選取與關鍵字搜索功能的通訊錄列表。 自定義
Android 取得系統時間與星期:SimpleDateFormat 格式化用法整理
Android 上取得當前系統時間的做法有好幾種,本文採用的是 System.currentTimeMillis() 搭配 Date 與 SimpleDateFormat