How to Place Bitmaps Vertically

I've got some code from user xil3 where it merges the bitmaps horizontally. Does anyone know how I can make it to do it vertically instead?

    public Bitmap combineImages(Bitmap c, Bitmap s, String loc) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom
    Bitmap cs = null;

    int width, height = 0;

    if (c.getHeight() > s.getHeight()) {
        width = c.getWidth() + s.getWidth();
        height = c.getHeight();
    } else {
        width = c.getWidth() + s.getWidth();
        height = s.getHeight();
    }

    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);

    Canvas comboImage = new Canvas(cs);

    comboImage.drawBitmap(c, 0f, 0f, null);
    comboImage.drawBitmap(s, c.getHeight(), 0f, null);

    // this is an extra bit I added, just incase you want to save the new image somewhere and then return the location
    String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";

    OutputStream os = null;
    try {
        os = new FileOutputStream(loc + tmpImg);
        cs.compress(Bitmap.CompressFormat.PNG, 100, os);
    } catch (IOException e) {
        Log.e("combineImages", "problem combining images", e);
    }

    return cs;
}

Answers 1

  • I ended up finding a new piece of code here which saves them vertically-

        private Bitmap mergeMultiple(ArrayList<Bitmap> parts) {
    
        int w = 0, h = 0;
        for (int i = 0; i < parts.size(); i++) {
            if (i < parts.size() - 1) {
                w = parts.get(i).getWidth() > parts.get(i + 1).getWidth() ? parts.get(i).getWidth() : parts.get(i + 1).getWidth();
            }
            h += parts.get(i).getHeight();
        }
    
        Bitmap temp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(temp);
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        int top = 0;
        for (int i = 0; i < parts.size(); i++) {
    
            top = (i == 0 ? 0 : top + parts.get(i).getHeight() + 100);
            canvas.drawBitmap(parts.get(i), 0f, top,paint );
        }
        return temp;
    
    }
    

Related Articles