Android开发之palette的使用(安卓调色板)

来源:互联网 发布:java array length 编辑:程序博客网 时间:2024/06/14 22:13

前言:随着UI的丰富性,每当我们切换的时候,都需要一个主色调,这个时候我们可以用Palette来去提取UI中的主色调,一般都是从Bitmap中提取颜色,然后把颜色设置给title,content等!

------------------------------分割线--------------------------

使用:首先通过imageview获取Drawable,然后在获取Bitmap。

 BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); Bitmap bitmap = drawable.getBitmap();
Palette是v7包下的拓展类首先添加依赖:

compile 'com.android.support:palette-v7:26.0.0-alpha1'
接着我们使用Palette分析bitmap,我们分析图片的时候,图片可能会比较大,或者颜色比较复杂,所以为了防止主线程阻塞,我们使用google给我们提供的异步来解决:

Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {            @Override            public void onGenerated(Palette palette) {            }        });
Palette提供了很多api来设置颜色,来我们看一下常用api的用法:

1.获取某种特性颜色的样品:

Palette.Swatch lightVibrantSwatch = palette.getLightVibrantSwatch();Palette.Swatch vibrantSwatch = palette.getVibrantSwatch();
2.获取google推荐的整体颜色的rgb值--主色调(这里我们使用的是vibrantSwatch)

int rgb = vibrantSwatch.getRgb();
3.获取google推荐的标题的颜色:
int titleTextColor = vibrantSwatch.getTitleTextColor();
4.颜色向量:
float[] hsl = vibrantSwatch.getHsl();
5.分析该颜色在图片中所占的像素多少值:

int population = vibrantSwatch.getPopulation();

6.在实际使用的时候我们也可以仅仅使用颜色的rgb来设置背景色,然后在添加一个透明度
protected int getTranslucentColor(float percent, int rgb) {         int blue = Color.blue(rgb);        int green = Color.green(rgb);        int red = Color.red(rgb);        int alpha = Color.alpha(rgb);        alpha = Math.round(alpha * percent);        return Color.argb(alpha, red, green, blue);    }
7.给标题设置背景色:
tvTtle.setBackgroundColor(getTranslucentColor(0.6f, rgb));
8.给标题字体设置颜色:

tvTtle.setTextColor(titleTextColor);

ok!来我们看一下效果图:


----------------------------分割线--------------------------------------

当然了,我们也可以自定义显示的颜色样式!

1.暗、柔和(Color.BLUE是默认颜色,下面同):

int darkMutedColor = palette.getDarkMutedColor(Color.BLUE);
2.亮、柔和:
int lightMutedColor = palette.getLightMutedColor(Color.BLUE);

3.暗、鲜艳:

int darkVibrantColor = palette.getDarkVibrantColor(Color.BLUE);

4.亮、鲜艳:

int lightVibrantColor = palette.getLightVibrantColor(Color.BLUE);

5.柔和:

int mutedColor = palette.getMutedColor(Color.BLUE);

6.鲜艳:

int vibrantColor = palette.getVibrantColor(Color.BLUE);

。。。

效果图就不和大家展示了,自己试着去实现一下!

----------------------------结束!!!------------------------------------------------





原创粉丝点击