Android 合并两个位图图像(并排)

Android 合并两个位图图像(并排),android,Android,有人能帮助将两个位图图像合并成一个位图吗 在android中(并排) 谢谢, Yuvaraj您可以使用Canvas-查看本文: 更新了并行执行的代码: public Bitmap combineImages(Bitmap c, Bitmap s) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom Bi

有人能帮助将两个位图图像合并成一个位图吗

在android中(并排)

谢谢,
Yuvaraj

您可以使用
Canvas
-查看本文:

更新了并行执行的代码:

public Bitmap combineImages(Bitmap c, Bitmap s) { // 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.getWidth() > s.getWidth()) { 
      width = c.getWidth() + s.getWidth(); 
      height = c.getHeight(); 
    } else { 
      width = s.getWidth() + s.getWidth(); 
      height = c.getHeight(); 
    } 

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

    Canvas comboImage = new Canvas(cs); 

    comboImage.drawBitmap(c, 0f, 0f, null); 
    comboImage.drawBitmap(s, c.getWidth(), 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(CompressFormat.PNG, 100, os); 
    } catch(IOException e) { 
      Log.e("combineImages", "problem combining images", e); 
    }*/ 

    return cs; 
  } 

出色的工作完成了选定的答案。 如果要使用位图数组列表并并排执行此操作,请查看以下内容:

private Bitmap combineImageIntoOneFlexWidth(ArrayList<Bitmap> bitmap) {
        int w = 0, h = 0;
        for (int i = 0; i < bitmap.size(); i++) {
            if (i < bitmap.size() - 1) {
                h = bitmap.get(i).getHeight() > bitmap.get(i + 1).getHeight() ? bitmap.get(i).getHeight() : bitmap.get(i + 1).getHeight();
            }
            w += bitmap.get(i).getWidth();
        }

        Bitmap temp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(temp);
        int top = 0;
        for (int i = 0; i < bitmap.size(); i++) {
            Log.e("HTML", "Combine: " + i + "/" + bitmap.size() + 1);

            top = (i == 0 ? 0 : top + bitmap.get(i).getWidth());
            //attributes 1:bitmap,2:width that starts drawing,3:height that starts drawing
            canvas.drawBitmap(bitmap.get(i), top, 0f, null);
        }
        return temp;
    }
专用位图组合图像InTooneFlexWidth(ArrayList位图){
int w=0,h=0;
对于(int i=0;ibitmap.get(i+1).getHeight()?bitmap.get(i).getHeight():bitmap.get(i+1).getHeight();
}
w+=bitmap.get(i).getWidth();
}
Bitmap temp=Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
画布=新画布(临时);
int-top=0;
对于(int i=0;i
我最终将xil3的答案修改为一个kotlin扩展函数,也许它对某些人有用。在我的例子中,我希望图像垂直堆叠

fun Bitmap?.combine(b: Bitmap?): Bitmap? =
when {
    b == null || this == null -> {
        this ?: b
    } else -> {
        val cs = Bitmap.createBitmap(
            max(this.width, b.width),
            this.height + b.height,
            Bitmap.Config.ARGB_8888
        )
        val canvas = Canvas(cs)
        canvas.drawBitmap(this, 0f, 0f, null)
        canvas.drawBitmap(b, 0f, this.height.toFloat(), null)
        cs
    }
}

谢谢你。我试过了,但它会将两个图像一个接一个地组合在一起。但是我需要并排合并两个图像。我用代码更新了答案,应该可以并排工作。只需对原始图像进行一些小的修改。有人可以上传图像合并的完整源代码吗?我急需..请任何人帮助提前感谢。如何将第二个图像放在第一个图像的右上角?这段代码将第二张图片放在第一张图片的右边。根据我被拒绝的编辑,这里的尺寸计算有点偏离。将图像并排放置时,宽度始终为c+s,从c和s中取较高者作为高度。下面是该部分的正确代码:if(c.getHeight()>s.getHeight()){width=c.getWidth()+s.getWidth();height=c.getHeight();}否则{width=c.getWidth()+s.getWidth();height=s.getHeight();}