page.title=使用可繪項目 @jd:body

本課程示範

  1. 為可繪項目資源著色
  2. 從影像提取顯著顏色
  3. 建立矢量可繪

您也應該閱讀

下列可繪項目功能可以幫助您在應用程式中運用材料設計:

本課程示範如何在應用程式中使用這些功能。

為可繪項目資源著色

若您使用 Android 5.0 (API 級別 21) 或以上版本,您可對點陣圖和定義為 Alpha 遮罩的九宮格影像著色。 您可使用顏色資源或解析為顏色資源的設計風格屬性 (如 ?android:attr/colorPrimary 為圖片著色。 這些資產您通常只會建立一次,然後自動著色以符合您的設計風格。

您可使用 {@code setTint()} 方法對 {@link android.graphics.drawable.BitmapDrawable} 或 {@link android.graphics.drawable.NinePatchDrawable} 物件套用著色。您也可以使用 android:tintandroid:tintMode 屬性,設定版面配置中的著色顏色和模式。

從影像提取顯著顏色

Android 支援程式庫 r21 和更新版本包含 {@link android.support.v7.graphics.Palette} 類別,可讓您從影像提取出顯著的顏色。此類別會提取下列顯著顏色:

如果要提取這些顏色,請將 {@link android.graphics.Bitmap} 物件傳遞到載入影像之背景執行緒中的 {@link android.support.v7.graphics.Palette#generate Palette.generate()} 靜態方法。 如果您無法使用該執行緒,請改為呼叫 {@link android.support.v7.graphics.Palette#generateAsync Palette.generateAsync()} 方法並提供接聽程式。

您可使用 Palette 類別中的 getter 方法 (如 Palette.getVibrantColor 從影像擷取顯著顏色。

如果要在專案中使用 {@link android.support.v7.graphics.Palette} 類別,請在應用程式的模組中加入下列 Gradle 相依性

dependencies {
    ...
    compile 'com.android.support:palette-v7:21.0.0'
}

如需詳細資訊,請參閱 {@link android.support.v7.graphics.Palette} 類別的 API 參考資料。

建立矢量可繪

影片

Android Vector Graphics

在 Android 5.0 (API 級別 21) 或以上版本中,您可定義矢量可繪,這種圖形可以調整大小但不會遺失定義。 一個矢量影像只需要一個資產檔案,但點陣圖影像的每一種螢幕密度都需要一個資產檔案。 如果要建立矢量影像,您必須在 <vector> XML 元素內定義圖形的詳細資料。

下列範例定義一個具有心形圖案的矢量影像:

<!-- res/drawable/heart.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    <!-- intrinsic size of the drawable -->
    android:height="256dp"
    android:width="256dp"
    <!-- size of the virtual canvas -->
    android:viewportWidth="32"
    android:viewportHeight="32">

  <!-- draw a path -->
  <path android:fillColor="#8fff"
      android:pathData="M20.5,9.5
                        c-1.955,0,-3.83,1.268,-4.5,3
                        c-0.67,-1.732,-2.547,-3,-4.5,-3
                        C8.957,9.5,7,11.432,7,14
                        c0,3.53,3.793,6.257,9,11.5
                        c5.207,-5.242,9,-7.97,9,-11.5
                        C25,11.432,23.043,9.5,20.5,9.5z" />
</vector>

在 Android 中,{@link android.graphics.drawable.VectorDrawable} 物件代表矢量影像。 如需有關 pathData 語法的詳細資訊,請參閱 SVG 路徑參考資料。如需有關繪製矢量可繪屬性的詳細資訊,請參閱動畫顯示矢量可繪