android 之修改图片的某一颜色值

首先我们来创建一个叫Image的类,这个类主要用来处理与图有关的一些操作。

[java] view plain copy

     package org.cn.tools;  

  1. import java.io.IOException;  
  2. import java.io.InputStream;  
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.Matrix;  
  6. import android.graphics.Bitmap.Config;  
  7. import android.graphics.BitmapFactory;  
  8. public class Image {  
  9. public Bitmap bitmap;  //图片资源对象  
  10. /** 
  11.      * 通过id来获得图片  
  12.      * @param context 
  13.      * @param id 
  14.      */  
  15. public Image(Context context,int id){  
  16.         bitmap = BitmapFactory.decodeResource(context.getResources(), id);  
  17.     }  
  18. /** 
  19.      * 通过图片文件名来获得图片,主要用于图片在Asset目录下时 
  20.      * @param context 
  21.      * @param name    图片名 
  22.      * @param scaleW  图片宽的缩放比例 
  23.      * @param scaleH  图片高的缩放比例 
  24.      * @throws IOException 
  25.      */  
  26. public Image(Context context,String name,float scaleW,float scaleH) throws IOException{  
  27.         InputStream is = context.getAssets().open(name);  
  28.         bitmap = BitmapFactory.decodeStream(is);  
  29.         is.close();  
  30.         bitmap = setScaleSize(bitmap, scaleW,scaleH);  
  31.     }  
  32. public int getWidth(){  
  33. return bitmap.getWidth();  
  34.     }  
  35. public int getHeight(){  
  36. return bitmap.getHeight();  
  37.     }  
  38. /** 
  39.      * 将bitmap的像素值放入数组 
  40.      * @param array 
  41.      * @param x 
  42.      * @param y 
  43.      * @return 
  44.      */  
  45. public int[] getPixel(int[] array,int x,int y){  
  46.         bitmap.getPixels(array, 0, getWidth(), 0, 0, getWidth(), getHeight());  
  47. return array;  
  48.     }  
  49. /** 
  50.      * 通过一个像素数组创建Bitmap 
  51.      * @param array 
  52.      * @param w 
  53.      * @param h 
  54.      * @return 
  55.      */  
  56. public Bitmap CreateImage(int[] array,int w,int h){  
  57.         Bitmap image = Bitmap.createBitmap(array, w, h, Config.RGB_565);  
  58. return image;  
  59.     }  
  60. /** 
  61.      * 图片缩放 
  62.      * @param bitmap 
  63.      * @param arg0 
  64.      * @param arg1 
  65.      * @return 
  66.      */  
  67. private Bitmap setScaleSize(Bitmap bitmap,float arg0,float arg1){  
  68.         Matrix matrix = new Matrix();  
  69.         matrix.postScale(arg0,arg1);  
  70.         bitmap = Bitmap.createBitmap(bitmap, 0, 0,getWidth(),getHeight(), matrix,true);  
  71. return bitmap;  
  72.     }  
  73. /** 
  74.      * 图片内存回收 
  75.      *  
  76.      * @param image 
  77.      * @return 
  78.      */  
  79. public static void free(Image image) {  
  80. try {  
  81. if(image.bitmap != null){  
  82. if(image.bitmap.isRecycled()==false) {//如果没有回收    
  83. //回收图片所占的内存  
  84.                     image.bitmap.recycle();    
  85. if(image.bitmap.isRecycled()== true){  
  86.                         image.bitmap = null;  
  87.                     }  
  88.                 }else{  
  89.                     image.bitmap = null;  
  90.                 }  
  91.             }else{  
  92.                 image.bitmap = null;  
  93. //return null;  
  94.             }  
  95.         } catch (Exception e) {  
  96.             e.printStackTrace();  
  97.         }  
  98. //return image;  
  99.     }  
  100. }</span>  

创建完image处理类后我们便可以再activity里进行操作处理了,创建ImageAct.java.例子是将白色换为红色。

[java] view plain copy

     package org.cn;  

  1. import java.io.IOException;  
  2. import java.lang.Character.UnicodeBlock;  
  3. import android.app.Activity;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.drawable.BitmapDrawable;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.view.Window;  
  10. import android.view.WindowManager;  
  11. import android.view.WindowManager.LayoutParams;  
  12. import android.widget.ImageView;  
  13. public class ImageAct extends Activity {  
  14.     Image image;  
  15. int len;  
  16. int red;  
  17. int greed;  
  18. int blue;  
  19. int apla;  
  20. int pix;  
  21. /** Called when the activity is first created. */  
  22. @Override  
  23. public void onCreate(Bundle savedInstanceState) {  
  24. super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26. try {  
  27.             image = new Image(this, "Alfa_Romeo_8C_Competizione_Detail.png",1,1);  
  28.             len = image.getWidth() * image.getHeight();  
  29. int[] imageARGB = new int[len];  
  30. int[] newimage = new int[len];  
  31.             image.getPixel(imageARGB, 0, 0);  
  32. for (int i = 0; i < image.getHeight(); i++)  
  33. for (int j = 0; j < image.getWidth(); j++) {  
  34. if (imageARGB[i * image.getWidth() + j] != 0) {  
  35. //apla = ((imageARGB[i * image.getWidth() + j]&0xaa000000)>>24);  
  36.                         red = ((imageARGB[i * image.getWidth() + j] & 0x00ff0000) >> 16);  
  37.                         greed = ((imageARGB[i * image.getWidth() + j] & 0x0000ff00) >> 8);  
  38.                         blue = ((imageARGB[i * image.getWidth() + j] & 0x000000ff));  
  39. // int apla = ((5 * 255 / 10) << 24) | 0x00ffffff;  
  40. if (red == 255 && greed == 255 & blue == 255) {  
  41.                             red = 255;  
  42.                             greed = 0;  
  43.                             blue = 0;  
  44. // pix =((red<<16)|(greed<<8)|blue)&apla;  
  45.                         }  
  46.                         pix = (red << 16) | (greed << 8) | blue;  
  47.                         newimage[i * image.getWidth() + j] = pix;  
  48.                     } else  
  49.                         newimage[i * image.getWidth() + j] = imageARGB[i  
  50.                                 * image.getWidth() + j];  
  51.                 }  
  52.             Bitmap bitmap = image.CreateImage(newimage, image.getWidth(),  
  53.                     image.getHeight());  
  54.             ((ImageView) findViewById(R.id.imageView1)).setImageBitmap(bitmap);  
  55.         } catch (IOException e) {  
  56. // TODO Auto-generated catch block  
  57.             e.printStackTrace();  
  58.         }  
  59.     }  
  60. }


ok通过上面的两个类就可以将图片里的白色转换为红色了。

原文地址:https://www.cnblogs.com/Free-Thinker/p/6201493.html