本文实例讲述了android编程之图片颜色处理方法。分享给大家供大家参考,具体如下:
你想做到跟美图秀秀一样可以处理自己的照片,美化自己的照片吗?其实你也可以自己做一个这样的软件,废话不多说了,直接上图,上代码了!
效果图如下:
没处理前:
处理之后:
mainactivity.java的代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
package net.loonggg.test; import android.app.activity; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.color; import android.graphics.colormatrix; import android.graphics.colormatrixcolorfilter; import android.graphics.matrix; import android.graphics.paint; import android.os.bundle; import android.widget.imageview; import android.widget.seekbar; import android.widget.seekbar.onseekbarchangelistener; public class mainactivity extends activity { private seekbar sb1, sb2, sb3, sb4, sb5; private imageview iv; private bitmap bitmap, updatebitmap; private canvas canvas; private paint paint; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); iv = (imageview) findviewbyid(r.id.iv); sb1 = (seekbar) findviewbyid(r.id.sb1); sb2 = (seekbar) findviewbyid(r.id.sb2); sb3 = (seekbar) findviewbyid(r.id.sb3); sb4 = (seekbar) findviewbyid(r.id.sb4); sb5 = (seekbar) findviewbyid(r.id.sb5); bitmap = bitmapfactory.decoderesource(getresources(), r.drawable.b); updatebitmap = bitmap.createbitmap(bitmap.getwidth(), bitmap.getheight(), bitmap.getconfig()); canvas = new canvas(updatebitmap); paint = new paint(); final colormatrix cm = new colormatrix(); paint.setcolorfilter( new colormatrixcolorfilter(cm)); paint.setcolor(color.black); paint.setantialias( true ); final matrix matrix = new matrix(); canvas.drawbitmap(bitmap, matrix, paint); iv.setimagebitmap(updatebitmap); /** * rgb三原色 红色值的设置 */ sb1.setonseekbarchangelistener( new onseekbarchangelistener() { @override public void onstoptrackingtouch(seekbar seekbar) { int progress = seekbar.getprogress(); cm.set( new float [] { progress / 128f, 0 , 0 , 0 , 0 , // 红色值 0 , 1 , 0 , 0 , 0 , // 绿色值 0 , 0 , 1 , 0 , 0 , // 蓝色值 0 , 0 , 0 , 1 , 0 // 透明度 }); paint.setcolorfilter( new colormatrixcolorfilter(cm)); canvas.drawbitmap(bitmap, matrix, paint); iv.setimagebitmap(updatebitmap); } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { } }); /** * rgb三原色 绿色值的设置 */ sb2.setonseekbarchangelistener( new onseekbarchangelistener() { @override public void onstoptrackingtouch(seekbar seekbar) { int progress = seekbar.getprogress(); cm.set( new float [] { 1 , 0 , 0 , 0 , 0 , // 红色值 0 , progress / 128f, 0 , 0 , 0 , // 绿色值 0 , 0 , 1 , 0 , 0 , // 蓝色值 0 , 0 , 0 , 1 , 0 // 透明度 }); paint.setcolorfilter( new colormatrixcolorfilter(cm)); canvas.drawbitmap(bitmap, matrix, paint); iv.setimagebitmap(updatebitmap); } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { } }); /** * rgb三原色 蓝色值的设置 */ sb3.setonseekbarchangelistener( new onseekbarchangelistener() { @override public void onstoptrackingtouch(seekbar seekbar) { int progress = seekbar.getprogress(); cm.set( new float [] { 1 , 0 , 0 , 0 , 0 , // 红色值 0 , 1 , 0 , 0 , 0 , // 绿色值 0 , 0 , progress / 128f, 0 , 0 , // 蓝色值 0 , 0 , 0 , 1 , 0 // 透明度 }); paint.setcolorfilter( new colormatrixcolorfilter(cm)); canvas.drawbitmap(bitmap, matrix, paint); iv.setimagebitmap(updatebitmap); } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { } }); /** * rgb三原色 三个值都改变为设置饱和度(亮度) */ sb4.setonseekbarchangelistener( new onseekbarchangelistener() { @override public void onstoptrackingtouch(seekbar seekbar) { int progress = seekbar.getprogress(); cm.set( new float [] { progress / 128f, 0 , 0 , 0 , 0 , // 红色值 0 , progress / 128f, 0 , 0 , 0 , // 绿色值 0 , 0 , progress / 128f, 0 , 0 , // 蓝色值 0 , 0 , 0 , 1 , 0 // 透明度 }); paint.setcolorfilter( new colormatrixcolorfilter(cm)); canvas.drawbitmap(bitmap, matrix, paint); iv.setimagebitmap(updatebitmap); } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { } }); /** * rgb三原色 设置透明度 */ sb5.setonseekbarchangelistener( new onseekbarchangelistener() { @override public void onstoptrackingtouch(seekbar seekbar) { int progress = seekbar.getprogress(); cm.set( new float [] { 1 , 0 , 0 , 0 , 0 , // 红色值 0 , 1 , 0 , 0 , 0 , // 绿色值 0 , 0 , 1 , 0 , 0 , // 蓝色值 0 , 0 , 0 , progress / 128f, 0 // 透明度 }); paint.setcolorfilter( new colormatrixcolorfilter(cm)); canvas.drawbitmap(bitmap, matrix, paint); iv.setimagebitmap(updatebitmap); } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onprogresschanged(seekbar seekbar, int progress, boolean fromuser) { } }); } } |
布局文件代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:background= "#cdcdcd" android:orientation= "vertical" tools:context= ".mainactivity" > <imageview android:id= "@+id/iv" android:layout_width= "fill_parent" android:layout_height= "wrap_content" /> <linearlayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:gravity= "center_vertical" android:orientation= "horizontal" android:padding= "10dp" > <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "红色:" android:textcolor= "#ff0000" android:textsize= "24sp" /> <seekbar android:id= "@+id/sb1" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:max= "256" android:progress= "128" /> </linearlayout> <linearlayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:gravity= "center_vertical" android:orientation= "horizontal" android:padding= "10dp" > <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "绿色:" android:textcolor= "#00ff00" android:textsize= "24sp" /> <seekbar android:id= "@+id/sb2" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:max= "256" android:progress= "128" /> </linearlayout> <linearlayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:gravity= "center_vertical" android:orientation= "horizontal" android:padding= "10dp" > <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "蓝色:" android:textcolor= "#0000ff" android:textsize= "24sp" /> <seekbar android:id= "@+id/sb3" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:max= "256" android:progress= "128" /> </linearlayout> <linearlayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:gravity= "center_vertical" android:orientation= "horizontal" android:padding= "10dp" > <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "饱和度:" android:textcolor= "#000000" android:textsize= "16.5sp" /> <seekbar android:id= "@+id/sb4" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:max= "256" android:progress= "128" /> </linearlayout> <linearlayout android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:gravity= "center_vertical" android:orientation= "horizontal" android:padding= "10dp" > <textview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "透明度:" android:textcolor= "#000000" android:textsize= "16.5sp" /> <seekbar android:id= "@+id/sb5" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:max= "256" android:progress= "128" /> </linearlayout> </linearlayout> |
到这里就完了,看明白了吗?
希望本文所述对大家android程序设计有所帮助。