How to draw a bitmap with transparency

How to draw a bitmap with a given color as transparent?
For example, I want all white pixels to be transparent.

+4
source share
3 answers

you need to check every pixel of the image and change its color. You will receive a reply to this message.

+2
source

You need to set the Alpha value for the paint that you transfer to Bitmap.

http://developer.android.com/reference/android/graphics/Paint.html#setAlpha%28int%29

Values โ€‹โ€‹range from 0 to 255

EDIT:

Paint p = new Paint(); //Set Blue Color p.setColor(Color.WHITE); //Set transparency roughly at 50% p.setAlpha(125); 
+13
source

Another approach is to draw in transparent color on the canvas (holes for drawing). Bitmap needs an alpha channel.

  //Set transparent paint - you need all of these three Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); paint.setColor(Color.TRANSPARENT); // Do you wanna soften? // Set paint transparency: // 0 = transparent ink, no visible effect // 255 = full ink, hole in the bitmap p.setAlpha(192); // Do you want some blur? // Set blur radius in pixel paint.setMaskFilter(new BlurMaskFilter(10, Blur.NORMAL)); 
+1
source

Source: https://habr.com/ru/post/1344514/


All Articles