将两个图像合并为一个图像

时间:2016-01-11 11:09:03

标签: android image

我知道这个问题已经得到解答,但对我没有帮助。我的问题是不要重叠图像我要加入两个单独的图像,这些图像的大小相同,如下所示。

![我想要下面的图片] [1]

以下是代码:用于合并图片

private Bitmap createSingleImageFromMultipleImages(Bitmap firstImage, Bitmap secondImage) {

        Bitmap result = Bitmap.createBitmap(firstImage.getWidth(), firstImage.getHeight(), firstImage.getConfig());
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(firstImage, 0f, 0f, null);
        canvas.drawBitmap(secondImage, 200, 200, null);
        return result;
    }

 Bitmap mergedImages = createSingleImageFromMultipleImages(firstImage, SecondImage);

                    im.setImageBitmap(mergedImages);

我正在获得超越图像。任何人都可以提供帮助。

感谢。

2 个答案:

答案 0 :(得分:0)

如果要创建并排的合并图像,则需要创建一个结果位图,其宽度是第一个图像的2倍,或者更可能是图像宽度的总和:

目前,您正在创建宽度为firstImage.getWidth()的结果图片。它们将明显重叠或脱离画布。

此外,您需要将第二张图片放在 x == firstImage.getWidth()

查看此代码(未经测试):

private Bitmap createSingleImageFromMultipleImages(Bitmap firstImage, Bitmap secondImage) {
        Bitmap result = Bitmap.createBitmap(firstImage.getWidth() + secondImage.getWidth(), firstImage.getHeight(), firstImage.getConfig());
        Canvas canvas = new Canvas(result);
        canvas.drawBitmap(firstImage, 0f, 0f, null);
        canvas.drawBitmap(secondImage, firstImage.getWidth(), 0f, null);
        return result;
}

Bitmap mergedImages = createSingleImageFromMultipleImages(firstImage, secondImage);

im.setImageBitmap(mergedImages);

答案 1 :(得分:0)

我使用了https://stackoverflow.com/a/25569617/12074613

的解决方案
private Bitmap getBitmap(View v) {
v.clearFocus();
v.setPressed(false);

boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);

// Reset the drawing cache background color to fully 
transparent
// for the duration of this operation
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);

if (color != 0) {
    v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
    Toast.makeText(StopWarApp.getContext(), 
"Something went wrong",
            Toast.LENGTH_SHORT).show();
    return null;
}

Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);

return bitmap;
}
相关问题