Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 5.0 學習筆記:AnimatedVectorDrawable 入門

編輯:關於 Android 程式設計

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

範例程式碼:animated-vector-drawable

VectorDrawable

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

先看笑臉的 vector 檔:

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

看起來越是複雜的語法,往往代表越高的自由度。

AnimatedVectorDrawable

AnimatedVectorDrawable 類別負責為向量資源加上動畫。通常在三個 XML 檔中完成定義:

  • res/drawable/:vector 元素組成的向量資源。
  • res/drawable/:animated-vector 元素組成的向量動畫。
  • res/anim/:一個或多個 objectAnimator 元素。

向量動畫可以對 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();
    }
}

就這幾行,快去試試。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved