1 ImageView iv = new ImageView(this); 2 setContentView(iv); 3 Bitmap originImg = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 4 Bitmap grayImg = Bitmap.createBitmap(originImg.getWidth(), originImg.getHeight(), Bitmap.Config.ARGB_8888); 5 Canvas canvas = new Canvas(grayImg); 6 Paint paint = new Paint(); 7 ColorMatrix colorMatrix = new ColorMatrix(); 8 colorMatrix.setSaturation(0); 9 ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix); 10 paint.setColorFilter(colorMatrixFilter); 11 canvas.drawBitmap(originImg, 0, 0, paint); 12 iv.setImageBitmap(grayImg);
这里起关键作用的是colorMatrix.setSaturation(0);
0会把图像变成灰度图。只有黑白。