1.Palette 調色板
作用是提取頁面圖像突出的顏色,以此去修改statusBar與navigationBar的顏色,使頁面更加和諧。
使用:
1.添加依賴
compile 'com.android.support:palette-v7:23.4.0'
2.使用
/**
* 根據Palette提取的顏色,修改tab和toolbar以及狀態欄的顏色
*/
private void changeTopBgColor(int position) {
// 用來提取顏色的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()));
}
}
});
}
**
* 顏色加深處理
*
* @param RGBValues RGB的值,由alpha(透明度)、red(紅)、green(綠)、blue(藍)構成,
* Android中我們一般使用它的16進制,
* 例如:"#FFAABBCC",最左邊到最右每兩個字母就是代表alpha(透明度)、
* red(紅)、green(綠)、blue(藍)。每種顏色值佔一個字節(8位),值域0~255
* 所以下面使用移位的方法可以得到每種顏色的值,然後每種顏色值減小一下,在合成RGB顏色,顏色就會看起來深一些了
* @return
*/
private int colorBurn(int RGBValues) {
int alpha = RGBValues >> 24;
int red = RGBValues >> 16 & 0xFF;
int green = RGBValues >> 8 & 0xFF;
int blue = RGBValues & 0xFF;
red = (int) Math.floor(red * (1 - 0.1));
green = (int) Math.floor(green * (1 - 0.1));
blue = (int) Math.floor(blue * (1 - 0.1));
return Color.rgb(red, green, blue);
}
3,預設可選類型
Palette可以提取的顏色如下:
Vibrant (有活力的)
Vibrant dark(有活力的 暗色)
Vibrant light(有活力的 亮色)
Muted (柔和的)
Muted dark(柔和的 暗色)
Muted light(柔和的 亮色)
swatch對象對應的顏色方法
getPopulation(): 像素的數量
getRgb(): RGB顏色
getHsl(): HSL顏色
getBodyTextColor(): 用於內容文本的顏色
getTitleTextColor(): 標題文本的顏色