Canvas.drawBitmap () only honors the alpha layer when it is 0

I load png resources using BitmapFactory.decodeResource and then draw them on Canvas using drawBitmap() .

I draw the different layers one at a time so that the transparent objects cover what they were supposed to, but when I have alpha levels in my png that are above 0, they seem to be ignored. Places where alpha is 0 are not displayed correctly, but where alpha is less than 255, instead of blending the color with the existing color on that pixel, it just draws it without any alpha blending.

How to draw a bitmap on Canvas with proper blending based on alpha channel of source images? The following are snippets of code:

 BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig = Config.ARGB_8888; ... decorationTextures[1] = new TextureStatic(BitmapFactory.decodeResource(resources, R.drawable.ice_1, opt)); decorationTextures[2] = new TextureStatic(BitmapFactory.decodeResource(resources, R.drawable.ice_2, opt)); ... if(mTexture != null && mInPlay) { if(mZone != null) canvas.drawBitmap(mTexture.getBitmap(), mScreenX + mZone.getXOffset(), mScreenY + mZone.getYOffset(), null); else canvas.drawBitmap(mTexture.getBitmap(), mScreenX, mScreenY, null); } 
+4
source share
1 answer

Have you tried changing the format of your SurfaceView (if you are using it) and / or your Window to match the format ( ARGB_8888 ) of the bitmap used? This may prevent you from having true transparency effects, and in any case, you should try to match your window / bitmap formats for better performance / quality .

Try Window.setFormat(PixelFormat.RGBA_8888); and SurfaceHolder.setFormat(PixelFormat.RGBA_8888; set the pixel format.

0
source

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


All Articles