Android学习笔记之图像颜色处理(ColorMatrix)
对图像进行颜色方面的处理,通过使用颜色矩阵(ColorMatrix)来实现。从而可以达到很多特效如黑白老照片、泛黄旧照片等等。
1.颜色矩阵(ColorMatrix)
这里有详细的介绍:http://developer.Android.com/reference/android/graphics/ColorMatrix.html
不过是英文的,在这里我就先导读一下。
一张位图可以转换为一个5*4的矩阵,涉及到颜色和透明度。如图1所示。在Android中,颜色矩阵M是以一维数组m=[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t]的方式进行存储的。
图1
在一张图片中,图像的RGBA(红色、绿色、蓝色、透明度)值决定了该图片所呈现出来的颜色效果。
而图像的RGBA值则存储在一个5*1的颜色分量矩阵C中,由颜色分量矩阵C可以控制图像的颜色效果。颜色分量矩阵C如图2所示。
图2
要想改变一张图片的颜色效果,只需要改变图像的颜色分量矩阵即可。通过颜色矩阵可以很方便的修改图像的颜色分量矩阵。假设修改后的图像颜色分量矩阵为C1,则有如图3所示的颜色分量矩阵计算公式。
图3
由此可见,通过颜色矩阵修改了原图像的RGBA值,从而达到了改变图片颜色效果的目的。并且,通过如图3所示的运算可知,颜色矩阵M的第一行参数abcde决定了图像的红色成分,第二行参数fghij决定了图像的绿色成分,第三行参数klmno决定了图像的蓝色成分,第四行参数pqrst决定了图像的透明度,第五列参数ejot是颜色的偏移量。
通常,改变颜色分量时可以通过修改第5列的颜色偏移量来实现,如图4所示的颜色矩阵M1,通过计算后可以得知该颜色矩阵的作用是使图像的红色分量和绿色分量均增加100,这样的效果就是图片泛黄(因为红色与绿色混合后得到黄色)。
图4
除此之外,也可以通过直接对颜色值乘以某一系数而达到改变颜色分量的目的。如图5所示的颜色矩阵M2,将绿色分量放大了2倍,这样的效果就是图片泛绿色。
图5
实例:
步骤一:我们首先自定义一个view,用来显示我们处理的图片。
ColorView.Java
- package com.mycolor;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.ColorMatrix;
- import android.graphics.ColorMatrixColorFilter;
- import android.graphics.Paint;
- import android.util.AttributeSet;
- import android.widget.ImageView;
- public class ColorView extends ImageView {
- private Paint myPaint = null;
- private Bitmap bitmap = null;
- private ColorMatrix myColorMatrix = null;
- private float[] colorArray = {1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0};
- public ColorView(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.a2);
- invalidate();
- }
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- //新建画笔对象
- myPaint = new Paint();
- //描画(原始图片)
- canvas.drawBitmap(bitmap,0, 0, myPaint);
- //新建颜色矩阵对象
- myColorMatrix = new ColorMatrix();
- //设置颜色矩阵的值
- myColorMatrix.set(colorArray);
- //设置画笔颜色过滤器
- myPaint.setColorFilter(new ColorMatrixColorFilter(myColorMatrix));
- //描画(处理后的图片)
- canvas.drawBitmap(bitmap,0,0,myPaint);
- invalidate();
- }
- //设置颜色数值
- public void setColorArray(float[] colorArray){
- this.colorArray = colorArray;
- }
- //设置图片
- public void setBitmap(Bitmap bitmap){
- this.bitmap = bitmap;
- }
- }
步骤二:自定义我们的布局
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/colorView_layout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <com.mycolor.ColorView
- android:id="@+id/myColorView"
- android:layout_width="480dp"
- android:layout_height="180dp"/>
- <LinearLayout
- android:id="@+id/colorlayout1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/Edit1"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="1" />
- <EditText
- android:id="@+id/Edit2"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0"
- />
- <EditText
- android:id="@+id/Edit3"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit4"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit5"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/colorlayout2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/Edit6"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit7"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="1" />
- <EditText
- android:id="@+id/Edit8"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit9"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit10"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/colorlayout3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/Edit11"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit12"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit13"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="1" />
- <EditText
- android:id="@+id/Edit14"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit15"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/colorlayout4"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/Edit16"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit17"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit18"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- <EditText
- android:id="@+id/Edit19"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="1" />
- <EditText
- android:id="@+id/Edit20"
- android:layout_width="50dp"
- android:layout_height="40dp"
- android:layout_weight="1"
- android:text="0" />
- </LinearLayout>
- <Button
- android:id="@+id/Button"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginBottom="0dp"
- android:text="提交" />
- </LinearLayout>
步骤三:完成我们的Activity
- package com.mycolor;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class ColorActivity extends Activity implements OnClickListener{
- private Button button = null;
- private ColorView colorView = null;
- private EditText[] editTextArray = null;
- private float colorArray[] = null;
- private int[] EditTextID = {R.id.Edit1,R.id.Edit2,R.id.Edit3,R.id.Edit4,R.id.Edit5,
- R.id.Edit6,R.id.Edit7,R.id.Edit8,R.id.Edit9,R.id.Edit10,
- R.id.Edit11,R.id.Edit12,R.id.Edit13,R.id.Edit14,R.id.Edit15,
- R.id.Edit16,R.id.Edit17,R.id.Edit18,R.id.Edit19,R.id.Edit20};
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- button = (Button)findViewById(R.id.Button);
- button.setOnClickListener(this);
- editTextArray = new EditText[20];
- colorArray = new float[20];
- for(int i = 0;i < 20;i++){
- editTextArray[i] = (EditText)findViewById(EditTextID[i]);
- }
- colorView = (ColorView)findViewById(R.id.myColorView);
- }
- @Override
- public void onClick(View v) {
- for(int i = 0;i < 20;i++){
- colorArray[i] = Float.valueOf(editTextArray[i].getText().toString().trim());
- System.out.println("i = " + i + ":" + editTextArray[i].getText().toString().trim());
- }
- colorView.setColorArray(colorArray);
- }
- }
这样就可以了。
效果图:
改变值可以呈现不同的效果:
原文参考:点击打开链接
代码:点击下载
Android学习笔记之图像颜色处理(ColorMatrix)的更多相关文章
- Android学习笔记-ImageView(图像视图)
本节引言: 本节介绍的UI基础控件是:ImageView(图像视图),见名知意,就是用来显示图像的一个View或者说控件! 官方API:ImageView;本节讲解的内容如下: ImageView的s ...
- 【转】Pro Android学习笔记(三):了解Android资源(上)
在Android开发中,资源包括文件或者值,它们和执行应用捆绑,无需在源代码中写死,因此我们可以改变或替换他们,而无需对应用重新编译. 了解资源构成 参考阅读Android学习笔记(三八):资源res ...
- 【转】Pro Android学习笔记(二):开发环境:基础概念、连接真实设备、生命周期
在Android学习笔记(二):安装环境中已经有相应的内容.看看何为新.这是在source网站上的Android架构图,和标准图没有区别,只是这张图颜色好看多了,录之.本笔记主要讲述Android开发 ...
- Android学习笔记进阶17之LinearGradient
具体的看一下博文:Android学习笔记进阶15之Shader渲染 package xiaosi.BitmapShader; import android.app.Activity; import a ...
- Android学习笔记进阶16之BitmapShader
<1>简介 具体的看一下博文:Android学习笔记进阶15之Shader渲染 public BitmapShader(Bitmap bitmap,Shader.TileMode ti ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
随机推荐
- SVN冲突的解决过程
此文教程只是个人记录使用,不建议当教程!(估计新手也看得懵) 改完之后Ctrl+s保存就好.
- yii2.0缓存篇之片段缓存
片段缓存指的是缓存页面内容中的某个片段.默认缓存 60秒. return $this->renderPartial("ca"); ...
- HTML图片映射
<img>图片映射 <map>与<area>一起使用来定义一个图像映射(一个可点击的链接区域). <img src="cat.jpg" a ...
- Ubuntu命令行快捷启动Pycharm
我pycharm安装目录为/home/dell/pycharm-community-2018.1.2/bin.那么安装完成之后系统不会给pycharm添加系统路径,只有把终端切换到安装目录/home/ ...
- python web开发 框架 模板 MVC
我是跟着廖雪峰老师学习的,对于我这样的纯小白来说,跟着他的网站学习,简直是被妈妈抱在怀里一样无忧无虑,这样的学习本来没有记录下来的必要,但是由于我的粗心大意,经常会出现一些错误,所以我决定把这些错误记 ...
- 题解 P2330 【[SCOI2005]繁忙的都市】
又是一道Kruskal题目. AC代码见下. 主要思路就是将所有的边储存起来,然后进行贪心地选择,期间需要判断两个端点是否有关联,这一过程通过并查集实现.Kruskal部分套模板就可以了. #incl ...
- 各大IT企业招聘所须要求技能
1.中兴 ZTE 软件研发project师 工作地点:西安.深圳.上海.天津 主要职责: 1.从事通讯产品相关软件开发 2.进行软件具体设计,代码编写.单元測试.集成測试.系统測试等 3.进行软件代码 ...
- EventBus框架原理解析(结合源代码)(上)
上一篇文章http://blog.csdn.net/crazy__chen/article/details/47425779 和大家一起模仿EventBus的实现机制.和大家一起写出了一个简易的Eve ...
- c++动态库中使用命名空间的问题
这是C++才会有的语言特性. 假如你使用一个程序库,他里面有桓霰淞拷衋bc,可是你自己也不小心定义了一个叫abc的变量,这样就会引起重定义错误.所以为了避免这样的现象,C++引入了名字空间(names ...
- mysql简单优化思路
mysql简单优化思路 作为开发人员,数据库知识掌握的可能不是很深入,但是一些基本的技能还是要有时间学习一下的.作为一个数据库菜鸟,厚着脸皮来总结一下 mysql 的基本的不能再基本的优化方法. 为了 ...