使用调色板Palette在背景图中取色

Palette是Android.support.v7.graphics包中定义的用于提取背景中的颜色的类,该类用final修饰,不可被继承。


在使用Palette前,需要在build.gradle中加入依赖:
dependencies{
compile ‘com.android.support:palette-v7:23.1.1’
}


Palette可提取的颜色按类型可分为以下几种:

Vibrant ——动感的
Vibrant Dark ——动感的亮
Vibrant Light ——动感的暗
Muted ——柔和的
Muted Dark ——柔和的亮
Muted Light ——柔和的暗

用几张图放入项目
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.feng.buttonsheetdialogtest.Main3Activity">

    <Button
        android:id="@+id/bt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bg1"
        android:layout_marginBottom="53dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"/>

    <ImageView
        android:id="@+id/iv_show"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:srcCompat="@drawable/bg"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"/>
    <TextView
        android:id="@+id/block"
        android:layout_height="20dp"
        android:layout_width="match_parent"
        android:layout_below="@+id/iv_show"
        android:text="分割线"
        android:background="#666"
        />
    <View
        android:id="@+id/v1"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/block"
        />
    <View
        android:id="@+id/v2"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/v1"
        />
    <View
        android:id="@+id/v3"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/v2"
        />
    <View
        android:id="@+id/v4"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/v3"
        />
    <View
        android:id="@+id/v5"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/v4"
        />
    <View
        android:id="@+id/v6"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/v5"
        />

    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/bt1"
        android:layout_toEndOf="@+id/bt1"
        android:text="bg2"/>

    <Button
        android:id="@+id/bt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/bt2"
        android:layout_toEndOf="@+id/bt2"
        android:text="bg3"/>

    <Button
        android:id="@+id/bt4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/bt3"
        android:layout_toEndOf="@+id/bt3"
        android:text="bg4"/>
</RelativeLayout>
package com.feng.buttonsheetdialogtest;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.graphics.Palette;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

public class Main3Activity extends FragmentActivity {

    private Bitmap mBitmap;
    private View mV1;
    private View mV2;
    private View mV3;
    private View mV4,mV5,mV6;
    private ImageView mImageView;
    Button mButton,bt2,bt3,bt4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        initView();
    }

    private void initView() {
        mV1 = findViewById(R.id.v1);
        mV2 = findViewById(R.id.v2);
        mV3 = findViewById(R.id.v3);
        mV4 = findViewById(R.id.v4);
        mV5 = findViewById(R.id.v5);
        mV6 = findViewById(R.id.v6);
        mButton=(Button)findViewById(R.id.bt1);
        bt2=(Button)findViewById(R.id.bt2);
        bt3=(Button)findViewById(R.id.bt3);
        bt4=(Button)findViewById(R.id.bt4);
        mImageView = (ImageView) findViewById(R.id.iv_show);
        mImageView.setScaleType(ImageView.ScaleType.FIT_XY);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mImageView.setImageResource(R.drawable.bg);
                mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
                setBgColor();
                mButton.setEnabled(false);
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mImageView.setImageResource(R.drawable.bgg);
                mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bgg);
                setBgColor();
                bt2.setEnabled(false);
            }
        });
        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mImageView.setImageResource(R.drawable.bggg);
                mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bggg);
                setBgColor();
                bt3.setEnabled(false);
            }
        });
        bt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mImageView.setImageResource(R.drawable.bgggg);
                mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bgggg);
                setBgColor();
                bt4.setEnabled(false);
            }
        });
    }

    private void setBgColor() {
    /*如果在主线程中,我们可以使用异步的方式:*/
        Palette.generateAsync(mBitmap, new Palette.PaletteAsyncListener() {
            public void onGenerated(Palette palette) {
                //柔和的
                mV1.setBackgroundColor(palette.getMutedColor(Color.BLACK));//设置默认颜色
                //鲜艳的暗色
                mV2.setBackgroundColor(palette.getDarkMutedColor(Color.BLACK));
                //柔和的亮色
                mV3.setBackgroundColor(palette.getLightMutedColor(Color.BLACK));
                //鲜艳的
                mV4.setBackgroundColor(palette.getVibrantColor(Color.BLACK));
                //鲜艳的暗色
                mV5.setBackgroundColor(palette.getDarkVibrantColor(Color.BLACK));
                //鲜艳的亮色
                mV6.setBackgroundColor(palette.getLightVibrantColor(Color.BLACK));
                if(Build.VERSION.SDK_INT>=21){
                    Window window=getWindow();
                    window.setStatusBarColor(palette.getVibrantColor(Color.BLACK));
                    window.setNavigationBarColor(palette.getVibrantColor(Color.BLACK));
                }
            }

        });
    }

}

效果图
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述


这个顺序就是这个颜色的取值
感觉还挺好用的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值