Android开发Android知识我爱编程

Palette(调色板)

2016-05-14  本文已影响526人  Jinwong

利用Palette库来取得图片中的主要色彩

使用这个Android的开源库android-support-v7-palette。

       //目标bitmap
       Bitmap bm =BitmapFactory.decodeResource(getResources(),R.drawable.kale);
       //方法1
       Palette.Builder builder = Palette.from(bm);
       Palette palette=builder.generate();
 
       //方法2   使用异步
       builder.generate(bitmap, new Palette.PaletteAsyncListener() {  
           @Override  
           public void onGenerated(Palette palette) {     
            // Here's your generated palette
       }    

Palette palette=Palette.generate() 等方法直接废弃掉了

getPopulation(): the amount of pixels which this swatch represents.
getRgb(): the RGB value of this color.
getHsl(): the HSL value of this color.
getBodyTextColor(): the RGB value of a text color which can be displayed on top of this color.
getTitleTextColor(): the RGB value of a text color which can be displayed on top of this color.

比如如果你的TextView 有个背景图片,要想让字体颜色能够和背景图片匹配,则使用getBodyTextColor()比较合适,getTitleTextColor()其实应该和getBodyTextColor()差不多

Bitmap bm = BitmapFactory.decodeResource(getResources(),
                R.drawable.kale);
        Palette palette = Palette.generate(bm);
        if (palette.getLightVibrantSwatch() != null) {//需要注意的是`getVibrantSwatch()可能会返回一个null值,所以检查一下是必须的。
            //得到不同的样本,设置给imageview进行显示
            iv.setBackgroundColor(palette.getLightVibrantSwatch().getRgb());
            iv1.setBackgroundColor(palette.getDarkVibrantSwatch().getRgb());
            iv2.setBackgroundColor(palette.getLightMutedSwatch().getRgb());
            iv3.setBackgroundColor(palette.getDarkMutedSwatch().getRgb());

        }
上一篇 下一篇

猜你喜欢

热点阅读