Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas shows transparent part of bitmap in black color - Android

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
    }
like image 776
Zankhna Avatar asked Oct 03 '13 07:10

Zankhna


2 Answers

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

like image 106
Rubicon Bezique Avatar answered Oct 02 '22 14:10

Rubicon Bezique


After some modification in my code, i got my output. Here is a code which i used.

  public class FrameView extends View{

    Bitmap bitmap = null;

    public FrameView(Context context) {
            super(context);
            this.context = context;

        }

        public FrameView(Context context, AttributeSet attrs) {
            super(context, attrs);
            bitmap = Bitmap.createBitmap(this.screenWidth, this.screenHeight,
                    Bitmap.Config.ARGB_8888);

        }

        public FrameView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            this.context = context;
        }

    @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if (isTouchGestures) {
                invalidate();
                mImgDrawables.get(0).draw(canvas);
                canvas.drawBitmap(bitmap, 0, 0, null);
            }

        }

    //this function is invoked from my activity which is using this view
    public void setFrame(int frame) {

            bitmap = BitmapFactory.decodeStream(getResources().openRawResource(
                    frame));

            bitmap = Bitmap.createScaledBitmap(bitmap, this.screenWidth,
                    this.screenHeight, true);

        }
    }
like image 31
Zankhna Avatar answered Oct 02 '22 13:10

Zankhna