简介
调色板,提图色。
场景
从图提色后赋Toolbar、标题等使界面色调统一。
样本
// 充满活力
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()
// RGB色
getRgb()
// HSL色
getHsl()
// 内容文本色
getBodyTextColor()
// 标题文本色
getTitleTextColor()
使用
依赖
implementation 'com.android.support:palette-v7:23.4.0'
代码
// 用来提色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背景
if (vibrant != null) {
toolbar_tab.setBackgroundColor(vibrant.getRgb());
if (android.os.Build.VERSION.SDK_INT >= 21) {
Window window = getWindow();
window.setStatusBarColor(vibrant.getRgb());
}
}
}
});