如何垂直放置位图

时间:2021-03-24 22:23:12

标签: java android bitmap

我有 some code from user xil3 用于水平合并位图。有谁知道我怎样才能让它垂直做?

    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;
}

1 个答案:

答案 0 :(得分:0)

我最终找到了一段新的代码 here 可以垂直保存它们-

    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;

}