今天我們講的這個(gè)Palette非常好用,也非常好玩癌蓖。 Palette的作用是從圖像中提取突出的顏色瞬哼,這樣我們可以根據(jù)提取到的色值把它賦給Toolbar,標(biāo)題租副,狀態(tài)欄等坐慰,可以使我們的整個(gè)界面色調(diào)統(tǒng)一,效果非常好看附井。
Palette介紹
Palette顧名思義調(diào)色板, Palette的作用是可以從圖像中提取圖片的顏色两残。我們可以把提取的顏色融入到App UI中永毅,可以使UI風(fēng)格更加美觀融洽。
- Palette可以提取的顏色如下:
- Vibrant (有活力的)
- Vibrant dark(有活力的 暗色)
- Vibrant light(有活力的 亮色)
- Muted (柔和的)
- Muted dark(柔和的 暗色)
- Muted light(柔和的 亮色)
通過Palette對(duì)象獲取到六個(gè)樣本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對(duì)象對(duì)應(yīng)的顏色方法
- getPopulation(): 像素的數(shù)量
- getRgb(): RGB顏色
- getHsl(): HSL顏色
- getBodyTextColor(): 用于內(nèi)容文本的顏色
- getTitleTextColor(): 標(biāo)題文本的顏色
Palette實(shí)例
Palette的一些方法:
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
//暗人弓、柔和的顏色
int darkMutedColor = palette.getDarkMutedColor(Color.BLUE);//如果分析不出來沼死,則返回默認(rèn)顏色
//暗、柔和
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的混合值---主色調(diào)
int rgb = vibrantSwatch.getRgb();
//谷歌推薦:圖片中間的文字顏色
int bodyTextColor = vibrantSwatch.getBodyTextColor();
//谷歌推薦:作為標(biāo)題的顏色(有一定的和圖片的對(duì)比度的顏色值)
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);
//上面四個(gè)的原理也就是下面的方法
// 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經(jīng)常用于和ViewPager,F(xiàn)ragment搭配使用健芭,當(dāng)我們的Pager切換時(shí)伴隨著Fragment的變化县钥,而Fragment里的內(nèi)容一般是不同的,所以每個(gè)Fragment里的一般視覺效果也是不同的慈迈,所以我們可以用Palette來去提取Fragment中的主色調(diào)若贮,把這個(gè)主色調(diào)用于整體的UI風(fēng)格。
先看效果圖,如下:
第一步:添加依賴
compile 'com.android.support:palette-v7:23.4.0'
第二步:創(chuàng)建Palette對(duì)象谴麦,并獲取圖片的顏色值
// 用來提取顏色的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) {
//獲取到充滿活力的這種色調(diào)
Palette.Swatch vibrant = palette.getVibrantSwatch();
//根據(jù)調(diào)色板Palette獲取到圖片中的顏色設(shè)置到toolbar和tab中背景蠢沿,標(biāo)題等,使整個(gè)UI界面顏色統(tǒng)一
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()));
}
}
});
就是這么簡(jiǎn)單匾效,這里略過了對(duì)TabLayout的講解舷蟀,因?yàn)檫@次主講的是Palette嘛,沒記錯(cuò)的話面哼,以前講解過TabLayout的使用野宜,不會(huì)的同學(xué)可以去看源碼或者是查找歷史消息去看看文章。