我想要绘制一个可绘制的灰度。我正在尝试Justin suggested,但它不适用于Donut(1.6)和Eclair(2.1)。它适用于Android 2.2+。我正在尝试的代码如下:
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
drawable.setColorFilter(filter);
任何线索?
答案 0 :(得分:1)
这适用于降低Api,试试
public Bitmap toGrayscale(Bitmap bmpOriginal)
{
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}
如果您想使用画布或画家使用此代码,则无法获得代码的灰度图像,因为您使用了不可变的drawable
Bitmap immutableBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.my_drawable);
Bitmap mutableBitmap = immutableBitmap.copy(Bitmap.Config.ARGB_8888, true);
//you have two bitmaps in memory, so clean up the mess a bit
immutableBitmap.recycle(); immutableBitmap=null;
Drawable d = new BitmapDrawable(mutableBitmap);
//mutate it
d.setColorFilter(new LightingColorFilter(color, lightenColor));
imageView.setImageDrawable(d);