Skip to content

Instantly share code, notes, and snippets.

@monove
Last active May 2, 2023 04:43
Show Gist options
  • Save monove/7d188031825eabb987aa2d734534f6c7 to your computer and use it in GitHub Desktop.
Save monove/7d188031825eabb987aa2d734534f6c7 to your computer and use it in GitHub Desktop.
Java Merge Multiple Bitmaps, horizontally or vertically
private Bitmap combineBitmaps(ArrayList<Bitmap> bitmaps, Boolean isHorizontal) {
int w = 0, h = 0;
for (int i = 0; i < bitmaps.size(); i++) {
if(i == 0 || isHorizontal){
w += bitmaps.get(i).getWidth();
} else if(!isHorizontal && bitmaps.get(i).getWidth() > w) {
w = bitmaps.get(i).getWidth();
}
if(i == 0 || !isHorizontal){
h += bitmaps.get(i).getHeight();
} else if(isHorizontal && bitmaps.get(i).getHeight() > h) {
h = bitmaps.get(i).getHeight();
}
}
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
int pos = 0;
for (int i = 0; i < bitmaps.size(); i++) {
if(isHorizontal){
canvas.drawBitmap(bitmaps.get(i), pos, 0f, null);
} else {
canvas.drawBitmap(bitmaps.get(i), 0f, pos, null);
}
pos += isHorizontal ? bitmaps.get(i).getWidth() : bitmaps.get(i).getHeight();
}
return bitmap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment