下面将分别介绍这两个矩阵的用法和相关的函数。

android中可以通过颜色矩阵（ColorMatrix类）方面的操作颜色，颜色矩阵是一个5x4 的矩阵（如图1.1）

[ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ]

C矩阵是图片中包含的ARGB信息，R矩阵是用颜色矩阵应用于C之后的新的颜色分量，运算结果如下：

R' = a*R + b*G + c*B + d*A + e;
G' = f*R + g*G + h*B + i*A + j;
B' = k*R + l*G + m*B + n*A + o;
A' = p*R + q*G + r*B + s*A + t;

1 CMatrix类：
2   public class CMatrix extends Activity {
3
4 private Button change;
5 private EditText [] et = new EditText[ 20 ];
6 private float []carray = new float [ 20 ];
7 private MyImage sv;
8 @Override
9 public void onCreate(Bundle savedInstanceState) {
10 super .onCreate(savedInstanceState);
11 setContentView(R.layout.main);
12
13 change = (Button)findViewById(R.id.set);
14 sv = (MyImage)findViewById(R.id.MyImage);
15
16 for ( int i = 0 ;i < 20 ;i ++ ){
17
18 et[i] = (EditText)findViewById(R.id.indexa + i);
19 carray[i] = Float.valueOf(et[i].getText().toString());
20 }
21
22 change.setOnClickListener(l);
23 }
24
25 private Button.OnClickListener l = new Button.OnClickListener(){
26
27 @Override
28 public void onClick(View arg0) {
29 // TODO Auto-generated method stub
30   getValues();
31 sv.setValues(carray);
32 sv.invalidate();
33 }
34
35 };
36 public void getValues(){
37 for ( int i = 0 ;i < 20 ;i ++ ){
38
39 carray[i] = Float.valueOf(et[i].getText().toString());
40 }
41
42 }
43
44
45 }
46 MyImage类继承自View类：
47 public class MyImage extends View {
48 private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
49 private Bitmap mBitmap;
50 private float [] array = new float [ 20 ];
51
52 private float mAngle;
53
54 public MyImage(Context context,AttributeSet attrs) {
55 super (context,attrs);
56
57 mBitmap = BitmapFactory.decodeResource(context.getResources(),
58 R.drawable.test);
59 invalidate();
60 }
61
62
63 public void setValues( float [] a){
64 for ( int i = 0 ;i < 20 ;i ++ ){
65 array[i] = a[i];
66 }
67
68 }
69
70 @Override protected void onDraw(Canvas canvas) {
71 Paint paint = mPaint;
72
73
74
75 paint.setColorFilter( null );
76 canvas.drawBitmap(mBitmap, 0 , 0 , paint);
77
78 ColorMatrix cm = new ColorMatrix();
79 // 设置颜色矩阵
80 cm.set(array);
81 // 颜色滤镜，将颜色矩阵应用于图片
82 paint.setColorFilter( new ColorMatrixColorFilter(cm));
83 // 绘图
84 canvas.drawBitmap(mBitmap, 0 , 0 , paint);
85 Log.i( " CMatrix " , " --------->onDraw " );
86
87
88 }
89
90 }

CMatrix类主要负责接收颜色矩阵的设置和重绘，没有要说的。MyImage类中进行绘图工作，首先设置颜色矩阵cm.set(..)从一维数组中读取数据20个数据给颜色矩阵赋值，paint.setColorFilter(..)设置颜色滤镜，然后绘图，效果就出来了（这个过程和PS差不多）如下：

x'=a*x+b*y+c
y'=d*x+e*y+f

1.      旋转

2.      缩放

3.         切变

4.         反射

( , )单位向量
5.         正投影

( , )单位向量

1
CooMatrix类：
2
3 public class CooMatrix extends Activity {
4
5 private Button change;
6 private EditText [] et = new EditText[ 9 ];
7 private float []carray = new float [ 9 ];
8 private MyImage sv;
9 /** Called when the activity is first created. */
10 @Override
11 public void onCreate(Bundle savedInstanceState) {
12 super .onCreate(savedInstanceState);
13 setContentView(R.layout.main);
14
15 change = (Button)findViewById(R.id.set);
16 sv = (MyImage)findViewById(R.id.MyImage);
17
18 for ( int i = 0 ;i < 9 ;i ++ ){
19
20 et[i] = (EditText)findViewById(R.id.indexa + i);
21 carray[i] = Float.valueOf(et[i].getText().toString());
22
23 }
24
25 change.setOnClickListener(l);
26
27
28 }
29
30 private Button.OnClickListener l = new Button.OnClickListener(){
31
32 @Override
33 public void onClick(View arg0) {
34 // TODO Auto-generated method stub
35 getValues();
36 sv.setValues(carray);
37 sv.invalidate();
38 }
39
40 };
41 public void getValues(){
42 for ( int i = 0 ;i < 9 ;i ++ ){
43
44 carray[i] = Float.valueOf(et[i].getText().toString());
45 }
46
47 }
48
49
50 }
51 MyImage类继承自View类：
52 public class MyImage extends View {
53 private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
54 private Bitmap mBitmap;
55 private float [] array = new float [ 9 ];
56
57
58
59 public MyImage(Context context,AttributeSet attrs) {
60 super (context,attrs);
61
62 mBitmap = BitmapFactory.decodeResource(context.getResources(),
63 R.drawable.ic_launcher_android);
64 invalidate();
65 }
66
67
68 public void setValues( float [] a){
69 for ( int i = 0 ;i < 9 ;i ++ ){
70 array[i] = a[i];
71 }
72
73 }
74
75 @Override protected void onDraw(Canvas canvas) {
76 Paint paint = mPaint;
77 canvas.drawBitmap(mBitmap, 0 , 0 , paint);
78 // new 一个坐标变换矩阵
79 Matrix cm = new Matrix();
80 // 为坐标变换矩阵设置响应的值
81 cm.setValues(array);
82 // 按照坐标变换矩阵的描述绘图
83 canvas.drawBitmap(mBitmap, cm, paint);
84 Log.i( " CMatrix " , " --------->onDraw " );
85
86
87 }
88
89
}

setScale(float sx, float sy, float px, float py) 放大
setSkew(float kx, float ky, float px, float py) 斜切
setTranslate(float dx, float dy)                       平移
setRotate(float degrees, float px, float py)    旋转

Matrix类提供了许多Map方法，将原图形映射到目标点构成新的图形，

[x0, y0, x1, y1, x2,y2,...]

src=new float[] { 32, 32, 64, 32 }
dst=new float[] { 32, 32, 64, 48 }