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应用开发提供了一种 ...
随机推荐
- Linux下安装使用MySQL
网上找那些安装教程比较多的版本,版本只要不是太旧就行. 下载mysql 5.6.28 通用版64位二进制版,二进制版相当于windows的安装包,可以直接安装,如果是源码版,还需要编译后再进行安装. ...
- 大数问题(相加) A + B
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum o ...
- css columns 与overflow结合的问题
想实现上面这样分栏,并且溢出滚动的效果.可是自己下面的代码只能得到横向滚动条.觉得出现这个情况觉得还蛮有意思的,特地记录一下. <li v-for="(item,index) in s ...
- Ubuntu 环境下的mysql 远程访问,redis 远程访问和设置密码 ,mongo 安装 ,设置用户密码,开启远程访问
MySQL远程访问 1.编辑mysql配置文件,把其中bind-address = 127.0.0.1注释了 vi /etc/mysql/mysql.conf.d/mysqld.cnf 2.使用roo ...
- openGl超级宝典学习笔记 (2) 7个主要的几何图元
点(GL_POINTS): 点总是正方形的像素,默认情况下,点的大小不受透视除法影响. 即无论与视点的距离怎样,它的大小都不改变.为了获得圆点.必须在抗锯齿模式下绘制点. 能够用glPointSize ...
- Ubuntu14环境下minigui安装问题记录--object.lo错误
minigui3.0.12在Ubuntu14上面编译只是去?出现这个错误:object.h:275:9: error: incompatible types when assigning to typ ...
- HDU 4372 Count the Buildings
Count the Buildings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- android 客户端 Cookie处理
Cookie,有时也用其复数形式Cookies,指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密). Cookie最早是网景公司的前雇员Lou Montul ...
- jquery常规选择器再学习_1123
jquery选择器基本模拟css语法来获取元素: 1 常规选择器 id 常见的元素标签 class 2 进阶选择器 组合选择器 常规选择器多个组合在一起 通配符选择器 * ,通常用于局部环境下 后代选 ...
- Windows环境下VMware虚拟机的自启动与自动关机--命令行操作
.设置开机免密登录系统 1. 按下Windows + R 组合键,输入“netplwiz”,点击回车. 2. 去除需要密码登录的勾. 3. 如果需要密码,输入密码,点击确认. 二.编辑vmware ...