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应用开发提供了一种 ...
随机推荐
- http --- 从输入URL到页面加载的过程发生了什么?
可以分为这几个大的过程: DNS解析 TCP连接 客户端发送HTTP请求 服务器处理请求并返回HTTP报文 浏览器解析渲染页面 结束 其中(1)DNS解析可以理解为主寻找这个IP地址的过程,其中如果找 ...
- Objects and values
If we execute these assignment statements: We know that a and b both refer to a string, but we don’t ...
- 6.boostTCP通信
客户端 #include <boost/asio.hpp> #include <iostream> #include <stdlib.h> using namesp ...
- ZoomIt(投影演示辅助软件)下载、安装与运行使用
下载ZoomIt后,打开即可使用:打开时,你讲看到如下的几个页面,这几个页面是为了介绍每个功能的使用,还可以去设定你觉得比较舒服的快捷键, 默认的是Ctrl+1屏幕放大.Ctrl+2屏幕标注,Ctrl ...
- windows下远程链接虚拟机Linux下MySQL数据库问题处理
参考解决:https://www.cnblogs.com/blogforly/p/5997553.html 今天远程连接虚拟机中的MySQL数据库,一直连不上,在网上搜索了一下,发现原因是MySQL对 ...
- Unix版权史
原文出处: 阮一峰 这几天,我在读<Unix编程艺术>. 书中介绍了Unix的发展历史.我发现,这是一个很好的例子,说明现行版权制度具有阻碍社会发展的负面作用. 2. Unix诞生于 ...
- Ubuntu18.04 解压zip文件乱码的解决方法
在Ubuntu的系统下解压zip文件的时候居然出现了乱码,通过查找网上的资料,解决的办法有两种 一.通过unzip行命令解压,指定字符集,由于zip格式中并没有指定编码格式,Windows下生成的zi ...
- Mac配置PHP环境
本文章来自:http://blog.csdn.net/wj_november/article/details/51417491 本人使用的是:MacOs 10.12.3,根据如上操作已经安装成功,感谢 ...
- bzoj1612 Usaco08 Jan 牛大赛
水题模拟 建一个图,每两个牛进行比赛就连一条边,然后两遍dfs求出比他弱和比他强的牛,最后如果相加数量等于n,说明他能与全部的牛进行比较,排名确定. #include<bits/stdc++.h ...
- python学习---【__all__】
python包中都会有一个__init__.py的模块,这个模块是区分该父文件是一个python包,或是一个文件目录.这个__init__.py可以是空,也可以添加内容,最常见的就是其中的__all_ ...