Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge bitmaps in android? [closed]

I was wondering how can I merge couple of bitmaps in Android to create something like this:

enter image description here

or something like this: enter image description here

I suppose that is done by merging two bitmaps, but how do I center the inner bitmap like this?

like image 687
dmacan23 Avatar asked Aug 04 '15 15:08

dmacan23


People also ask

What is the use of bitmap in Android?

A bitmap is simply a rectangle of pixels. Each pixel can be set to a given color but exactly what color depends on the type of the pixel. The first two parameters give the width and the height in pixels. The third parameter specifies the type of pixel you want to use.

What is bitmap recycle?

The recycle() method allows an app to reclaim memory as soon as possible. Caution: You should use recycle() only when you are sure that the bitmap is no longer being used. If you call recycle() and later attempt to draw the bitmap, you will get the error: "Canvas: trying to use a recycled bitmap" .


2 Answers

I've figured it out:

public static Bitmap mergeToPin(Bitmap back, Bitmap front) {
    Bitmap result = Bitmap.createBitmap(back.getWidth(), back.getHeight(), back.getConfig());
    Canvas canvas = new Canvas(result);
    int widthBack = back.getWidth();
    int widthFront = front.getWidth();
    float move = (widthBack - widthFront) / 2;
    canvas.drawBitmap(back, 0f, 0f, null);
    canvas.drawBitmap(front, move, move, null);
    return result;
}

The trick that front image has equal height and width, and that that height/width matches the width of the back image (or scaled - I've resized it to 90% of the original pin width).

It doesn't matter if you put circled or squared pin, as long as the position for image (circle or square) has equal width and height. Of course, you need to create circular bitmap if you wish to add it to the circled pin :)

like image 180
dmacan23 Avatar answered Oct 14 '22 00:10

dmacan23


Try this function.

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, 10, 10, null);
return result;
}

Hope this helps.

like image 41
ashwani Avatar answered Oct 14 '22 00:10

ashwani