android 使用ColorMatrix把图变成灰色
ColorMatrix类有一个内置的方法可用于改变饱和度。
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(.5f);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
传入一个大于1的数字将增加饱和度,而传入一个0~1之间的数字会减少饱和度。0值将产生一幅灰度图像。
所以获得灰色图像的代码如下:
public static final Bitmap grey(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap faceIconGreyBitmap = Bitmap
.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(faceIconGreyBitmap);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(
colorMatrix);
paint.setColorFilter(colorMatrixFilter);
canvas.drawBitmap(bitmap, 0, 0, paint);
return faceIconGreyBitmap;
}