0

I am using following code to draw bitmaps on top of each other.

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
    }

    public class MyView extends View
    {
        public MyView(Context context)
        {
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas)
        {

            Bitmap bitmap = null;
            Resources res = getResources();

            Bitmap bitmap1 = BitmapFactory.decodeResource(res, R.drawable.ic_launcher); 

            Bitmap bitmap2 = BitmapFactory.decodeResource(res, R.drawable.ic_launcher2); 

            bitmap = Bitmap.createBitmap(bitmap1.getWidth() + 200, bitmap1.getHeight() + 200,
                    Config.ARGB_8888);

            canvas.drawBitmap(bitmap1, 0, 0, null);

            super.onDraw(canvas);

         }
     }
  }

The following line draws bitmap1 on the canvas.

canvas.drawBitmap(bitmap1, 0, 0, null);

However if I use following code, it does not draw anything. I understand it's new canvas but where it will draw then?

    canvas = new Canvas(bitmap); // Sets background bitmap.
    canvas.drawBitmap(bitmap1, 0, 0, null);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(bitmap2, 0, 0, paint)

For existing canvas object, I have also tried using canvas.setBitmap(bitmap); but still it does not draw anything. Any help would be appreciated. Thanks.

Max Testing
  • 135
  • 2
  • 8
  • Why are you creating a new `Canvas`? What is `c`? – tachyonflux Aug 14 '15 at 23:42
  • ouch. c was a typo. I edited it to show canvas. I don't know how to set 'bitmap' for background, so I tried created a new canvas object. I also tried setting canvas.setBitmap(bitmap) but it still does not draw anything. – Max Testing Aug 14 '15 at 23:54
  • I don't understand what issue you're having. What was wrong with `drawBitmap`? – tachyonflux Aug 15 '15 at 00:45

1 Answers1

0

I figured it out. This is how one can do it. I copied overlay() method from SO but I forgot the reference.

   @Override
    protected void onDraw(Canvas canvas)
    {
        Resources res = getResources();
        bitmapOverlay(canvas, res);
        super.onDraw(canvas);
    }


public static void bitmapOverlay(Canvas canvas, Resources res){
    Bitmap bitmap = null;
    try {

        Bitmap bitmap1 = BitmapFactory.decodeResource(res, R.drawable.ic_launcher); // blue

        Bitmap bitmap2 = BitmapFactory.decodeResource(res, R.drawable.dashboard_icon); // green

        Bitmap result = overlay(bitmap2, bitmap1);
        canvas.drawBitmap(result, 300, 300, null);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth() + 200, bmp2.getHeight() + 200,
            bmp1.getConfig());
    float left = (bmp2.getWidth() - (bmp1.getWidth() * ((float) bmp2.getHeight() / (float) bmp1
            .getHeight()))) / (float) 2.0;
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, left, 0, null);
    canvas.drawBitmap(bmp2, new Matrix(), null);
    return bmOverlay;
}
Max Testing
  • 135
  • 2
  • 8