Canvas shows transparent part of bitmap in black color - Android

Zankhna picture Zankhna · Oct 3, 2013 · Viewed 7.7k times · Source

In my android application, I want to draw two images - img1 and img2. At first, I will draw img2 on Canvas. After that i will draw img1 on Canvas which will overlap img2. Img1 contains transparent part. The problem is that, transparent part of img1 is shown in Black color, but I want img2 to be visible through the transparent part of img1. I am not able to do that. Please help me to solve this problem. Thank you.

Code:

protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap b = BitmapFactory.decodeResource(getResources(),
                R.drawable.white_bg);    //img2
        canvas.drawBitmap(b, 0, 0, null);
        canvas.save();

        canvas.drawBitmap(realImage, 0, 0, null);  //img1
    }

Answer

Rubicon Bezique picture Rubicon Bezique · Jun 11, 2015

Try bitmap.setHasAlpha(true) after you load the bitmap.