原文出处:http://qichaochen.github.io/2014/11/16/105-Android-5.0-Palette-01/

在Material Design设计中很重要的一部分内容是应用中图片颜色和文字颜色需要和主题相匹配,比如下面在这个应用:

文本的颜色根据不同图片动态进行对应适配(也许你会说,如果全部用白色文本多省事,何必这么麻烦呢?额…可以脑补一下高富帅和矮矬穷的区别)

那么在应用程序中如何提取图片的颜色信息呢?可以提取多少种颜色信息呢? 在最新的Support Library v21提供了Palette类可以很方便的实现这个需求。

下面看一下提取图片颜色信息的步骤和注意事项:

1、首先需要添加Palette的依赖

在build.gralde的dependencies添加appcomat v7和palette-v7依赖

dependencies {//...其他依赖compile 'com.android.support:appcompat-v7:21.0.0'compile 'com.android.support:palette-v7:21.+'
}

添加完成后需要同步一下Gradle,同步成功后就可以使用Palette类了。

2、创建Palette对象

官方提供了四种根据Bitmap对象来创建Palette对象的方法,具体分别如下:

两种同步方法:

// 最好在加载图片线程中使用
// 默认调色板大小(16).
Palette p = Palette.generate(bitmap);
//设置调色板大小(24)
Palette p = Palette.generate(bitmap, 24);

两种异步方法:

// 简单快速的实现方法,内部使用AsyncTask
// 但是可能不是最优的方法(因为有线程的切换)
// 默认调色板大小(16).
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {@Overridepublic void onGenerated(Palette palette) {// palette为生成的调色板}
});
// 设置调色板大小(24)
Palette.generateAsync(bitmap, 24, new Palette.PaletteAsyncListener() {@Overridepublic void onGenerated(Palette palette) {// palette为生成的调色板}
});

调色板的大小值越大,生成的调色板的时间越长,数值越小,得到的颜色信息也越少,调色板的大小最好根据图片类型来决定,比如:

  • 联系人头像:最优值24~32
  • 风景图:8-16
  • 当然默认是16,大多数情况下都可以取得很好的效果。

3、使用Palette对象

生成了Palette对象后就可以尝试获取六种不同的色板,具体如下:

Palette.Swatch s1 = Palette.getVibrantSwatch(); //充满活力的色板
Palette.Swatch s2 = Palette.getDarkVibrantSwatch(); //充满活力的暗色类型色板
Palette.Swatch s3 = Palette.getLightVibrantSwatch(); //充满活力的亮色类型色板
Palette.Swatch s4 = Palette.getMutedSwatch(); //黯淡的色板
Palette.Swatch s5 = Palette.getDarkMutedSwatch(); //黯淡的暗色类型色板
Palette.Swatch s6 = Palette.getLightMutedSwatch(); //黯淡的亮色类型色板

可以根据需求自由选择色板类型(充满活力和或充满活力暗色类型色板应该是大部分开发经常使用的),有了色板就可以根据色板来获取具体颜色信息,在Swatch色板中提供了五种颜色信息,分别如下:

方法 说明
getPopulation() the number of pixels represented by this swatch
getRgb() the RGB value of this color
getHsl() the HSL value of this color
getBodyTextColor() the RGB value of a text color which can be displayed on top of this color
getTitleTextColor() the RGB value of a text color which can be displayed on top of this color

 
示例代码如下:

Palette.Swatch swatch = palette.getVibrantSwatch();
TextView titleView = ...;
if (swatch != null) {titleView.setBackgroundColor(swatch.getRgb());titleView.setTextColor(swatch.getTitleTextColor()); //设置文本颜色

请注意必须对swatch进行是否为null判断,因为如果面板无法找到相匹配的标准色,那么然后将返回null,不进行判断将会出现空指针异常。

最后附上三个测试效果图和完整代码。

完整代码

public class MainActivity extends ActionBarActivity {private ImageView image, iv1, iv2, iv3, iv4, iv5, iv6;private Palette palette;private Palette.Swatch s1,s2,s3,s4,s5,s6;private int index = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv1 = (ImageView) findViewById(R.id.iv1);iv2 = (ImageView) findViewById(R.id.iv2);iv3 = (ImageView) findViewById(R.id.iv3);iv4 = (ImageView) findViewById(R.id.iv4);iv5 = (ImageView) findViewById(R.id.iv5);iv6 = (ImageView) findViewById(R.id.iv6);image = (ImageView) findViewById(R.id.image);GetPalette(R.drawable.a11);}private void GetPalette(int imageId) {image.setImageResource(imageId);//使用默认的调色板大小(16)Palette.generateAsync(BitmapFactory.decodeResource(getResources(), imageId), new Palette.PaletteAsyncListener() {@Overridepublic void onGenerated(Palette palette) {s1 = palette.getVibrantSwatch();s2 = palette.getDarkVibrantSwatch();s3 = palette.getLightVibrantSwatch();s4 = palette.getMutedSwatch();s5 = palette.getDarkMutedSwatch();s6 = palette.getLightMutedSwatch();if (s1 != null) {iv1.setBackgroundColor(s1.getRgb());s1.getPopulation();}if (s2 != null) {iv2.setBackgroundColor(s2.getRgb());}if (s3 != null) {iv3.setBackgroundColor(s3.getRgb());}if (s4 != null) {iv4.setBackgroundColor(s4.getRgb());}if (s5 != null) {iv5.setBackgroundColor(s5.getRgb());}if (s6 != null) {iv6.setBackgroundColor(s6.getRgb());}}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.menu_main,menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if (item.getItemId() == R.id.action_settings){index++;if (index%3 == 0){GetPalette(R.drawable.a11);} else if (index % 3 == 1){GetPalette(R.drawable.a12);} else if (index % 3 == 2){GetPalette(R.drawable.a13);}}return true;}
}

参考

Chris Banes:《Palette v21》
Chris Banes:《Palette preview》
Palette Reference

使用Palette类提取图片的颜色信息相关推荐

  1. 百度图片源码流出~按照颜色搜图片~提取图片主体颜色

    百度图片中有一个按照颜色搜图片的功能,其核心算法是提取图片主体颜色法,本文将使用python实现提取图片主体颜色算法. 百度将图片主体颜色归类到如下11中颜色: 颜色代码: label_colors ...

  2. 在Android下通过ExifInterface类操作图片的Exif信息

    什么是Exif 先来了解什么是Exif.Exif是一种图像文件格式,它的数据存储于JPEG格式是完全相同的,实际上Exif格式就是JPEG格式头插入了 数码照片的信息,包括拍摄的光圈.快门.平衡白.I ...

  3. 用python提取图片主要颜色_Python可视化|09-使用python和R提取图片颜色绘图(五-颜色使用完结篇)...

    本文是继前面四篇python可视化颜色使用的完结篇,介绍如何使用python提取图片中的颜色绘图: 如果你不想使用前人设定好的色号或者colormap,想自己从好看的图片中提取颜色,请往下看: 1.颜 ...

  4. 用python提取图片主要颜色_用Python提取图片主要颜色

    原文来自: 这段代码主要用来从图片提取其主要颜色,类似Goolge和Baidu的图片搜索时可以指定按照颜色搜索,所以我们先需要将每张图片的主要颜色提取出来,然后将颜色划分到与其最接近的颜色段上,然后就 ...

  5. 用Python提取图片主要颜色

    原文来自: http://www.sharejs.com/codes/python/8655 这段代码主要用来从图片提取其主要颜色,类似Goolge和Baidu的图片搜索时可以指定按照颜色搜索,所以我 ...

  6. MNIST数据集提取图片和标注信息

    MNIST数据集 简介 MNIST数据集(http://yann.lecun.com/exdb/mnist/)是著名的手写数字分类数据集,主要由一下四部分组成: 训练集图片:train-images. ...

  7. python获取图片的颜色信息

    python获取图片的颜色更多 0 getcolors 图片颜色 python image = Image.open("outofmemory.cn.png") image.get ...

  8. 【Pytorch神经网络实战案例】09 使用卷积提取图片的轮廓信息(手动模拟Sobel算子)

    1 载入图片并显示 import matplotlib.pyplot as plt import matplotlib.image as mpimg import torch import torch ...

  9. 获取图片的EXIF信息如此困难?

    对于数码相机所拍摄出的图片,Exif信息非常重要.Exif是英语Exchangeable Image File(可交换图像文件)的缩写,最初由日本电子工业发展协会(JEIDA --Japan Elec ...

最新文章

  1. 让Socket穿透Windows防火墙
  2. 如何书写高质量的jQuery代码
  3. python堆堆乐教程_python堆排序,详细过程图和讲解,这样做小白都会
  4. OpenCV用代码解释单应性的基本概念
  5. android常见面试问题
  6. 成功试验基于C#/.NET的Android开发
  7. rust 案例_RUST-X气相防锈产品落户中国,助力中国高端制造出口海外
  8. js获取元素的方法与属性
  9. javascript 终极循环方法for... of ..推荐
  10. 前端试题-CSS试题(1)
  11. 通信原理电子版_2021届通信工程专业保研经历分享+个人经验总结
  12. Html转Word解决转存图片时候的跨域问题、默认打开视图问题
  13. 软考中级软件设计师-计算机系统知识点速查
  14. 基于文本检测模型检测文本框对图像进行旋转校正
  15. 两台笔记本一台连接不上wifi
  16. git + 移动端 web 开发
  17. 大整数加减乘除的实现
  18. pt100热电阻c语言的程序,单片机+ADC0832热电阻PT100测温程序
  19. 为什么抖音一直显示服务器升级换不了头像,抖音换不了头像怎么回事
  20. Tokyo Cabinet及Tokyo Tyrant tcb tch比较分析

热门文章

  1. IT团队如何安全地加速云计算的采用
  2. osgi实战学习之路:6. Service-1
  3. Numpy基础学习与总结
  4. 领域驱动设计系列文章汇总
  5. PHP pear安装
  6. 酷炫Jquery收集
  7. WIN-8“内置管理员无法激活此应用”问题
  8. 输入对话框基于PyQt4的输入对话框
  9. 自动登录DISCUZ,发帖的代码(部分)
  10. C++编程语言之赋值运算符