王学岗Palette调色板的使用

本文介绍了一种使用Android Palette库从图片中提取颜色的方法,并展示了如何将这些颜色应用于UI元素,如TextView的背景色和文字色。

什么是调试版?
从图像中提取突出的颜色,这样可以把颜色值赋值给其它控件
先导入夹包
implementation ‘com.android.support:palette-v7:26.0.0-alpha1’
注意:我们导入的夹包要与v7版本一致,不然会报错
implementation ‘com.android.support:appcompat-v7:26.1.0’
上代码;布局很简单,就不给出了,一个ImageView和留个TextView

package com.example.acer.test_18_04_02;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<String> data;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView iv_imageView = (ImageView) findViewById(R.id.iv_imageView);
        TextView tv_one = (TextView) findViewById(R.id.tv_text1);
        TextView tv_two = (TextView) findViewById(R.id.tv_text2);
        TextView tv_three = (TextView) findViewById(R.id.tv_text3);
        TextView tv_four = (TextView) findViewById(R.id.tv_text4);
        TextView tv_five = (TextView) findViewById(R.id.tv_text5);
        TextView tv_six = (TextView) findViewById(R.id.tv_text6);

        //获取Bitmap
        BitmapDrawable bitmapdrawable = (BitmapDrawable) iv_imageView.getDrawable();
        Bitmap bitmap = bitmapdrawable.getBitmap();
        //创建Palette实例
        Palette palette = Palette.generate(bitmap);
        //取出暗的鲜艳的颜色的值,如果没有,就返回默认的红色值
        int darVibrantkColor = palette.getDarkVibrantColor(Color.RED);
        //取出暗的柔和的颜色
        int darkMyted = palette.getDarkMutedColor(Color.RED);
        //亮、鲜艳
        int lightvibran = palette.getLightVibrantColor(Color.RED);
        //亮、柔和
        int lightmuted = palette.getLightMutedColor(Color.RED);
        //柔和颜色
        int muted = palette.getMutedColor(Color.RED);
        //鲜艳颜色
        int vibrant = palette.getVibrantColor(Color.RED);

        tv_one.setTextColor(darVibrantkColor);
        tv_two.setTextColor(darkMyted);
        tv_three.setTextColor(lightvibran);
        tv_four.setTextColor(lightmuted);
        tv_five.setTextColor(muted);
        tv_six.setTextColor(vibrant);


    }
}

看效果,虽然不明显,但效果确实有了
这里写图片描述
下面我们实现这种效果,从图片中获取颜色,使用这种颜色做背景,对图片进行介绍。

  //根据调色版来获取图片颜色的采样本
        Palette.Swatch psdm = palette.getDarkMutedSwatch();//获取暗的,柔和的颜色
        //从采集的样本中获取图片的RGB
        psdm.getHsl();//获取颜色向量
        int rgb = psdm.getRgb();
        //从样本中获取文本信息文本颜色的值
        int bodytextColor = psdm.getBodyTextColor();
        int titleTextColor = psdm.getTitleTextColor();
        tv_one.setBackgroundColor(rgb);
        tv_one.setTextColor(bodytextColor);
        tv_two.setTextColor(titleTextColor);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值