Question

I am using following code to make my picture black

BitmapDrawable bdarw = new BitmapDrawable(imagePath);

                ColorMatrix cm = new ColorMatrix();
                cm.set(new float[] {
                        2, 1f, 0, 0, 0, 
                        0, 2, 0f, 0, 0,
                        0, 0, 2, 0f, 0, 
                        0, 0, 0, 1f, 0 });

                bdarw.setColorFilter(new ColorMatrixColorFilter(cm));

                Bitmap bitmap= bdarw.getBitmap();

                ImageView imageView = (ImageView) findViewById(R.id.imgV);
                imageView.setImageBitmap(bitmap);

but it seems color matrix is not correct can you please help me out

Was it helpful?

Solution

Surely for black you need 0 for all the color components. The only thing to worry about is the alpha. To leave it as is you would want.

            cm.set(new float[] {
                     0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0,
                     0, 0, 0, 1f, 0 }); 

To force the alpha to zero change the one to a 0, to force the alpha to ff change the last 0 to either 1 or 255 I am not sure which, try it and see.

ColorMatrix documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top