原图                                                                          效果图

代码:

  1. package com.color;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.graphics.BitmapFactory;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.util.AttributeSet;
  9. import android.widget.ImageView;
  10. public class ColorView extends ImageView {
  11. private Paint myPaint = null;
  12. private Bitmap bitmap = null;
  13. private int width, height;
  14. private int[] oldPixels;
  15. private int[] newPixels;
  16. private int color, color2;
  17. private int pixelsR, pixelsG, pixelsB, pixelsA, pixelsR2, pixelsG2,
  18. pixelsB2;
  19. public ColorView(Context context, AttributeSet attrs) {
  20. super(context, attrs);
  21. bitmap = BitmapFactory.decodeResource(context.getResources(),
  22. R.drawable.ww);
  23. width = bitmap.getWidth();
  24. height = bitmap.getHeight();
  25. oldPixels = new int[width * height];
  26. newPixels = new int[width * height];
  27. invalidate();
  28. }
  29. @Override
  30. protected void onDraw(Canvas canvas) {
  31. super.onDraw(canvas);
  32. // 获取像素
  33. bitmap.getPixels(oldPixels, 0, width, 0, 0, width, height);
  34. for (int i = 1; i < height * width; i++) {
  35. color = oldPixels[i];
  36. // 获取RGB分量
  37. pixelsA = Color.alpha(color);
  38. pixelsR = Color.red(color);
  39. pixelsG = Color.green(color);
  40. pixelsB = Color.blue(color);
  41. //R
  42. int pixel = pixelsR - pixelsG - pixelsB;
  43. pixel = pixel * 3 / 2;
  44. if (pixel < 0) {
  45. pixel = -pixel;
  46. }
  47. if (pixel > 255){
  48. pixel = 255;
  49. }
  50. pixelsR = pixel; // 计算后重置R值,以下类同
  51. //G
  52. pixel = pixelsG - pixelsR - pixelsB;
  53. pixel = pixel * 3 / 2;
  54. if (pixel < 0) {
  55. pixel = -pixel;
  56. }
  57. if (pixel > 255){
  58. pixel = 255;
  59. }
  60. pixelsG = pixel;
  61. //B
  62. pixel = pixelsB - pixelsR - pixelsG;
  63. pixel = pixel * 3 / 2;
  64. if (pixel < 0) {
  65. pixel = -pixel;
  66. }
  67. if (pixel > 255){
  68. pixel = 255;
  69. }
  70. pixelsB = pixel;
  71. // 根据新的RGB生成新像素
  72. newPixels[i] = Color.argb(pixelsA, pixelsR, pixelsG, pixelsB);
  73. }
  74. // 根据新像素生成新图片
  75. bitmap.setPixels(newPixels, 0, width, 0, 0, width, height);
  76. canvas.drawBitmap(bitmap, 0, 0, myPaint);
  77. }
  78. }

参考博文:点击打开链接

最新文章

  1. linux -小记(2)问题:yum 安装报错&quot;Another app is currently holding the yum lock; waiting for it to exit... ...: yum Memory : 26 M RSS (868 MB VSZ) Started: Wed Oct 26 22:48:24 2016 - 0&quot;
  2. MyBatis知多少(17)MyBatis和JDBC
  3. android 学习随笔十五(Activity的生命周期与摧毁时返回数据 )
  4. APK反编译之一
  5. php生成缩略图
  6. PHP不显示报错了怎么办~
  7. 全球最快的JS模板引擎
  8. paip.tree 生成目录树到txt后的折叠查看
  9. Software development process
  10. linkin大话数据结构--Queue
  11. jQuery学习之旅 Item4 细说DOM操作
  12. nodejs搭建web服务器
  13. python 获取mac地址zz
  14. .net开发COM组件之组件签名&amp;注册
  15. C# 无法将类型为“__DynamicallyInvokableAttribute”的对象强制转换为类型...
  16. Git Learning3 Eclipse Tools(未完成)
  17. 有哪些api接口可以实现微信自动唤醒浏览器,下载app,打开网页
  18. javascript的hashCode实现
  19. kali蓝牙连接
  20. inline-block元素间隙问题原因及解决方法

热门文章

  1. numa 和 mysql
  2. Goldengate进程的合并与拆分规范
  3. iOS——扬声器与听筒的切换
  4. Centos7(阿里云服务器)安装Anaconda的详细步骤与心得
  5. 在ubuntu上使用华为的3G无线上网卡 - usb_modeswitch
  6. CSS动画框架Loaders.css +animate.css
  7. RC Immix
  8. python note #1
  9. 洛谷 P1952 火星上的加法运算_NOI导刊2009提高(3)
  10. [Poi] Use Poi to Build an Index.js with Modern JavaScript Features