首页 > 系统 > Android > 正文

Android编程之图片颜色处理方法

2019-10-24 20:35:10
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Android编程之图片颜色处理方法,涉及Android针对图片的颜色值、饱和度、透明度等处理技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android编程之图片颜色处理方法。分享给大家供大家参考,具体如下:

你想做到跟美图秀秀一样可以处理自己的照片,美化自己的照片吗?其实你也可以自己做一个这样的软件,废话不多说了,直接上图,上代码了!

效果图如下:

没处理前:

Android编程之图片颜色处理方法

处理之后:

Android编程之图片颜色处理方法

MainActivity.java的代码如下:

 

 
  1. package net.loonggg.test;  
  2. import android.app.Activity;  
  3. import android.graphics.Bitmap;  
  4. import android.graphics.BitmapFactory;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.ColorMatrix;  
  8. import android.graphics.ColorMatrixColorFilter;  
  9. import android.graphics.Matrix;  
  10. import android.graphics.Paint;  
  11. import android.os.Bundle;  
  12. import android.widget.ImageView;  
  13. import android.widget.SeekBar;  
  14. import android.widget.SeekBar.OnSeekBarChangeListener;  
  15. public class MainActivity extends Activity {  
  16. private SeekBar sb1, sb2, sb3, sb4, sb5;  
  17. private ImageView iv;  
  18. private Bitmap bitmap, updateBitmap;  
  19. private Canvas canvas;  
  20. private Paint paint;  
  21. @Override 
  22. protected void onCreate(Bundle savedInstanceState) {  
  23. super.onCreate(savedInstanceState);  
  24. setContentView(R.layout.activity_main);  
  25. iv = (ImageView) findViewById(R.id.iv);  
  26. sb1 = (SeekBar) findViewById(R.id.sb1);  
  27. sb2 = (SeekBar) findViewById(R.id.sb2);  
  28. sb3 = (SeekBar) findViewById(R.id.sb3);  
  29. sb4 = (SeekBar) findViewById(R.id.sb4);  
  30. sb5 = (SeekBar) findViewById(R.id.sb5);  
  31. bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.b);  
  32. updateBitmap = Bitmap.createBitmap(bitmap.getWidth(),  
  33. bitmap.getHeight(), bitmap.getConfig());  
  34. canvas = new Canvas(updateBitmap);  
  35. paint = new Paint();  
  36. final ColorMatrix cm = new ColorMatrix();  
  37. paint.setColorFilter(new ColorMatrixColorFilter(cm));  
  38. paint.setColor(Color.BLACK);  
  39. paint.setAntiAlias(true);  
  40. final Matrix matrix = new Matrix();  
  41. canvas.drawBitmap(bitmap, matrix, paint);  
  42. iv.setImageBitmap(updateBitmap);  
  43. /**  
  44. * RGB三原色 红色值的设置  
  45. */ 
  46. sb1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  47. @Override 
  48. public void onStopTrackingTouch(SeekBar seekBar) {  
  49. int progress = seekBar.getProgress();  
  50. cm.set(new float[] { progress / 128f, 0, 0, 0, 0,// 红色值  
  51. 0, 1, 0, 0, 0,// 绿色值  
  52. 0, 0, 1, 0, 0,// 蓝色值  
  53. 0, 0, 0, 1, 0 // 透明度  
  54. });  
  55. paint.setColorFilter(new ColorMatrixColorFilter(cm));  
  56. canvas.drawBitmap(bitmap, matrix, paint);  
  57. iv.setImageBitmap(updateBitmap);  
  58. }  
  59. @Override 
  60. public void onStartTrackingTouch(SeekBar seekBar) {  
  61. }  
  62. @Override 
  63. public void onProgressChanged(SeekBar seekBar, int progress,  
  64. boolean fromUser) {  
  65. }  
  66. });  
  67. /**  
  68. * RGB三原色 绿色值的设置  
  69. */ 
  70. sb2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  71. @Override 
  72. public void onStopTrackingTouch(SeekBar seekBar) {  
  73. int progress = seekBar.getProgress();  
  74. cm.set(new float[] { 1, 0, 0, 0, 0,// 红色值  
  75. 0, progress / 128f, 0, 0, 0,// 绿色值  
  76. 0, 0, 1, 0, 0,// 蓝色值  
  77. 0, 0, 0, 1, 0 // 透明度  
  78. });  
  79. paint.setColorFilter(new ColorMatrixColorFilter(cm));  
  80. canvas.drawBitmap(bitmap, matrix, paint);  
  81. iv.setImageBitmap(updateBitmap);  
  82. }  
  83. @Override 
  84. public void onStartTrackingTouch(SeekBar seekBar) {  
  85. }  
  86. @Override 
  87. public void onProgressChanged(SeekBar seekBar, int progress,  
  88. boolean fromUser) {  
  89. }  
  90. });  
  91. /**  
  92. * RGB三原色 蓝色值的设置  
  93. */ 
  94. sb3.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  95. @Override 
  96. public void onStopTrackingTouch(SeekBar seekBar) {  
  97. int progress = seekBar.getProgress();  
  98. cm.set(new float[] { 1, 0, 0, 0, 0,// 红色值  
  99. 0, 1, 0, 0, 0,// 绿色值  
  100. 0, 0, progress / 128f, 0, 0,// 蓝色值  
  101. 0, 0, 0, 1, 0 // 透明度  
  102. });  
  103. paint.setColorFilter(new ColorMatrixColorFilter(cm));  
  104. canvas.drawBitmap(bitmap, matrix, paint);  
  105. iv.setImageBitmap(updateBitmap);  
  106. }  
  107. @Override 
  108. public void onStartTrackingTouch(SeekBar seekBar) {  
  109. }  
  110. @Override 
  111. public void onProgressChanged(SeekBar seekBar, int progress,  
  112. boolean fromUser) {  
  113. }  
  114. });  
  115. /**  
  116. * RGB三原色 三个值都改变为设置饱和度(亮度)  
  117. */ 
  118. sb4.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  119. @Override 
  120. public void onStopTrackingTouch(SeekBar seekBar) {  
  121. int progress = seekBar.getProgress();  
  122. cm.set(new float[] { progress / 128f, 0, 0, 0, 0,// 红色值  
  123. 0, progress / 128f, 0, 0, 0,// 绿色值  
  124. 0, 0, progress / 128f, 0, 0,// 蓝色值  
  125. 0, 0, 0, 1, 0 // 透明度  
  126. });  
  127. paint.setColorFilter(new ColorMatrixColorFilter(cm));  
  128. canvas.drawBitmap(bitmap, matrix, paint);  
  129. iv.setImageBitmap(updateBitmap);  
  130. }  
  131. @Override 
  132. public void onStartTrackingTouch(SeekBar seekBar) {  
  133. }  
  134. @Override 
  135. public void onProgressChanged(SeekBar seekBar, int progress,  
  136. boolean fromUser) {  
  137. }  
  138. });  
  139. /**  
  140. * RGB三原色 设置透明度  
  141. */ 
  142. sb5.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {  
  143. @Override 
  144. public void onStopTrackingTouch(SeekBar seekBar) {  
  145. int progress = seekBar.getProgress();  
  146. cm.set(new float[] { 1, 0, 0, 0, 0,// 红色值  
  147. 0, 1, 0, 0, 0,// 绿色值  
  148. 0, 0, 1, 0, 0,// 蓝色值  
  149. 0, 0, 0, progress / 128f, 0 // 透明度  
  150. });  
  151. paint.setColorFilter(new ColorMatrixColorFilter(cm));  
  152. canvas.drawBitmap(bitmap, matrix, paint);  
  153. iv.setImageBitmap(updateBitmap);  
  154. }  
  155. @Override 
  156. public void onStartTrackingTouch(SeekBar seekBar) {  
  157. }  
  158. @Override 
  159. public void onProgressChanged(SeekBar seekBar, int progress,  
  160. boolean fromUser) {  
  161. }  
  162. });  
  163. }  

布局文件代码如下:

 

 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2. xmlns:tools="http://schemas.android.com/tools" 
  3. android:layout_width="match_parent" 
  4. android:layout_height="match_parent" 
  5. android:background="#CDCDCD" 
  6. android:orientation="vertical" 
  7. tools:context=".MainActivity" >  
  8. <ImageView 
  9. android:id="@+id/iv" 
  10. android:layout_width="fill_parent" 
  11. android:layout_height="wrap_content" />  
  12. <LinearLayout 
  13. android:layout_width="fill_parent" 
  14. android:layout_height="wrap_content" 
  15. android:gravity="center_vertical" 
  16. android:orientation="horizontal" 
  17. android:padding="10dp" >  
  18. <TextView 
  19. android:layout_width="wrap_content" 
  20. android:layout_height="wrap_content" 
  21. android:text="红色:" 
  22. android:textColor="#FF0000" 
  23. android:textSize="24sp" />  
  24. <SeekBar 
  25. android:id="@+id/sb1" 
  26. android:layout_width="fill_parent" 
  27. android:layout_height="wrap_content" 
  28. android:max="256" 
  29. android:progress="128" />  
  30. </LinearLayout>  
  31. <LinearLayout 
  32. android:layout_width="fill_parent" 
  33. android:layout_height="wrap_content" 
  34. android:gravity="center_vertical" 
  35. android:orientation="horizontal" 
  36. android:padding="10dp" >  
  37. <TextView 
  38. android:layout_width="wrap_content" 
  39. android:layout_height="wrap_content" 
  40. android:text="绿色:" 
  41. android:textColor="#00FF00" 
  42. android:textSize="24sp" />  
  43. <SeekBar 
  44. android:id="@+id/sb2" 
  45. android:layout_width="fill_parent" 
  46. android:layout_height="wrap_content" 
  47. android:max="256" 
  48. android:progress="128" />  
  49. </LinearLayout>  
  50. <LinearLayout 
  51. android:layout_width="fill_parent" 
  52. android:layout_height="wrap_content" 
  53. android:gravity="center_vertical" 
  54. android:orientation="horizontal" 
  55. android:padding="10dp" >  
  56. <TextView 
  57. android:layout_width="wrap_content" 
  58. android:layout_height="wrap_content" 
  59. android:text="蓝色:" 
  60. android:textColor="#0000FF" 
  61. android:textSize="24sp" />  
  62. <SeekBar 
  63. android:id="@+id/sb3" 
  64. android:layout_width="fill_parent" 
  65. android:layout_height="wrap_content" 
  66. android:max="256" 
  67. android:progress="128" />  
  68. </LinearLayout>  
  69. <LinearLayout 
  70. android:layout_width="fill_parent" 
  71. android:layout_height="wrap_content" 
  72. android:gravity="center_vertical" 
  73. android:orientation="horizontal" 
  74. android:padding="10dp" >  
  75. <TextView 
  76. android:layout_width="wrap_content" 
  77. android:layout_height="wrap_content" 
  78. android:text="饱和度:" 
  79. android:textColor="#000000" 
  80. android:textSize="16.5sp" />  
  81. <SeekBar 
  82. android:id="@+id/sb4" 
  83. android:layout_width="fill_parent" 
  84. android:layout_height="wrap_content" 
  85. android:max="256" 
  86. android:progress="128" />  
  87. </LinearLayout>  
  88. <LinearLayout 
  89. android:layout_width="fill_parent" 
  90. android:layout_height="wrap_content" 
  91. android:gravity="center_vertical" 
  92. android:orientation="horizontal" 
  93. android:padding="10dp" >  
  94. <TextView 
  95. android:layout_width="wrap_content" 
  96. android:layout_height="wrap_content" 
  97. android:text="透明度:" 
  98. android:textColor="#000000" 
  99. android:textSize="16.5sp" />  
  100. <SeekBar 
  101. android:id="@+id/sb5" 
  102. android:layout_width="fill_parent" 
  103. android:layout_height="wrap_content" 
  104. android:max="256" 
  105. android:progress="128" />  
  106. </LinearLayout>  
  107. </LinearLayout> 

到这里就完了,看明白了吗?

希望本文所述对大家Android程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表