Search code examples
androidbitmapandroid-canvasbitmapfactory

create a bitmap with a bitmap inside


I know - the title may sounds strange. Let me explain:

I created an image to show you, what I'm talking about: the problem

I got an image (Bitmap (1)), that as the size of 150w/200h.

Now I need to make the bitmap bigger ((2) 400w/400h), but the original image must have the same size. So that the image is embedded in white background.

I think one way to solve it is this: * create a big bitmap * create a canvas for it * draw the original bitmap on the canvas * draw the canvas * generate a bitmap of the canvas

The problem for me is, that it must be done in a background thread without drawing a view. I hope you understand me.


Solution

  • You can use the code bellow to achive it. Where smallBitmap is your original image and bigBitmap is the final image:

    Bitmap bigBitmap = Bitmap.createBitmap(width, height , Bitmap.Config.ARGB_8888);
    canvas = new Canvas(bigBitmap);
    canvas.drawBitmap(smallBitmap, left, top, new Paint());
    

    Regards.