Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have three questions actually:

  1. Is it better to draw an image on a bitmap or create a bitmap as resource and then draw it over a bitmap? Performance wise, which one is better?
  2. If I want to draw something transparent over a bitmap, how would I go about doing it?
  3. If I want to overlay one transparent bitmap over another, how would I do it?

Sorry for the long list, but in the interest of learning, I would like to explore both the approaches.

like image 337
Legend Avatar asked Oct 08 '09 20:10

Legend


People also ask

How do you make a bitmap on canvas?

Use the Canvas method public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) . Set dst to the size of the rectangle you want the entire image to be scaled into. EDIT: Here's a possible implementation for drawing the bitmaps in squares across on the canvas.

What is bitmap and canvas in android?

6,0492 32 54. 1. 1. Bitmap are close to OS, it is an array of data describing pixels. Canvas is close to what human can see in life like a painting canvas where you can draw a circle or a point, but it doesn't save how this circle pixels will represent in memory (that does bitmap).


1 Answers

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;     } 
like image 124
Declan Shanaghy Avatar answered Sep 30 '22 15:09

Declan Shanaghy