Android Jetpack:利用Palette进行图片取色_android 提取 图片 颜色

color1.setBackgroundColor(palette.getMutedColor(Color.WHITE))
color2.setBackgroundColor(palette.getDarkMutedColor(Color.WHITE))
color3.setBackgroundColor(palette.getLightMutedColor(Color.WHITE))
color4.setBackgroundColor(palette.getVibrantColor(Color.WHITE))
color5.setBackgroundColor(palette.getDarkVibrantColor(Color.WHITE))
color6.setBackgroundColor(palette.getLightVibrantColor(Color.WHITE))


运行后结果如下:


![111.jpg](https://img-blog.csdnimg.cn/img_convert/9f281ea624cfc2c93a7ac578b300f6a4.png)


这样各个颜色的差别就一目了然。除了上面的函数,还可以使用`getColorForTarget`这个函数,如下:



@ColorInt
public int getColorForTarget(@NonNull final Target target, @ColorInt final int defaultColor) {


这个函数需要一个Target,提供了6个静态字段,如下:



/**

  • A target which has the characteristics of a vibrant color which is light in luminance.
    */
    public static final Target LIGHT_VIBRANT;

/**

  • A target which has the characteristics of a vibrant color which is neither light or dark.
    */
    public static final Target VIBRANT;

/**

  • A target which has the characteristics of a vibrant color which is dark in luminance.
    */
    public static final Target DARK_VIBRANT;

/**

  • A target which has the characteristics of a muted color which is light in luminance.
    */
    public static final Target LIGHT_MUTED;

/**

  • A target which has the characteristics of a muted color which is neither light or dark.
    */
    public static final Target MUTED;

/**

  • A target which has the characteristics of a muted color which is dark in luminance.
    */
    public static final Target DARK_MUTED;

其实就是对应着上面除了主色调之外的六种颜色。


## 文字颜色自动适配


在上面的运行结果中可以看到,每个颜色上面的文字都很清楚的显示,而且它们并不是同一种颜色。其实这也是Palette提供的功能。


通过下面的函数,我们可以得到各种色调所对应的Swatch对象:


* getDominantSwatch
* getMutedSwatch
* getDarkMutedSwatch
* getLightMutedSwatch
* getVibrantSwatch
* getDarkVibrantSwatch
* getLightVibrantSwatch


注意:同上面一样,也可以通过`getSwatchForTarget(@NonNull final Target target)`来获取


Swatch类提供了以下函数:


* getPopulation(): 样本中的像素数量
* getRgb(): 颜色的RBG值
* getHsl(): 颜色的HSL值
* getBodyTextCo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值