1

I know this question was already answered but It was not helped me. My question is not to overlap the Images I want to join two seperate Images which are in the same size like which is shown below.

![I want like below image][1]

Here is the code: Used to combine Images

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

I am getting ovelapping image. Can anyone help.

Thanks.

KCN
  • 472
  • 6
  • 19

2 Answers2

2

If you want to create a side-by-side merged image you will need to create a result bitmap with 2 times the width of first image, or, more scalably, the sum of the widths of the images:

Currently, you are creating a result image with width firstImage.getWidth(). They will clearly overlap or be off the canvas.

Also, you will need to place the second image at x == firstImage.getWidth()

Check out this code (untested):

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);
caulitomaz
  • 2,141
  • 14
  • 20
  • this code is working fine but I want to do up and right combine can anyone help me please? – Abhinav Gupta Aug 31 '18 at 07:34
  • 1
    @AbhinavGupta just replace `firstImage.getWidth() + secondImage.getWidth()` with `firstImage.getWidth() `. Then replace `canvas.drawBitmap(secondImage, firstImage.getWidth(), 0f, null);` with `canvas.drawBitmap(secondImage, 0f, 0f, null);`. Finally experiment with the coordinates 0f, 0f. Since you want it on the right, your "left" offset will be `firstImage.getWidth() - secondImage.getWidth()` only 2 years wait for my help :-) – Someone Somewhere Jul 01 '20 at 01:54
0

I used this solution from 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;
}
AKHIL V S
  • 64
  • 6