Android UI進階之旅7 Material Design之Palette

小楠總發表於2017-12-21

###Palette

Palette是V7包裡面的調色盤,這個庫可以根據傳入的圖片,然後獲取一些指定條件的顏色。例如可以在一張圖片裡面分析出一些色彩特性:主色調、鮮豔的顏色、柔和顏色等等。

###使用例子

下面是一個基本使用例如,其中Palette.generate已經過時不推薦使用。取而代之的是Palette.from方法,返回的是一個Builder物件,通過這個Builder物件我們可以指定圖片的分析範圍等等,最後在回撥中獲取相應的顏色即可。獲取到的顏色(可以設定透明度)可以設定給其他控制元件。

public class PaletteActivity extends AppCompatActivity {
    private ImageView iv;
    private TextView tv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_palette);
        iv = (ImageView) findViewById(R.id.iv);

        tv_title = (TextView) findViewById(R.id.tv_title);
        final TextView tv1 = (TextView) findViewById(R.id.tv1);
        final TextView tv2 = (TextView) findViewById(R.id.tv2);
        final TextView tv3 = (TextView) findViewById(R.id.tv3);
        final TextView tv4 = (TextView) findViewById(R.id.tv4);
        final TextView tv5 = (TextView) findViewById(R.id.tv5);
        final TextView tv6 = (TextView) findViewById(R.id.tv6);

        BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        //得到bitmap裡面的的一些色彩資訊---通過Palette類分析出來的
//		Palette palette = Palette.generate(bitmap);
        //非同步任務---可能分析的圖片會比較大或者顏色分佈比較複雜,會耗時比較久,防止卡死主執行緒。
        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {

            @Override
            public void onGenerated(Palette palette) {
                //暗、柔和的顏色
                int darkMutedColor = palette.getDarkMutedColor(Color.BLUE);//如果分析不出來,則返回預設顏色
                //暗、柔和
                int lightMutedColor = palette.getLightMutedColor(Color.BLUE);
                //暗、鮮豔
                int darkVibrantColor = palette.getDarkVibrantColor(Color.BLUE);
                //亮、鮮豔
                int lightVibrantColor = palette.getLightVibrantColor(Color.BLUE);
                //柔和
                int mutedColor = palette.getMutedColor(Color.BLUE);
                //柔和
                int vibrantColor = palette.getVibrantColor(Color.BLUE);


                //獲取某種特性顏色的樣品
//				Swatch lightVibrantSwatch = palette.getLightVibrantSwatch();
                Palette.Swatch lightVibrantSwatch = palette.getVibrantSwatch();
                //谷歌推薦的:圖片的整體的顏色rgb的混合值---主色調
                int rgb = lightVibrantSwatch.getRgb();
                //谷歌推薦:圖片中間的文字顏色
                int bodyTextColor = lightVibrantSwatch.getBodyTextColor();
                //谷歌推薦:作為標題的顏色(有一定的和圖片的對比度的顏色值)
                int titleTextColor = lightVibrantSwatch.getTitleTextColor();

                //顏色向量
                //float[] hsl = lightVibrantSwatch.getHsl();
                //分析該顏色在圖片中所佔的畫素多少值
                //int population = lightVibrantSwatch.getPopulation();

                tv_title.setBackgroundColor(getTranslucentColor(0.6f, rgb));
                tv_title.setTextColor(titleTextColor);

                tv1.setBackgroundColor(darkMutedColor);
                tv1.setText("darkMutedColor");
                tv2.setBackgroundColor(lightMutedColor);
                tv2.setText("lightMutedColor");
                tv3.setBackgroundColor(darkVibrantColor);
                tv3.setText("darkVibrantColor");
                tv4.setBackgroundColor(lightVibrantColor);
                tv4.setText("lightVibrantColor");
                tv5.setBackgroundColor(mutedColor);
                tv5.setText("mutedColor");
                tv6.setBackgroundColor(vibrantColor);
                tv6.setText("vibrantColor");

            }
        });


    }

    /**
     * 設定顏色透明度
     */
    protected int getTranslucentColor(float percent, int rgb) {
        int blue = Color.blue(rgb);
        int green = Color.green(rgb);
        int red = Color.red(rgb);
        int alpha = Color.alpha(rgb);
//		int blue = rgb & 0xff;
//		int green = rgb>>8 & 0xff;
//		int red = rgb>>16 & 0xff;
//		int alpha = rgb>>>24;

        alpha = Math.round(alpha * percent);
        Toast.makeText(this, "alpha:" + alpha + ",red:" + red + ",green:" + green, Toast.LENGTH_SHORT).show();
        return Color.argb(alpha, red, green, blue);
    }

}
複製程式碼

getTranslucentColor是設定當前argb的顏色的透明度,API的主要思路是通過位移以及與操作來完成。

如果覺得我的文字對你有所幫助的話,歡迎關注我的公眾號:

公眾號:Android開發進階

我的群歡迎大家進來探討各種技術與非技術的話題,有興趣的朋友們加我私人微信huannan88,我拉你進群交(♂)流(♀)

相關文章