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(): 标题文本的颜色
新闻热点
疑难解答