Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a bitmap's opacity?

I have a bitmap:

Bitmap bitmap = BitmapFactory.decodeFile("some/arbitrary/path/image.jpg"); 

But I'm not going to display the image to the user. I want the alpha to be 100 (out of 255). If this is not possible, can I set the opacity of the Bitmap?

like image 569
Mohit Deshpande Avatar asked Feb 25 '11 15:02

Mohit Deshpande


People also ask

How do I change the transparency of an image in C#?

The ImageAttributes method will work fine with PNG as the original post has it listed, but for JPEG you need to flood fill the graphics canvas with a color first. Since this can leave a tiny undesired border, only do it if the opacity is something less than 1.0: if(opacity < 1.0) { // g is a Graphics object g.

How do you clear a bitmap?

You can use eraseColor on bitmap to set its color to Transparent. It will useable again without recreating it.


1 Answers

As far as I know, opacity or other color filters can't be set on the Bitmap itself. You will need to set the alpha when you use the image:

If you're using ImageView, there is ImageView.setAlpha().

If you're using a Canvas, then you need to use Paint.setAlpha():

Paint paint = new Paint(); paint.setAlpha(100); canvas.drawBitmap(bitmap, src, dst, paint);

Also, incorporating WarrenFaith's answer, if you will use the Bitmap where a drawable is required, you can use BitmapDrawable.setAlpha().

like image 199
Matthew Willis Avatar answered Sep 28 '22 07:09

Matthew Willis