Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.image);
// Palette的部分
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
@Override
public void onGenerated(Palette palette) {
Palette.Swatch vibrant = palette.getVibrantSwatch();
/* 界面颜色UI统一性处理,看起来更Material一些 */
mTabs.setTextColor(vibrant.getTitleTextColor());
// 其中状态栏、游标、底部导航栏的颜色需要加深一下,也可以不加,具体情况在代码之后说明
mTabs.setIndicatorColor(colorBurn(vibrant.getRgb()));
setStatusBarColor(vibrant.getRgb());
mToolbar.setBackgroundColor(vibrant.getRgb());
mTabs.setBackgroundColor(vibrant.getRgb());
setStatusBarColor(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.3));
green = (int) Math.floor(green * (1 - 0.3));
blue = (int) Math.floor(blue * (1 - 0.3));
return Color.rgb(red, green, blue);
}
@SuppressLint("NewApi")
private void setStatusBarColor(int color){
if (android.os.Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
// 很明显,这两货是新API才有的。
window.setStatusBarColor(colorBurn(color));
window.setNavigationBarColor(color);
}
}