android 之修改图片的某一颜色值
首先我们来创建一个叫Image的类,这个类主要用来处理与图有关的一些操作。
package org.cn.tools;
- import java.io.IOException;
- import java.io.InputStream;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Matrix;
- import android.graphics.Bitmap.Config;
- import android.graphics.BitmapFactory;
- public class Image {
- public Bitmap bitmap; //图片资源对象
- /**
- * 通过id来获得图片
- * @param context
- * @param id
- */
- public Image(Context context,int id){
- bitmap = BitmapFactory.decodeResource(context.getResources(), id);
- }
- /**
- * 通过图片文件名来获得图片,主要用于图片在Asset目录下时
- * @param context
- * @param name 图片名
- * @param scaleW 图片宽的缩放比例
- * @param scaleH 图片高的缩放比例
- * @throws IOException
- */
- public Image(Context context,String name,float scaleW,float scaleH) throws IOException{
- InputStream is = context.getAssets().open(name);
- bitmap = BitmapFactory.decodeStream(is);
- is.close();
- bitmap = setScaleSize(bitmap, scaleW,scaleH);
- }
- public int getWidth(){
- return bitmap.getWidth();
- }
- public int getHeight(){
- return bitmap.getHeight();
- }
- /**
- * 将bitmap的像素值放入数组
- * @param array
- * @param x
- * @param y
- * @return
- */
- public int[] getPixel(int[] array,int x,int y){
- bitmap.getPixels(array, 0, getWidth(), 0, 0, getWidth(), getHeight());
- return array;
- }
- /**
- * 通过一个像素数组创建Bitmap
- * @param array
- * @param w
- * @param h
- * @return
- */
- public Bitmap CreateImage(int[] array,int w,int h){
- Bitmap image = Bitmap.createBitmap(array, w, h, Config.RGB_565);
- return image;
- }
- /**
- * 图片缩放
- * @param bitmap
- * @param arg0
- * @param arg1
- * @return
- */
- private Bitmap setScaleSize(Bitmap bitmap,float arg0,float arg1){
- Matrix matrix = new Matrix();
- matrix.postScale(arg0,arg1);
- bitmap = Bitmap.createBitmap(bitmap, 0, 0,getWidth(),getHeight(), matrix,true);
- return bitmap;
- }
- /**
- * 图片内存回收
- *
- * @param image
- * @return
- */
- public static void free(Image image) {
- try {
- if(image.bitmap != null){
- if(image.bitmap.isRecycled()==false) {//如果没有回收
- //回收图片所占的内存
- image.bitmap.recycle();
- if(image.bitmap.isRecycled()== true){
- image.bitmap = null;
- }
- }else{
- image.bitmap = null;
- }
- }else{
- image.bitmap = null;
- //return null;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- //return image;
- }
- }</span>
创建完image处理类后我们便可以再activity里进行操作处理了,创建ImageAct.java.例子是将白色换为红色。
package org.cn;
- import java.io.IOException;
- import java.lang.Character.UnicodeBlock;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.WindowManager.LayoutParams;
- import android.widget.ImageView;
- public class ImageAct extends Activity {
- Image image;
- int len;
- int red;
- int greed;
- int blue;
- int apla;
- int pix;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- try {
- image = new Image(this, "Alfa_Romeo_8C_Competizione_Detail.png",1,1);
- len = image.getWidth() * image.getHeight();
- int[] imageARGB = new int[len];
- int[] newimage = new int[len];
- image.getPixel(imageARGB, 0, 0);
- for (int i = 0; i < image.getHeight(); i++)
- for (int j = 0; j < image.getWidth(); j++) {
- if (imageARGB[i * image.getWidth() + j] != 0) {
- //apla = ((imageARGB[i * image.getWidth() + j]&0xaa000000)>>24);
- red = ((imageARGB[i * image.getWidth() + j] & 0x00ff0000) >> 16);
- greed = ((imageARGB[i * image.getWidth() + j] & 0x0000ff00) >> 8);
- blue = ((imageARGB[i * image.getWidth() + j] & 0x000000ff));
- // int apla = ((5 * 255 / 10) << 24) | 0x00ffffff;
- if (red == 255 && greed == 255 & blue == 255) {
- red = 255;
- greed = 0;
- blue = 0;
- // pix =((red<<16)|(greed<<8)|blue)&apla;
- }
- pix = (red << 16) | (greed << 8) | blue;
- newimage[i * image.getWidth() + j] = pix;
- } else
- newimage[i * image.getWidth() + j] = imageARGB[i
- * image.getWidth() + j];
- }
- Bitmap bitmap = image.CreateImage(newimage, image.getWidth(),
- image.getHeight());
- ((ImageView) findViewById(R.id.imageView1)).setImageBitmap(bitmap);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
ok通过上面的两个类就可以将图片里的白色转换为红色了。