利用Palette庫來取得圖片中的主要色彩


  其實就我對開源庫的了解,有很多開源庫都能實現自動計算出任意一張圖片中的主要色彩的功能,這種看似神奇實則枯燥的技術很容易適用到手機的UI中。根據不同的背景定制不同的UI,這個在最新的Android Material Design里面就很有用了。本篇來講述如何使用這個Android的開源庫android-support-v7-palette。

流程:得到一個bitmap,通過方法進行分析,取出LightVibrantSwatch,DarkVibrantSwatch,LightMutedSwatch,DarkMutedSwatch這些樣本,然后得到rgb。

 

Palette這個類中提取以下突出的顏色:

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

 

代碼:

package com.example.palettedemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.graphics.Palette;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private ImageView iv, iv1, iv2, iv3, iv4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        /**
         * Palette從圖像中提取突出的顏色,這樣可以把色值賦給ActionBar、或者其他,可以讓界面整個色調統一,效果見上圖(Palette)
         * 。
         * 
         * Palette這個類中提取以下突出的顏色:
         * 
         * Vibrant (有活力) Vibrant dark(有活力 暗色) Vibrant light(有活力 亮色)
         *
         * Muted (柔和) Muted dark(柔和 暗色) Muted light(柔和 亮色)
         */
        iv = (ImageView) findViewById(R.id.iv);
        iv1 = (ImageView) findViewById(R.id.imageView1);
        iv2 = (ImageView) findViewById(R.id.imageView2);
        iv3 = (ImageView) findViewById(R.id.imageView3);

        //目標bitmap
        Bitmap bm = BitmapFactory.decodeResource(getResources(),
                R.drawable.kale);
        Palette palette = Palette.generate(bm);
        if (palette.getLightVibrantSwatch() != null) {
            //得到不同的樣本,設置給imageview進行顯示
            iv.setBackgroundColor(palette.getLightVibrantSwatch().getRgb());
            iv1.setBackgroundColor(palette.getDarkVibrantSwatch().getRgb());
            iv2.setBackgroundColor(palette.getLightMutedSwatch().getRgb());
            iv3.setBackgroundColor(palette.getDarkMutedSwatch().getRgb());

        }

    }

}

 

源碼下載:http://download.csdn.net/detail/shark0017/8107523

 

參考:http://blog.csdn.net/a396901990/article/details/40187769


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM