Post

Android's Palette

Palette是一个v7包中提供的工具,可以用来在你的一个Bitmap中提取出一些代表色值,就想下面的这一幅图
palette01 现在支持这几种代表色:

  • Vibrant (鲜艳的)
  • Vibrant Dark (鲜艳的 暗色)
  • Vibrant Light (鲜艳的 亮色)
  • Muted (暗淡的)
  • Muted Dark(暗淡的 暗色)
  • Muted Light(暗淡的 亮色)
1
2
3
4
public static Palette generate (Bitmap bitmap);
public static Palette generate (Bitmap bitmap, int numColors);
public static AsyncTask<Bitmap, Void, Palette> generateAsync (Bitmap bitmap, Palette.PaletteAsyncListener listener)
public static AsyncTask<Bitmap, Void, Palette> generateAsync (Bitmap bitmap, int numColors, Palette.PaletteAsyncListener listener);

generate的生成速度很快,大约几十毫秒的样子,取色的时候可以加上默认值.对于numColors,风景画类的12-16就可以了,要是人脸就得24-32,肯定是越少越快,越多越精细

1
Generate a Palette from a Bitmap using the specified numColors. Good values for numColors depend on the source image type. For landscapes, a good values are in the range 12-16. For images which are largely made up of people's faces then this value should be increased to 24-32.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.strictdroid);
Palette.generateAsync(bitmap, new PaletteAsyncListener() {

    @Override
    public void onGenerated(Palette palette) {
        getSupportActionBar().setBackgroundDrawable(
                new ColorDrawable(palette.getVibrantSwatch().getRgb()));
        for (int i = 0; i < viewList.size(); i++) {
            View view = viewList.get(i);
            switch (i) {
                case 0:
                    view.setBackgroundColor(palette.getDarkMutedColor(Color.BLACK));
                    break;
                case 1:
                    view.setBackgroundColor(palette.getDarkVibrantColor(Color.BLACK));
                    break;
                case 2:
                    view.setBackgroundColor(palette.getLightMutedColor(Color.BLACK));
                    break;
                case 3:
                    view.setBackgroundColor(palette.getLightVibrantColor(Color.BLACK));
                    break;
                case 4:
                    view.setBackgroundColor(palette.getMutedColor(Color.BLACK));
                    break;
                case 5:
                    view.setBackgroundColor(palette.getVibrantColor(Color.BLACK));
                    break;
            }

        }
    }
});

源码地址
PaletteDemo 参考
v7-palette

This post is licensed under CC BY 4.0 by the author.