canvas.drawBitmap() only honors alpha layer when it is 0
I am loading png resources with BitmapFactory.decodeResource
and then drawing them on a Canvas
using drawBitmap()
.
I draw different layers one at a time so that transparent objects should occlude what they are supposed to, but when I have alpha levels in my pngs that are above 0 they seem to get ignored. Places where alpha is 0 aren't rendered which is correct, but where the alpha is less than 255 instead of blending the color with the existing color at that pixel it just draws it without any alpha blending at all.
How can I draw a bitmap on a Canvas
with proper blending based on the source images' alpha channel? Pertinent code snippets follow:
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);
}
Have you tried changing the format of your SurfaceView
(if you are using one) and/or your Window
to match the format (ARGB_8888
) of the Bitmap you are using? This may be preventing you from having true transparency effects, and in any case, you should try to match up your window/bitmap formats for better performance/quality.
Try Window.setFormat(PixelFormat.RGBA_8888);
and SurfaceHolder.setFormat(PixelFormat.RGBA_8888;
to set the pixel format.
精彩评论