首先我們來創建一個叫Image的類,這個類主要用來處理與圖有關的一些操作。
[java]
<span style="font-size:18px;">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.例子是將白色換為紅色。
[java]
<span style="font-size:18px;">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();
}
}
}</span>
ok通過上面的兩個類就可以將圖片裡的白色轉換為紅色了。