Android Palette(調(diào)色板)的使用

一、Palette的簡(jiǎn)單介紹

android-support-v7-palette 里面的Palette是Android L SDK 中的新特性,可以使用 Palette 從圖像中提取出突出的顏色(主色調(diào)),獲取到顏色之后我們?cè)賹⑦@個(gè)顏色值賦給 ActionBar、狀態(tài)欄等。從而達(dá)到界面色調(diào)的統(tǒng)一,使界面美觀協(xié)調(diào)。

原理
通過(guò)得到一個(gè)bitmap,通過(guò)方法進(jìn)行分析,取出LightVibrantSwatch,DarkVibrantSwatch,LightMutedSwatch,DarkMutedSwatch這些樣本,然后得到rgb。

Palette這個(gè)類中提取以下突出的顏色

Vibrant (有活力)
Vibrant dark(有活力 暗色)
Vibrant light(有活力 亮色)
Muted (柔和)
Muted dark(柔和 暗色)
Muted light(柔和 亮色)

創(chuàng)建方法
      //目標(biāo)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
      }
二、內(nèi)部嵌套類和常用方法
Palette.Builder

生成器類,生成 Palette 實(shí)例。

Palette.Filter

過(guò)濾器接口,使 Palette 有更加細(xì)膩的顏色過(guò)濾

Palette.PaletteAsyncListener

異步加載監(jiān)聽(tīng)

pattle.Swatch

提供獲取結(jié)果的色彩樣本

from(List<Palette.Switch> switches)

通過(guò)預(yù)設(shè)的 Palette.Swatch 顏色樣本列表 來(lái)生成 Palette
返回值:static Palette

from(Bitmap bitmap)

通過(guò)返回 Palette.Builder 實(shí)例來(lái)構(gòu)建 Palette
返回值:static Palette.Builder

generate(Bitmap bitmap,int numColors) 、generate(Bitmap bitmap) 、

該方法已被遺棄,建議用 Palette.Builder 來(lái)生成Palette
返回值:static Palette

getColorForTarget(Target target,int defaultColor)

返回一個(gè)從目標(biāo)獲取的的 rgb 色值
返回值:int

getDarkMutedColor(int defaultColor)

返回一個(gè)柔和的暗色調(diào) rgb 值
返回值:int

getDarkMutedSwatch()

返回一個(gè)柔和的暗色調(diào)樣本類
返回值:Palette.Swatch

getDarkVibrantColor(int defaultColor)

返回一個(gè)鮮明的暗色調(diào) rgb 值
返回值:int

getDomainSwatch()

返回一個(gè)主色調(diào)的樣本類
返回值:Palette.Swatch

getLightMutedColor(int defaultColor)

返回一個(gè)柔和的亮色調(diào)顏色 rgb
返回值:Palette.Swatch

getLightVibrantSwatch()

返回一個(gè)鮮明的亮色調(diào)樣本類
返回值:Palette.Swatch

三、使用樣本(swatch)

創(chuàng)建完一個(gè)實(shí)例之后,我們還需要得到一種采集的樣本(swatch),有6中樣本(swatch):

Palette.getVibrantSwatch()
Palette.getDarkVibrantSwatch()
Palette.getLightVibrantSwatch()
Palette.getMutedSwatch()
Palette.getDarkMutedSwatch()
Palette.getLightMutedSwatch()
List<Palette.Swatch> swatches = palette.getSwatches();//一次性獲得所有的swatch;

使用方法
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 有個(gè)背景圖片,要想讓字體顏色能夠和背景圖片匹配,則使用getBodyTextColor()比較合適,getTitleTextColor()其實(shí)應(yīng)該和getBodyTextColor()差不多

四、Size問(wèn)題

在上面的代碼中,你可能注意到了可以設(shè)置palette的size。size越大,花費(fèi)的時(shí)間越長(zhǎng),而越小,可以選擇的色彩也越小。最佳的選擇是根據(jù)image的用途:

頭像之類的,size最好在24-32之間;
風(fēng)景大圖之類的 size差不多在8-16;
默認(rèn)是16.

Bitmap bm = BitmapFactory.decodeResource(getResources(),
              R.drawable.kale);
      Palette palette = Palette.generate(bm);
      if (palette.getLightVibrantSwatch() != null) {
          //需要注意的是`getVibrantSwatch()可能會(huì)返回一個(gè)null值,
          //所以檢查一下是必須的。
          //得到不同的樣本,設(shè)置給imageview進(jìn)行顯示
          iv.setBackgroundColor(palette.getLightVibrantSwatch().getRgb());
          iv1.setBackgroundColor(palette.getDarkVibrantSwatch().getRgb());
          iv2.setBackgroundColor(palette.getLightMutedSwatch().getRgb());
          iv3.setBackgroundColor(palette.getDarkMutedSwatch().getRgb());

      }
注意

加載不能在主線程中進(jìn)行,加載方式有同步加載和異步加載兩種:

1 同步

由于他們很可能會(huì)比較耗時(shí)(在分析大圖片或者所需顏色較多時(shí)),所以它們不應(yīng)該在主線程中執(zhí)行。你應(yīng)該先在別的線程中使用這兩個(gè)函數(shù)進(jìn)行解析,解析成功之后再使用。

//在加載圖片的后臺(tái)線程中同步加載
Palette palette = Palette.from(bitmap).genrate();
.
.
.
2 異步

有時(shí)候你不會(huì)在加載圖片的線程(非主線程)中使用解析出的顏色,所以Palette提供了異步方法,他們與之前的函數(shù)的區(qū)別就是需要傳入PaletteAsyncListener,提供在圖片解析完成后的回調(diào)函數(shù)。

//異步加載
Palette.from(bitmap).genrate(new PaletteAsyncListener(){
  public void onGenerated(Palette p){
      .
      .
      .
  }
});
Palette經(jīng)常用于和ViewPager,F(xiàn)ragment搭配使用,當(dāng)我們的Pager切換時(shí)伴隨著Fragment的變化,而Fragment里的內(nèi)容一般是不同的,所以每個(gè)Fragment里的一般視覺(jué)效果也是不同的,所以我們可以用Palette來(lái)去提取Fragment中的主色調(diào),把這個(gè)主色調(diào)用于整體的UI風(fēng)格。

Demo下載地址:http://download.csdn.net/detail/breeze_wf/9273313

2625875-1a5ffa9daaeb0192.gif

其他Demo
private void setPaletteColor(TextView tv, int color) {
    tv.setBackgroundColor(color);
    tv.setText(ColorUtils.toRGBHexString(color));
    tv.setTextColor(ColorUtils.parseBackgroundColor(color));
}

private void initPalette() {
    Drawable drawable = mPictureIv.getDrawable();
    Bitmap bitmap = drawableToBitmap(drawable);

    // Synchronous
    // mPalette = Palette.from(bitmap).generate();

    // Asynchronous
    Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
        @Override
        public void onGenerated(Palette palette) {
            // Use generated instance
            int defaultColor = Color.parseColor("#b64242");

            int mVibrantColor = palette.getVibrantColor(defaultColor);
            int mDarkVibrantColor = palette.getDarkVibrantColor(defaultColor);
            int mLightVibrantColor = palette.getLightVibrantColor(defaultColor);
            int mMutedColor = palette.getMutedColor(defaultColor);
            int mDarkMutedColor = palette.getDarkMutedColor(defaultColor);
            int mLightMutedColor = palette.getLightMutedColor(defaultColor);

            setPaletteColor(mVibrantColorTv, mVibrantColor);
            setPaletteColor(mDarkVibrantColorTv, mDarkVibrantColor);
            setPaletteColor(mLightVibrantColorTv, mLightVibrantColor);
            setPaletteColor(mMutedColorTv, mMutedColor);
            setPaletteColor(mDarkMutedColorTv, mDarkMutedColor);
            setPaletteColor(mLightMutedColorTv, mLightMutedColor);

            // dominant color (主色)
            int mDominantColor = palette.getDominantColor(defaultColor);
            setPaletteColor(mDominantColorTv, mDominantColor);

            // Swatch - 色塊 // 15種
            List<Palette.Swatch> mSwatchList = palette.getSwatches();
            Toast.makeText(MainActivity.this, "Swatch num: " + mSwatchList.size(), Toast.LENGTH_SHORT).show();
            int index = -1;
            LinearLayout mSwatchesContainer = null;
            LinearLayout.LayoutParams params;
            for (Palette.Swatch swatch : mSwatchList) {
                int color = swatch.getRgb();
                index++;

                if (index % 3 == 0) {
                    mSwatchesContainer = new LinearLayout(getApplicationContext());
                    mSwatchesContainer.setOrientation(LinearLayout.HORIZONTAL);
                    params = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT
                    );
                    params.topMargin = (int) DisplayUtils.dp2px(getApplicationContext(), 10);
                    mContainerLayout.addView(mSwatchesContainer, params);       //
                }

                LinearLayout mSwatchContainer = new LinearLayout(getApplicationContext());
                mSwatchContainer.setOrientation(LinearLayout.VERTICAL);
                params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT);
                params.weight = 1;
                params.gravity = Gravity.CENTER;
                if (mSwatchesContainer != null) {
                    mSwatchesContainer.addView(mSwatchContainer, params);       //
                }

                TextView mColorTv = new TextView(getApplicationContext());
                mColorTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                setPaletteColor(mColorTv, color);           //
                mColorTv.setGravity(Gravity.CENTER);
                params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        (int) DisplayUtils.dp2px(getApplicationContext(), 80)
                );
                params.gravity = Gravity.CENTER;
                mSwatchContainer.addView(mColorTv, params);                 //

                TextView mColorNameTv = new TextView(getApplicationContext());
                mColorNameTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                mColorNameTv.setText("Swatch " + index);
                mColorNameTv.setGravity(Gravity.CENTER);
                mColorNameTv.setTextColor(Color.parseColor("#333333"));
                params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT
                );
                params.gravity = Gravity.CENTER;
                mSwatchContainer.addView(mColorNameTv, params);
            }
        }
    });
}
Drawable轉(zhuǎn)Bitmap的方法:
public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = Bitmap.createBitmap(
            drawable.getIntrinsicWidth(),
            drawable.getIntrinsicHeight(),
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap); // canvas -> bitmap
    //canvas.setBitmap(bitmap);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);      // drawable -> canvas
    return bitmap;  // drawable -> canvas -> bitmap
}
ColorUtils
public class ColorUtils {

    public static int parseBackgroundColor2(int color) {
        int counter = 0;
        counter += Color.red(color) >= 128 ? 1 : 0;
        counter += Color.green(color) >= 128 ? 1 : 0;
        counter += Color.blue(color) >= 128 ? 1 : 0;
        return counter >= 2 ? Color.BLACK : Color.WHITE;
    }

    // 通過(guò)分析背景色來(lái)決定當(dāng)前文字的匹配顏色,使文字顏色自適應(yīng)背景顏色
    public static int parseBackgroundColor(int color) {
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);
        if (red >= 128 && green >= 128      // 三選二
                || red >= 128 && blue >= 128
                || green >= 128 && blue >= 128) {
            return Color.rgb(0, 0, 0);
        }
        return Color.rgb(255, 255, 255);
    }

    // #FF55FF => color
    // int color = Color.parseColor("#b64242");

    // color -> #FF55FF
    public static String toRGBHexString(final int color) {
        return toRGBHexString(Color.red(color), Color.green(color), Color.blue(color));
    }

    // (r,g,b) -> #FF55FF
    public static String toRGBHexString(int red, int green, int blue) {
        return toARGBHexString(-1, red, green, blue);
    }

    // default prefix: "#"
    // (a,r,g,b) -> #FF55FF55
    public static String toARGBHexString(int alpha, int red, int green, int blue) {
        return toARGBHexString("#", alpha, red, green, blue);
    }

    public static String toARGBHexString(String prefix, int alpha, int red, int green, int blue) {
        StringBuilder sb = new StringBuilder();
        sb.append(prefix);
        if (alpha != -1) {
            String mAlphaStr = Integer.toHexString(alpha);
            sb.append(mAlphaStr.length() == 1 ? "0" + mAlphaStr : mAlphaStr);
        }
        String mRedStr = Integer.toHexString(red);
        sb.append(mRedStr.length() == 1 ? "0" + mRedStr : mRedStr);
        String mGreenStr = Integer.toHexString(green);
        sb.append(mGreenStr.length() == 1 ? "0" + mGreenStr : mGreenStr);
        String mBlueStr = Integer.toHexString(blue);
        sb.append(mBlueStr.length() == 1 ? "0" + mBlueStr : mBlueStr);
        return sb.toString().toUpperCase();
    }

}

Palette
Demo地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容