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
?
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.
You can use eraseColor on bitmap to set its color to Transparent. It will useable again without recreating it.
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With