5.X提出了color palette 的概念,能够让主题动态的去适应当前的页面色调,让整个app色调看起来比较和谐统一

那么如何使用Palette呢,必不可少,我们需要在Android studio中引用相关的jar包,我使用的jar是

 compile 'com.android.support:palette-v7:21.0.0'

第一:主要是通过传递一个Bitmap对象给Palette,我们可以使用Paltte下的方法,静态的方法Paltte.generate()或者Paltte.generateAsync(),我们可以看下源码

//generate(Bitmap) 生成含有16种颜色种类的Palette;
public static Palette generate(Bitmap bitmap) {
return generate(bitmap, DEFAULT_CALCULATE_NUMBER_COLORS);
}
//generate(Bitmap, int) 生成含有指定数量颜色种类的Palette,数量越多需要的时间越久。
public static Palette generate(Bitmap bitmap, int numColors) {
checkBitmapParam(bitmap);
checkNumberColorsParam(numColors);
// First we'll scale down the bitmap so it's shortest dimension is 100px
final Bitmap scaledBitmap = scaleBitmapDown(bitmap);
// Now generate a quantizer from the Bitmap
ColorCutQuantizer quantizer = ColorCutQuantizer.fromBitmap(scaledBitmap, numColors);
// If created a new bitmap, recycle it
if (scaledBitmap != bitmap) {
scaledBitmap.recycle();
}
// Now return a ColorExtractor instance
return new Palette(quantizer.getQuantizedColors());
}
public static AsyncTask<Bitmap, Void, Palette> generateAsync(
Bitmap bitmap, PaletteAsyncListener listener) {
return generateAsync(bitmap, DEFAULT_CALCULATE_NUMBER_COLORS, listener);
}
public static AsyncTask<Bitmap, Void, Palette> generateAsync(
final Bitmap bitmap, final int numColors, final PaletteAsyncListener listener) {
checkBitmapParam(bitmap);
checkNumberColorsParam(numColors);
checkAsyncListenerParam(listener); return AsyncTaskCompat.executeParallel(
new AsyncTask<Bitmap, Void, Palette>() {
@Override
protected Palette doInBackground(Bitmap... params) {
return generate(params[], numColors);
} @Override
protected void onPostExecute(Palette colorExtractor) {
listener.onGenerated(colorExtractor);
}
}, bitmap);
}

2. 由于Android 对bitmap的操作可能是好事操作,我们可以选择使用Palette.generateAsync()的方法.他比Palette.generate()的区别就是需要传入PaletteAsyncListener

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView mIv=(ImageView)findViewById(R.id.iv ); Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.splash);
mIv.setImageBitmap(bitmap);
Palette.generateAsync(bitmap,new Palette.PaletteAsyncListener(){ @Override
public void onGenerated(Palette palette) {
//通过palette来获取对应的色调
Palette.Swatch vibrant=palette.getLightVibrantSwatch();
//int vibrant=palette.getMutedColor(990000);
//将颜色设置给相应的组件,比如actionbar,状态栏
getActionBar().setBackgroundDrawable(new ColorDrawable(vibrant.getRgb()));
//getActionBar().setBackgroundDrawable(new ColorDrawable(vibrant));
Window window =getWindow();
window.setStatusBarColor(vibrant.getRgb());
// window.setStatusBarColor(vibrant);
}
});
}

3.Palette默认情况会解析出来16种颜色,但是我们常用的无非就6种:

//获取鲜艳的颜色
public int getVibrantColor(int defaultColor) {
return mVibrantSwatch != null ? mVibrantSwatch.getRgb() : defaultColor;
}
//获取鲜艳颜色中的亮颜色
public int getLightVibrantColor(int defaultColor) {
return mLightVibrantSwatch != null ? mLightVibrantSwatch.getRgb() : defaultColor;
}
//获取鲜艳颜色中的暗色
public int getDarkVibrantColor(int defaultColor) {
return mDarkVibrantSwatch != null ? mDarkVibrantSwatch.getRgb() : defaultColor;
}

//获取柔和的颜色
public int getMutedColor(int defaultColor) {
return mMutedSwatch != null ? mMutedSwatch.getRgb() : defaultColor;
}
//获取柔和色中的亮颜色
public int getLightMutedColor(int defaultColor) {
return mLightMutedColor != null ? mLightMutedColor.getRgb() : defaultColor;
}
//获取柔和颜色中的暗色
public int getDarkMutedColor(int defaultColor) {
return mDarkMutedSwatch != null ? mDarkMutedSwatch.getRgb() : defaultColor;
}

我们看下效果图:

上图1部分代码表示:
Bitmap bitmap= BitmapFactory.decodeResource(getResources(), R.mipmap.banner_pingan_default);
mIv.setImageBitmap(bitmap);
Palette palette = Palette.generate(bitmap);
int vibrant = palette.getVibrantColor();//个人试了一下,好像这个颜色数字没多大用。。。。
int vibrantLight = palette.getLightVibrantColor();
int vibrantDark = palette.getDarkVibrantColor();
int muted = palette.getMutedColor();
int mutedLight = palette.getLightMutedColor();
int mutedDark = palette.getDarkMutedColor();
上图2部分代码表示:
Palette.generateAsync(bitmap,new Palette.PaletteAsyncListener(){
@Override
public void onGenerated(Palette palette) {
//通过palette来获取对应的色调
Palette.Swatch vibrant=palette.getLightVibrantSwatch();
//int vibrant=palette.getMutedColor(990000);
//将颜色设置给相应的组件
getActionBar().setBackgroundDrawable(new ColorDrawable(vibrant.getRgb()));
//getActionBar().setBackgroundDrawable(new ColorDrawable(vibrant));
Window window =getWindow();
window.setStatusBarColor(vibrant.getRgb());
// window.setStatusBarColor(vibrant);
}
});
}

4.Palette获取相应的颜色另一种方式 使用swatch:palette解析来的颜色都有对应swatch,它里面包含了很多颜色信息,常用的获取颜色有以下几几种

    public Swatch getVibrantSwatch() {
return mVibrantSwatch;
} /**
* Returns a light and vibrant swatch from the palette. Might be null.
*/
public Swatch getLightVibrantSwatch() {
return mLightVibrantSwatch;
} /**
* Returns a dark and vibrant swatch from the palette. Might be null.
*/
public Swatch getDarkVibrantSwatch() {
return mDarkVibrantSwatch;
} /**
* Returns a muted swatch from the palette. Might be null.
*/
public Swatch getMutedSwatch() {
return mMutedSwatch;
} /**
* Returns a muted and light swatch from the palette. Might be null.
*/
public Swatch getLightMutedSwatch() {
return mLightMutedColor;
} /**
* Returns a muted and dark swatch from the palette. Might be null.
*/
public Swatch getDarkMutedSwatch() {
return mDarkMutedSwatch;
}

你可以从Swatch里面获取你需要的颜色信息,比如RGB颜色值、HSL颜色向量、对应颜色在图像中所占的比例、与对应颜色搭配的标题字体颜色和正文字体颜色。

Palette palette  = Palette.generate(bitmap);
Palette.Swatch swatch = palette.getVibrantSwatch();
//hsl颜色向量
float[] hslValues = swatch.getHsl();
//rgb颜色值
int rgbColor = swatch.getRgb();
//该颜色在图像中所占的像素数
int pixelCount = swatch.getPopulation();
//对应的标题字体颜色
int titleTextColor = swatch.getTitleTextColor();
//对应的正文字体颜色
int bodyTextColor = swatch.getBodyTextColor();

在这里要注意的是,如果想要使用swatch获取的颜色,不能像上面直接使用get方法,他不需要传入默认的颜色值,如下例子:

通过palette来获取对应的色调
Palette.Swatch vibrant=palette.getLightVibrantSwatch();

 不知道大家有没有注意到源码中这一句话,他的意思说如果Palette没有解析到swatch的话,就会返回一个null。

 /**
* Returns a light and vibrant swatch from the palette. Might be null.
*/

5.在源码中还有这样一个方法:获取所有的颜色(16种),返回的是一个list集合


/**
* Returns all of the swatches which make up the palette.
*/
public List<Swatch> getSwatches() {
return Collections.unmodifiableList(mSwatches);
}

 效果图:

代码实现:

 List<Palette.Swatch> swatchs=Palette.generate(bitmap).getSwatches();
for (int i=;i<swatchs.size();i++){
View view = new View(MainActivity.this);
view.setBackgroundColor(swatchs.get(i).getRgb());
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,));
mLinearLayout.addView(view);
}
 

 

  

  


  

如何使用Palette提取Bitmap的颜色的更多相关文章

  1. C#读写BitMap及颜色相乘

    C#读写BitMap及颜色相乘 private Bitmap ReadBitMapAndMultipy(Bitmap bitmap0) { int x1width = bitmap0.Width; i ...

  2. LIRe 源代码分析 5:提取特征向量[以颜色布局为例]

    ===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...

  3. php 提取图片主要颜色

    PHP实现获取图片颜色值的方法 PHP获取图片颜色值检测图片主要颜色是通过imagecreatefromjpeg函数读取图片,再循环获得各个颜色值加以计算实现的. /** * 获取图片主要颜色 * @ ...

  4. 使用 Palette 让你的 UI 色彩与内容更贴合

    版权声明: 本账号发布文章均来自公众号,承香墨影(cxmyDev),版权归承香墨影所有. 每周会统一更新到这里,如果喜欢,可关注公众号获取最新文章. 未经允许,不得转载. 一.前言 今天介绍一个 An ...

  5. Palette 的使用

    Palette有什么用? Palette主要功能就是可以从图片中提取各种与颜色有关的元素.通过使用 Palette ,我们可以很轻松的实现界面风格的统一. Palette的使用很简单,首先你可以从gi ...

  6. [转]AppCompat 22.1,Goole暴走,MD全面兼容低版本

    AppCompat 22.1,Goole暴走,MD全面兼容低版本 分类: Android2015-04-24 09:48 1354人阅读 评论(0) 收藏 举报 android   目录(?)[+] ...

  7. Android开发学习之路-Palette颜色提取工具类使用

    视频(要FQ):https://www.youtube.com/watch?v=5u0dtzXL3PQ Palette是一个在support-v7包中的一个颜色提取工具类,用法比较简单,而且是谷歌官方 ...

  8. Android Material Design NavigationView 及 Palette 颜色提取器

    DrawerLayout + NavigationView DrawerLayout布局,通常在里面添加两个子控件,程序主界面添加到NavitagionView前面. <android.supp ...

  9. Palette状态栏颜色提取,写的不错就分享了

    Palette 说Palette之前先说下前面提到的Pager.ViewPager是什么大家应该都是知道的了,一般ViewPager.xxxTabStrip.Fragment三个好基友是一起出现的.这 ...

随机推荐

  1. SQLServer 中实现类似MySQL中的group_concat函数的功能

    SQLServer中没有MySQL中的group_concat函数,可以把分组的数据连接在一起. 后在网上查找,找到了可以实现此功能的方法,特此记录下. SELECT a, stuff((SELECT ...

  2. 高精度 java的一些题

    poj 1001 Exponentiation import java.util.*; import java.math.*; public class Main { public static vo ...

  3. gets和fgets函数的区别

    1. gets与fgets gets函数原型:char*gets(char*buffer);//读取字符到数组:gets(str);str为数组名. gets函数功能:从键盘上输入字符,直至接受到换行 ...

  4. windbg命令学习3

    3.进程与线程: 既可以显示进程和线程列表,又可以显示指定进程或线程的详细信息.调试命令可以提供比taskmgr更详尽的进程资料,在调试过程中不可或缺. 3.1. 进程命令 进程命令包括以下:显示进程 ...

  5. SQL Server一些重要视图 1

    第一个: sys.indexs 每个堆与索引在它上有一行. 第二个: sys.partitions每个堆与索引的每一个分区返回一行.每一张表最多可以有1000个区. 第三个: sys. allocat ...

  6. SendMessage基本认识

    SendMessage基本认识 函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.而函数PostMessage不同,将一个消息寄送到一个线 ...

  7. 面向对象程序设计-C++_课时11new & delete

    Dynamic memory allocation new new int; new Stash; new int[10]; new返回这个对象的指针 delete delete p; delete[ ...

  8. 网易云课堂_C语言程序设计进阶_第四周:ACL图形库

    创建ACLLib程序 #include"acllib.h" #include<stdio.h> int Setup1() { initWindow(, );//初始化窗 ...

  9. Swift初体验(三)

    /*******************************************************************************/ // 协议 protocol Des ...

  10. WINFORM中几句程序获取整个屏幕的图片及当前窗口的图片快照

    /// <summary> /// 获取整个屏幕的图片        /// </summary>        /// <returns></returns ...