這幾天為了學些android5.0版本sdk的新特性,折騰了好久。AndroidStudio被我反復的安裝又卸載又安裝,在eclipse和AndroidStudio
之間來回折騰。沒想到sdk升級到5.0版本,竟然會出這么多的麻煩。一開始還想着繼續用eclipse,但是被各種升級插件以及導包折磨的死去活來,
換成AndroidStudio,結果電腦總是卡成狗!我都無語死了,后來在百般折騰下,最終還是拋棄了eclipse,使用AndroidStuidio。現在還是
不很習慣AndroidStudio的操作。以后會慢慢習慣吧。
好了吐槽完畢。既然工具已經搞定了,就開始迫不及待的看看android 5.0X都新增了哪些新特性。今天就來說說我學習的第一個新特性,
即調色板Palette。因為這個Palette可以從一張Bitmap圖片中提取你所需要的色調,這樣子極大了方便開發者來保持app的顏色觀和諧統一。
下面是我的收獲和一個小的例子。
一、使用前的准備
首先需要在gradle中添加依賴。
即在你build.gradle的dependencies中添加appconat-v7和palette-v7的依賴。如下:
dependencies {
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:palette-v7:21.+'
}
二、關於Palette的使用API
首先是獲取到一個Palette對象,有四種方式來獲取,如下:
(一)
Palette p = Palette.generate(bitmap);
(二)
Palette p = Palette.generate(bitmap, 24);
(三)
Palette.generateAsync(bitmap,
new
Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
}
});
Palette.generateAsync(bitmap, 24,
new
Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {
}
});
注:(1)前兩種方法為同步獲取,后兩種方法為異步獲取,使用哪一種都行。異步獲取到的palette在
onGenerated方法的參數中。因此一般顏色設定的邏輯也在這個方法里。
(2)在獲取Palette對象時,可以指定它的size。一般size越大獲取需要的時間也就越長。不指定
時,默認的調色板大小,即size為16.
(3)size大小多少為合適呢?一般來說頭像之類的設定,最好在24~32,風景大圖之類的,一般在
8~16之間。
獲取到Palette就該使用它了,即接着通過Palette對象獲取到一個樣本swatch,有6中樣本,如下:
Palette.Swatch s = p.getVibrantSwatch(); //獲取到充滿活力的這種色調
Palette.Swatch s = p.getDarkVibrantSwatch(); //獲取充滿活力的黑
Palette.Swatch s = p.getLightVibrantSwatch(); //獲取充滿活力的亮
Palette.Swatch s = p.getMutedSwatch(); //獲取柔和的色調
Palette.Swatch s = p.getDarkMutedSwatch(); //獲取柔和的黑
Palette.Swatch s = p.getLightMutedSwatch(); //獲取柔和的亮
最后我們就可以利用采集的色調樣本swatch對象給需要的東西賦予顏色了,有如下幾個方法:
getPopulation(): the amount of pixels which
this
swatch represents.
getRgb(): the RGB value of
this
color.
getHsl(): the HSL value of
this
color.
getBodyTextColor(): the RGB value of a text color which can be displayed on top of
this
color.
getTitleTextColor(): the RGB value of a text color which can be displayed on top of
this
color.
比如你的TextView有個背景圖片,你可以使用Palette獲取到這個背景圖片的色調,然后利用getBodyTextColor
來給這個TextView的文字設定顏色,從而與背景圖片比較相稱!
三,一個小小的例子
用AndroidStudio新建一個項目,然后修改它的activity_main.xml代碼。如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical"> 7 8 <TextView 9 android:id="@+id/mytext_view" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:textSize="30sp" 13 android:text="Hello World!" /> 14 <Button 15 android:id="@+id/btn" 16 android:layout_width="match_parent" 17 android:layout_height="50dp" 18 android:background="@drawable/palette" 19 android:text="我是按鈕"/> 20 </LinearLayout>
很簡單,放置了一個TextView和一個按鈕,且給按鈕設置了一個背景圖片。下面我要做的就是從
這張背景圖片上提取色調來給整個布局賦予顏色。修改MainActivity的代碼如下:
1 package kun.fuly.myapplication; 2 3 import android.annotation.TargetApi; 4 import android.graphics.Bitmap; 5 import android.graphics.BitmapFactory; 6 import android.graphics.drawable.ColorDrawable; 7 import android.os.Build; 8 import android.os.Bundle; 9 import android.support.v7.app.ActionBarActivity; 10 import android.support.v7.graphics.Palette; 11 import android.widget.Button; 12 import android.widget.TextView; 13 14 15 16 public class MainActivity extends ActionBarActivity { 17 18 private Bitmap bmp; 19 private TextView myText; 20 private Button btn; 21 22 23 @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main); 27 28 bmp = BitmapFactory.decodeResource(getResources(), R.drawable.palette); 29 30 myText = (TextView) findViewById(R.id.mytext_view); 31 32 btn = (Button) findViewById(R.id.btn); 33 34 Palette p = Palette.generate(bmp ); 35 36 Palette.Swatch s = p.getVibrantSwatch(); 37 38 //給TextView設置背景顏色和文本顏色 39 myText.setBackground(new ColorDrawable(s.getRgb())); 40 41 myText.setTextColor(s.getBodyTextColor()); 42 43 //為按鈕的文本設置顏色 44 btn.setTextColor(s.getBodyTextColor()); 45 46 47 //為actionbar設置顏色 48 getSupportActionBar().setBackgroundDrawable(new ColorDrawable(s.getRgb())); 49 } 50 51 52 }
代碼很簡單,沒什么好解釋的。運行,效果如下:
好吧,看起來還是很丑,不過顏色還算是和諧。相信Palette在你的手里一定能運用的非常漂亮。其他的就不再說了,
關於Palette的介紹到此為止。