Android: How to Overlay a Bitmap and Draw Over a Bitmap

Android: How to overlay a bitmap and draw over a bitmap?

I can't believe no one has answered this yet! A rare occurrence on SO!

1

The question doesn't quite make sense to me. But I'll give it a stab.
If you're asking about direct drawing to a canvas (polygons, shading, text etc...) vs. loading a bitmap and blitting it onto the canvas that would depend on the complexity of your drawing.
As the drawing gets more complex the CPU time required will increase accordingly.
However, blitting a bitmap onto a canvas will always be a constant time which is proportional to the size of the bitmap.

2

Without knowing what "something" is how can I show you how to do it?
You should be able to figure out #2 from the answer for #3.

3

Assumptions:

  • bmp1 is larger than bmp2.
  • You want them both overlaid from the top left corner.

        private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    canvas.drawBitmap(bmp2, new Matrix(), null);
    return bmOverlay;
    }

How to overlay a bitmap over another

The most easier way to do that without go deeper with graphics libs is using FrameLayout, with this layout you can place one view above the other in order as they are added to the layout, example:

<FrameLayout>
<ImageView android:id="@+id/imageView1" />
<ImageView android:id="@+id/imageView2" />
</FrameLayout>

In example above imageView2 will overlap imageView1, this is so far the fastest way to overlay one image with another. This approach allows to place any descendant of View above another one.

How to overlay bitmap over another bitmap at particular XY position

Use below code

private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, x,y, null);
return bmOverlay;
}

Where x and y are actual positions where you have to draw the overlay bitmap.

Overlay Bitmap over another Android

thanks man i figured it out by own. using this

void hm1(){
Bitmap border = BitmapFactory.decodeResource(getResources(), R.drawable.vignette2);
int width = bmp.getWidth();
int height = bmp.getHeight();
change = Bitmap.createScaledBitmap(change, width, height, false);
Canvas canvas = new Canvas(change);
Bitmap scaledBorder = Bitmap.createScaledBitmap(border,width,height, false);
canvas.drawBitmap(scaledBorder, 0, 0,null);
//canvas.drawBitmap(k, 0, 0, null);
view.setImageBitmap(change);
}

by adding this method on any click button , menu etc you can draw two bitmaps over each other.

P.S : Bitmap change is another bitmap from the original one as i don't want the user to apply the Overlay on the original method but on the changed one.
hope the answer helps someone. thanks

How to draw an image on top a bitmap and combine as one bitmap in Android Java

You can do it like this.

1, read or create your bitmap:

Bitmap rectBitmap = BitmapFactory.decodeStream(istr);
Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

2, create a canvas on the bitmap:

Canvas canvas = new Canvas(bitmap);

3, draw something:

canvas.drawColor(Color.RED)   
canvas.drawRect / canvas.drawLine / canvas.drawArc ...
//for triangle shape you can use drawPath

4, save the bitmap:

bitmap.compress(CompressFormat format, int quality, OutputStream stream)


Related Topics



Leave a reply



Submit