Bosah Chude
Bosah Chude

Reputation: 3810

Overlay images in Android

I have two images that I want to merge into one. (Eg "House.png" on top of "street.png")

How do i achieve this in Android? I just want to merge the images and export them to a file.

This example Sets the image to an ImageView but i wish to export it.
This other example does not work in Android since the classes are not available.

Upvotes: 1

Views: 3784

Answers (2)

Singhak
Singhak

Reputation: 8896

You can do like this...............

public Bitmap Overlay(Bitmap Bitmap1, Resources paramResources, Bitmap Bitmap2, int alpha)
    {
      Bitmap bmp1 = Bitmap.createScaledBitmap(Bitmap2, Bitmap1.getWidth(), Bitmap1.getHeight(), true);
      Bitmap bmp2 = Bitmap.createBitmap(Bitmap1.getWidth(), Bitmap1.getHeight(), Bitmap1.getConfig());
      Paint localPaint = new Paint();
      localPaint.setAlpha(alpha);
      Canvas localCanvas = new Canvas(bmp2);
      Matrix localMatrix = new Matrix();
      localCanvas.drawBitmap(Bitmap1, localMatrix, null);
      localCanvas.drawBitmap(bmp1, localMatrix, localPaint);
      bmp1.recycle();
      System.gc();
      return bmp2;
    }

Upvotes: 1

Alexandru Cristescu
Alexandru Cristescu

Reputation: 3948

I'd try something like:

public static Bitmap mergeImages(Bitmap bottomImage, Bitmap topImage) {
    final Bitmap output = Bitmap.createBitmap(bottomImage.getWidth(), bottomImage
            .getHeight(), Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    paint.setAntiAlias(true);

    canvas.drawBitmap(bottomImage, 0, 0, paint);
    canvas.drawBitmap(topImage, 0, 0, paint);

    return output;
}

(not tested, I just wrote it here, might be some simple errors in there)

Basically what you do is create a 3rd empty bitmap, draw the bottom image on it and then draw the top image over it.

As for saving to a file, here are a few examples: Save bitmap to location

Upvotes: 3

Related Questions