将彩色图片转为灰度图

1.将彩色Bitmap转换为灰色Bitmap

		Bitmap grayImg = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(grayImg);

		Paint paint = new Paint();
		//设置颜色矩阵
		ColorMatrix colorMatrix = new ColorMatrix();
		//设置饱和度——变灰
		colorMatrix.setSaturation(0);
		//颜色滤镜
		ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(
				colorMatrix);
		//将颜色矩阵应用于图片
		paint.setColorFilter(colorMatrixFilter);
		//绘图,bitmap为彩图,最终灰度图为grayImg 
		canvas.drawBitmap(bitmap, 0, 0, paint);
2.Drawable和Bitmap互相转换

①Bitmap转Drawable

		Bitmap bm=xxx; //xxx根据你的情况获取
		BitmapDrawable bd=BitmapDrawable(bm);//BtimapDrawable是Drawable的子类,最终直接使用bd对象即可 
②Drawable转Bitmap

		Drawable d=xxx; //xxx根据自己的情况获取drawable,如:Drawable d = getResources().getDrawable(R.drawable.img);
		BitmapDrawable bd = (BitmapDrawable) d;
		Bitmap bm = bd.getBitmap();








你可能感兴趣的:(将彩色图片转为灰度图)