今天我們講的這個Palette非常好用,也非常好玩。 Palette的作用是從圖像中提取突出的顏色,這樣我們可以根據提取到的色值把它賦給Toolbar,標題,狀態欄等,可以使我們的整個界面色調統一,效果非常好看。
Palette介紹
Palette顧名思義調色板, Palette的作用是可以從圖像中提取圖片的顏色。我們可以把提取的顏色融入到App UI中,可以使UI風格更加美觀融洽。
- Palette可以提取的顏色如下:
- Vibrant (有活力的)
- Vibrant dark(有活力的 暗色)
- Vibrant light(有活力的 亮色)
- Muted (柔和的)
- Muted dark(柔和的 暗色)
- Muted light(柔和的 亮色)
通過Palette對象獲取到六個樣本swatch
Palette.Swatch vibrantSwatch = palette.getVibrantSwatch();//1.活力顏色
Palette.Swatch lightVibrantSwatch = palette.getLightVibrantSwatch();//2.亮的活力顏色
Palette.Swatch darkVibrantSwatch = palette.getDarkVibrantSwatch();//3.暗的活力顏色
Palette.Swatch mutedSwatch = palette.getMutedSwatch();//4.柔色
Palette.Swatch lightMutedSwatch = palette.getLightMutedSwatch();//5.亮的柔色
Palette.Swatch darkMutedSwatch = palette.getDarkMutedSwatch();//6.暗的柔色
swatch對象對應的顏色方法
- getPopulation(): 像素的數量
- getRgb(): RGB顏色
- getHsl(): HSL顏色
- getBodyTextColor(): 用于內容文本的顏色
- getTitleTextColor(): 標題文本的顏色
Palette實例
Palette的一些方法:
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
//暗、柔和的顏色
int darkMutedColor = palette.getDarkMutedColor(Color.BLUE);//如果分析不出來,則返回默認顏色
//暗、柔和
int lightMutedColor = palette.getLightMutedColor(Color.GRAY);
//暗、鮮艷
int darkVibrantColor = palette.getDarkVibrantColor(Color.BLUE);
//亮、鮮艷
int lightVibrantColor = palette.getLightVibrantColor(Color.BLUE);
//柔和
int mutedColor = palette.getMutedColor(Color.BLUE);
//柔和
int vibrantColor = palette.getVibrantColor(Color.BLUE);
//獲取某種特性顏色的樣品
Palette.Swatch vibrantSwatch = palette.getVibrantSwatch();//1.活力顏色
Palette.Swatch lightVibrantSwatch = palette.getLightVibrantSwatch();//2.亮的活力顏色
Palette.Swatch darkVibrantSwatch = palette.getDarkVibrantSwatch();//3.暗的活力顏色
Palette.Swatch mutedSwatch = palette.getMutedSwatch();//4.柔色
Palette.Swatch lightMutedSwatch = palette.getLightMutedSwatch();//5.亮的柔色
Palette.Swatch darkMutedSwatch = palette.getDarkMutedSwatch();//6.暗的柔色
//谷歌推薦的:圖片的整體的顏色rgb的混合值---主色調
int rgb = vibrantSwatch.getRgb();
//谷歌推薦:圖片中間的文字顏色
int bodyTextColor = vibrantSwatch.getBodyTextColor();
//谷歌推薦:作為標題的顏色(有一定的和圖片的對比度的顏色值)
int titleTextColor = vibrantSwatch.getTitleTextColor();
//顏色向量
float[] hsl = vibrantSwatch.getHsl();
//分析該顏色在圖片中所占的像素多少值
int population = vibrantSwatch.getPopulation();
}
});
private int getTranslucentColor(float percent, int rgb) {
// 10101011110001111
int blue = Color.blue(rgb);
int green = Color.green(rgb);
int red = Color.red(rgb);
int alpha = Color.alpha(rgb);
//上面四個的原理也就是下面的方法
// int blue = rgb & 0xff;
// int green = rgb>>8 & 0xff;
// int red = rgb>>16 & 0xff;
// int alpha = rgb>>>24;
alpha = Math.round(alpha*percent);
return Color.argb(alpha, red, green, blue);
}
Palette經常用于和ViewPager,Fragment搭配使用,當我們的Pager切換時伴隨著Fragment的變化,而Fragment里的內容一般是不同的,所以每個Fragment里的一般視覺效果也是不同的,所以我們可以用Palette來去提取Fragment中的主色調,把這個主色調用于整體的UI風格。
先看效果圖,如下:
效果圖
第一步:添加依賴
compile 'com.android.support:palette-v7:23.4.0'
第二步:創建Palette對象,并獲取圖片的顏色值
// 用來提取顏色的Bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), PaletteFragment.getBackgroundBitmapPosition(position));
// Palette的部分
Palette.Builder builder = Palette.from(bitmap);
builder.generate(new Palette.PaletteAsyncListener() {@Override public void onGenerated(Palette palette) {
//獲取到充滿活力的這種色調
Palette.Swatch vibrant = palette.getVibrantSwatch();
//根據調色板Palette獲取到圖片中的顏色設置到toolbar和tab中背景,標題等,使整個UI界面顏色統一
toolbar_tab.setBackgroundColor(vibrant.getRgb());
toolbar_tab.setSelectedTabIndicatorColor(colorBurn(vibrant.getRgb()));
toolbar.setBackgroundColor(vibrant.getRgb());
if (android.os.Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.setStatusBarColor(colorBurn(vibrant.getRgb()));
window.setNavigationBarColor(colorBurn(vibrant.getRgb()));
}
}
});
就是這么簡單,這里略過了對TabLayout的講解,因為這次主講的是Palette嘛,沒記錯的話,以前講解過TabLayout的使用,不會的同學可以去看源碼或者是查找歷史消息去看看文章。