標籤:des android blog http io color ar os 使用
以下Drawables的功能協助你在應用中實現Material Design:
圖片資源著色
在android 5.0(api 21)和更高版本,可以著色bitmap和.9 png 通過定義透明度遮蓋。你可以著色通過使用顏色資源或者主題的屬性去解析顏色資源(比如,?android:attr/colorPrimary).通常我們建立一次,然後資源自適應主題。
你可以給BitmapDrawable或NinePatchDrawable對象著色使用setTint()方法。你可以可以在布局檔案中使用android:tint和android:tintMode屬性設定著色顏色和著色模式。
從圖片中抽取醒目提示
support library r21和更高的版本中包括了Palette類,可以從一個圖片中提取醒目提示。這個類可以提起以下幾種突出顏色:
Vibrant 充滿生機
Vibrant dark 暗的充滿生機
Vibrant light 亮的充滿生機
Muted 柔和
Muted dark 暗的柔和
Muted light 亮的柔和
傳遞一個Bitmap對象給靜態方法Palette.generate(),它會在後台線程幫你從後台線程提取顏色。如果你不能使用這個後台線程,使用Palette.generateAsync()方法,並且設定一個監聽器listener.
你可以從圖片中取得突出顏色使用Palette類中的getter方法,比如Palette.getVibrantColor.
在項目中使用Palette方法,需要在項目中包含v7包palette的jar, gradle dependecy添加的方式是:
...compile ‘com.android.support:palette-v7:21.0.+‘
下面這個是範例程式碼:
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() { public void onGenerated(Palette palette) { // Do something with colors... palette.getVibrantColor(Color.BLACK); //get a color in rgb value } });
更多資訊,請查看Paltette的api文檔:http://developer.android.com/reference/android/support/v7/graphics/Palette.html
建立向量drawables
在android 5.0和更高版本中,可以建立向量的drawable,在縮放的時候不會失真。你只需要定義一個向量圖片檔案,相反的,使用bitmap位元影像則需要針對不同的解析度建立多個檔案。建立一個向量圖片,你需要說明圖形的詳細,在xml檔案的 標籤下。
下面是一個例子:
<?xml version="1.0" encoding="utf-8"?><!-- res/drawable/heart.xml --><vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="256dp" android:height="256dp" android:viewportHeight="32" android:viewportWidth="32"> <!-- draw a path --> <path android:fillColor="#f15467" 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對象。更多資訊,查看Svg Path reference。
參考資料:http://developer.android.com/training/material/drawables.html
原文地址:http://blog.isming.me/2014/11/03/creating-app-with-material-design-four-drawables/,轉載請註明出處。
建立Material Design風格的Android應用--使用Drawable