Gman
Gman

Reputation: 2453

overlay canvas image with another image android

I have an image of a floor plan displayed on screen, My Question how do I overlay another image on that.

see the image from another thread where I asked a how to here

/**
 * floor plan drawing.
 *
 * @param canvas the canvas on which the background will be drawn
 */
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Bitmap temp = BitmapFactory.decodeResource(getResources(), R.drawable.floorplan);
    image= Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
    canvas.drawBitmap(image, 0, 0, null);

}

Upvotes: 1

Views: 4078

Answers (2)

Jorgesys
Jorgesys

Reputation: 126455

This is my method, to overlay two images into an ImageView:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Using this method:

  public static Bitmap overlayBitmap(Bitmap bitmapBackground, Bitmap bitmapImage) {

        int bitmap1Width = bitmapBackground.getWidth();
        int bitmap1Height = bitmapBackground.getHeight();
        int bitmap2Width = bitmapImage.getWidth();
        int bitmap2Height = bitmapImage.getHeight();

        float marginLeft = (float) (bitmap1Width * 0.5 - bitmap2Width * 0.5);
        float marginTop = (float) (bitmap1Height * 0.5 - bitmap2Height * 0.5);

        Bitmap overlayBitmap = Bitmap.createBitmap(bitmap1Width, bitmap1Height, bitmapBackground.getConfig());
        Canvas canvas = new Canvas(overlayBitmap);
        canvas.drawBitmap(bitmapBackground, new Matrix(), null);
        canvas.drawBitmap(bitmapImage, marginLeft, marginTop, null);

        return overlayBitmap;
    }

get the references and bitmaps to make de overlay!

   ImageView imageView = (ImageView) findViewById(R.id.imageView);

    Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.background);
    Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.androide);

    Bitmap bmpImages = overlayBitmap(background, image);
    imageView.setImageBitmap(bmpImages);

to have this as a result:

enter image description here

Download the complete sample.

Upvotes: 0

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

What about adding the second image over the first one?

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
    Bitmap temp = BitmapFactory.decodeResource(getResources(), R.drawable.floorplan);
    image = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
    canvas.drawBitmap(image, 0, 0, null);

    Bitmap over = BitmapFactory.decodeResource(getResources(), R.drawable.overlay);
    image = Bitmap.createScaledBitmap(over, canvas.getWidth(), canvas.getHeight(), true);
    canvas.drawBitmap(image, 0, 0, null);
}

Upvotes: 1

Related Questions