標籤:material material design drawable
轉載請註明出處:http://blog.csdn.net/bbld_/article/details/40584331
下面的幾點drawables 的功能協助你在你的app中實現material design:
l 可繪製著色
l 突出的顏色提取
l 向量可繪性
本章節向你展示了怎麼在你的app中使用這些功能。
色調Drawable資源
如果使用Android5.0(API層級21)以上的系統版本,你可以著色位元影像和9patch圖作為透明度蒙板。你可以使用顏色資源或者主題屬性中的顏色資源(例如,?android:attr/colorPrimary)給它們著色。通常,你只需一次即可建立這些資源,並自動將它們上色以匹配你的主題。
你可以使用setTint()方法給位元影像資源或者9patch資源對象著色。你還可以在你的布局中使用android:tint屬性和android:tintMode屬性設定著色的顏色和模式。
從映像中提取突出的顏色
Android r21或以上的支援庫中包含了Palette類,它能讓你從映像中提取突出的顏色。這個類能提取以下突出的顏色:
l Vibrant(充滿活力的)
l Vibrant dark(充滿活力的黑)
l Vibrant light(充滿活力的亮)
l Muted(柔和的)
l Muted dark(柔和的黑)
l Muted lighr(柔和的亮)
要提取這些顏色,在你載入圖片的後台線程中傳遞一個位元影像對象給Palette.generate()靜態方法。如果你不適用線程,則調用Palette.generateAsync()方法並且提供一個監聽器去替代。
你可以在Palette類中使用getter方法來從檢索突出的顏色,比如Palette.getVibrantColor。
要在你的項目中使用Palette類,增加下面的Gradle依賴到你的程式的模組(module)中:
dependencies { ... compile 'com.android.support:palette-v7:21.0.+'}
補充:在Eclipse中使用Palette類:
很簡單,把sdk裡的extras裡的v7支援庫裡的palette支援jar包複製到你項目的libs檔案夾即可。
更多的資訊,請參閱Palette類的API文檔說明。
建立向量(vector)Drawables
在Android 5.0(API層級21)或以上的系統中,則可以定義向量drawables,它可以在不失清晰度的情況下進行縮放。你僅僅需要需要一個向量圖片的資源檔,而需要為每個螢幕密度設定一個資源檔。要建立一個向量圖片,你需要定義形狀元素的細節在<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中被表示為VectorDrawable對象。更多有關pathData文法的資訊,請參閱SVG Path 的文檔參考。更多有關動畫向量drawable屬性,請參閱AnimatingVector Drawables(未更新)。
Demo示範效果:
demo源碼:http://download.csdn.net/detail/bbld_/8094905
Android Material Design-Working with Drawables(使用Drawable)-(五)