什么是调试版?
从图像中提取突出的颜色,这样可以把颜色值赋值给其它控件
先导入夹包
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);