Home » Android » android – How to copy a smaller bitmap into a larger one?

android – How to copy a smaller bitmap into a larger one?

Posted by: admin June 15, 2020 Leave a comment


Hopefully this should be an easy question. I’m trying to copy a series of small bitmaps into a larger one, arranging them side by side without any gaps or overlap in their pixels. For example, if I have 3 square bitmaps, I’d like to copy them into one long and thin rectangle. I know how to do the opposite, namely creating a small bitmap out of a larger one, but not this way around. What’s the right command?

(If anyone’s curious, I want to do this to be able to reuse some code I wrote for handling animation with a single bitmap.)


How to&Answers:

Create a canvas for the large bitmap, then use that to draw your small bitmaps. I’m pretty new to android, but I’m guessing that it’s something like this:

Bitmap makeBigBitmap(Bitmap srcBmps[]) {
    Bitmap wideBmp;
    Canvas wideBmpCanvas;
    Rect src, dest;

    // assume all of the src bitmaps are the same height & width
    wideBmp = Bitmap.createBitmap(srcBmps[0].getWidth() * srcBmps.length, 
        srcBmps[0].getHeight(), srcBitmaps[0].getConfig());

    wideBmpCanvas = new Canvas(wideBmp); 

    for (int i = 0; i < srcBmps.length; i++) {
         src = new Rect(0, 0, srcBmps[i].getWidth(), srcBmps[i].getHeight());
         dest = new Rect(src); 
         dest.offset(i * srcBmps[i].getWidth(), 0); 

         wideBmpCanvas.drawBitmap(srcBmps[i], src, dest, null); 

    return wideBmp;