高級UI-Palette

Google推出的Palette是用來調色的,正如其漢語意思一樣,可以用來顯示顏色,在顯示圖片的時候,會配合圖片的色調來顯示,這樣就顯得很融合,其實Palette可以分析出圖片中的很多特性,例如主色調、鮮豔度、柔和度等

Palette獲得的顏色

其主要的獲取顏色方法如下:
獲取主要顏色:getDominantColor()
獲取柔和顏色:getMutedColor()
獲取鮮豔顏色:getVibrantColor()
獲取亮、柔和顏色:getLightMutedColor()
獲取亮、鮮豔顏色:getLightVibrantColor()
獲取暗、柔和顏色:getDarkMutedColor()
獲取暗、鮮豔顏色:getDarkVibrantColor()

Palette實例

在一張圖片中顯示出獲得的以上顏色,並以Google推薦的顏色顯示在圖片上
在手機中找到一張以前做的拍黃瓜的圖片,還有煎雞蛋的圖片,這裏就是用這兩張圖片來演示,代碼沒有任何變化,只是改變了ImageView裏面的src源
在使用Palette要先導入依賴,25.4.0爲版本號

implementation 'com.android.support:palette-v7:25.4.0'

貼出佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp">
        <ImageView
            android:id="@+id/image_view"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_centerInParent="true"
            android:src="@drawable/cuke" />
        <TextView
            android:id="@+id/text_view"
            android:layout_marginTop="5dp"
            android:layout_width="300dp"
            android:layout_height="120dp"
            android:layout_centerHorizontal="true"
            android:layout_alignBottom="@id/image_view"
            android:gravity="center"
            android:textSize="24sp"/>
    </RelativeLayout>

    <TextView
        android:id="@+id/text_view_1"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="24sp"/>
    <TextView
        android:id="@+id/text_view_2"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="24sp"/>
    <TextView
        android:id="@+id/text_view_3"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="24sp"/>
    <TextView
        android:id="@+id/text_view_4"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="24sp"/>
    <TextView
        android:id="@+id/text_view_5"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="24sp"/>
    <TextView
        android:id="@+id/text_view_6"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="24sp" />
    <TextView
        android:id="@+id/text_view_7"
        android:layout_marginTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="24sp"/>

</LinearLayout>

然後在活動中使用

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private TextView textView4;
    private TextView textView5;
    private TextView textView6;
    private TextView textView7;
    private TextView textView;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.image_view);
        textView1 = (TextView) findViewById(R.id.text_view_1);
        textView2 = (TextView) findViewById(R.id.text_view_2);
        textView3 = (TextView) findViewById(R.id.text_view_3);
        textView4 = (TextView) findViewById(R.id.text_view_4);
        textView5 = (TextView) findViewById(R.id.text_view_5);
        textView6 = (TextView) findViewById(R.id.text_view_6);
        textView7 = (TextView) findViewById(R.id.text_view_7);
        textView = (TextView) findViewById(R.id.text_view);

        BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
        Bitmap bitmap = bitmapDrawable.getBitmap();
        //同步方法,已棄用,可能造成線程阻塞
        //Palette palette = Palette.generate(bitmap);
        //異步
        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(Palette palette) {
                int dominantColor = palette.getDominantColor(Color.GRAY);
                textView1.setBackgroundColor(dominantColor);
                textView1.setText("DominantColor");
                int mutedColor = palette.getMutedColor(Color.GRAY);
                textView2.setBackgroundColor(mutedColor);
                textView2.setText("MutedColor");
                int vibrantColor = palette.getVibrantColor(Color.GRAY);
                textView3.setBackgroundColor(vibrantColor);
                textView3.setText("VibrantColor");
                int lightMutedColor = palette.getLightMutedColor(Color.GRAY);
                textView4.setBackgroundColor(lightMutedColor);
                textView4.setText("LightMutedColor");
                int lightVibrantColor = palette.getLightVibrantColor(Color.GRAY);
                textView5.setBackgroundColor(lightVibrantColor);
                textView5.setText("LightVibrantColor");
                int darkMutedColor = palette.getDarkMutedColor(Color.GRAY);
                textView6.setBackgroundColor(darkMutedColor);
                textView6.setText("DarkMutedColor");
                int darkVibrantColor = palette.getDarkVibrantColor(Color.GRAY);
                textView7.setBackgroundColor(darkVibrantColor);
                textView7.setText("DarkVibrantColor");
                //推薦顏色獲取
                Palette.Swatch swatch = palette.getLightVibrantSwatch();
                //推薦的主色調
                int rgb = swatch.getRgb();
                //推薦的主體文字顏色
                int bodyTextColor = swatch.getBodyTextColor();
                //推薦的標題文字顏色
                int titleTextColor = swatch.getTitleTextColor();
                //顏色向量
                float[] hsl = swatch.getHsl();
                //得到該顏色在圖片中的值
                int population = swatch.getPopulation();

                textView.setBackgroundColor(getTranslucentColor(0.7F, rgb));
                textView.setTextColor(bodyTextColor);
                textView.setText("這是一道我做的菜");
            }
        });
    }

    private int getTranslucentColor(float persent, int rgb) {
        //轉化透明度
        int blue = rgb & 0xFF;
        int green = rgb >>> 8 & 0xFF;
        int red = rgb >>> 16 & 0xFF;
        int alpha = rgb >>> 24;
        alpha = Math.round(alpha * persent);
        return Color.argb(alpha, red, green, blue);
    }
}

運行結果如下
Palette-煎雞蛋圖例
Palette-拍黃瓜圖例

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章