(1) 灰度
public Bitmap switchColor(Bitmap switchBitmap){
int width=switchBitmap.getWidth();
int height=switchBitmap.getHeight();
Bitmap newBitmap=Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);
Canvas canvas=new Canvas(newBitmap);
Paint paint=new Paint();
ColorMatrix colorMatrix=new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter filter=new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(filter);
canvas.drawBitmap(switchBitmap, 0, 0,paint);
return newBitmap;
http://blog.sina.com.cn/s/blog_4ab2f5c801015dfs.html
此外参照这篇博客,还可以调整灰度的效果,可以偏亮或者偏柔和
(2)黑白
public Bitmap switchColor(Bitmap switchBitmap){
int width=switchBitmap.getWidth();
int height=switchBitmap.getHeight();
// Turn the picture black and white
Bitmap newBitmap=Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);
Canvas canvas=new Canvas(newBitmap);
canvas.drawBitmap(switchBitmap, new Matrix(), new Paint());
int current_color,red,green,blue,alpha,avg=0;
for (int i=0;i<width;i++){
for (int j=0;j<height;j++){
current_color=switchBitmap.getPixel(i, j);
red=Color.red(current_color);
green=Color.green(current_color);
blue=Color.blue(current_color);
alpha=Color.alpha(current_color);
avg=(red+green+blue)/3;// RGB average
if (avg>=126){
newBitmap.setPixel(i, j, Color.argb(alpha, 255, 255, 255));// white
} else{
newBitmap.setPixel(i, j, Color.argb(alpha, 0, 0, 0));// black
}
}
}
return newBitmap;
}
其中avg的值可以自己设定
(3)底片效果
public Bitmap switchInvert(Bitmap switchBitmap){
int width=switchBitmap.getWidth();
int height=switchBitmap.getHeight();
Bitmap newBitmap=Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);
Canvas canvas=new Canvas(newBitmap);
canvas.drawBitmap(switchBitmap, new Matrix(), new Paint());
int current_color,red,green,blue,alpha,r,g,b=0;
for (int i=0;i<width;i++){
for (int j=0;j<height;j++){
current_color=switchBitmap.getPixel(i, j);
red=Color.red(current_color);
green=Color.green(current_color);
blue=Color.blue(current_color);
alpha=Color.alpha(current_color);
//底片效果是当前像素点对应的red,green,blue三色对255做差
r=255-red;
g=255-green;
b=255-blue;
newBitmap.setPixel(i, j, Color.argb(alpha, r, g, b));
}
}
return newBitmap;
}