使用硬件层来实现灰度视图
2015-11-02 17:21
405 查看
最近从设计团队那里得到一个需求,让一个特定的view在彩色和灰度之间切换。灰度,一个专用术语,意思是去掉一张图片中的所有饱和度。我快速Google 了一下得到了如下的代码:
public Bitmap toGrayscale(Bitmap original) {
int height = original.getHeight();
int width = original.getWidth();
Bitmap grayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(grayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0); // <-- important line here
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(original, 0, 0, paint);
return grayscale;
}
如果你要做的就是去掉一张图片的饱和度非常简单,但是假如我们是想去掉一个view (如果是个ViewGroup还包括所有子view)的饱和度呢?
其实一样很简单,可以通过在一个View layer上应用颜色过滤来完成。
如果我们查看LAYER_TYPE_HARDWARE(或者LAYER_TYPE_SOFTWARE)的文档,可以看到如下的解释:
"A hardware layer is useful to apply a specific color filter and/or blending mode and/or translucency to a view and all its children."
“一个hardware layer对于在view及其子view上应用颜色过滤,混合模式,半透明很有用 ”
Nice!我们可以用下面的代码来应用我们的ColorMatrixColorFilter:
这里是 Cheesesquare app变成灰度模式的gif效果:
![](http://jcodecraeer.com/uploads/20151031/1446229970769408.gif)
附加提示:hardware layer非常昂贵!最好只是暂时激活一个hardware layer。
英文原文:Greyscale Views on Android
public Bitmap toGrayscale(Bitmap original) {
int height = original.getHeight();
int width = original.getWidth();
Bitmap grayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(grayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0); // <-- important line here
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(original, 0, 0, paint);
return grayscale;
}
如果你要做的就是去掉一张图片的饱和度非常简单,但是假如我们是想去掉一个view (如果是个ViewGroup还包括所有子view)的饱和度呢?
其实一样很简单,可以通过在一个View layer上应用颜色过滤来完成。
如果我们查看LAYER_TYPE_HARDWARE(或者LAYER_TYPE_SOFTWARE)的文档,可以看到如下的解释:
"A hardware layer is useful to apply a specific color filter and/or blending mode and/or translucency to a view and all its children."
“一个hardware layer对于在view及其子view上应用颜色过滤,混合模式,半透明很有用 ”
Nice!我们可以用下面的代码来应用我们的ColorMatrixColorFilter:
public void setGreyscale(View v, boolean greyscale) { if (greyscale) { // Create a paint object with 0 saturation (black and white) ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); Paint greyscalePaint = new Paint(); greyscalePaint.setColorFilter(new ColorMatrixColorFilter(cm)); // Create a hardware layer with the greyscale paint v.setLayerType(LAYER_TYPE_HARDWARE, greyscalePaint); } else { // Remove the hardware layer v.setLayerType(LAYER_TYPE_NONE, null); } }
这里是 Cheesesquare app变成灰度模式的gif效果:
![](http://jcodecraeer.com/uploads/20151031/1446229970769408.gif)
附加提示:hardware layer非常昂贵!最好只是暂时激活一个hardware layer。
英文原文:Greyscale Views on Android
相关文章推荐
- Eclipse在引入Maven工程后,缺失Maven Dependencies
- about percpu
- mysql数据类型
- 学着分析android framework telephony
- Sublime Text的使用
- 绑定的细节
- BootStrap学习笔记-3
- 事件委托
- Warm up-HUD4612(树的直径+Tarjin缩点)
- [DB那些事]数据库加密
- eclipse设置备忘
- 第10周 项目1 - 二叉树算法库
- logstash elasticsearch output插件的bulk提交
- Java的字符串中对子字符串的查找方法总结
- 第十周实践项目3—利用二叉树遍历思想解决问题
- easyUI 关于自动添加一行的源码地址
- iOS面试题
- android中在指定的界面中插入引导页
- yii2 restful api搭建 实例
- Spring: @Transactional中的propagation属性