Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting a canvas into bitmap image in android

I am trying to develop an app on canvas,I am drawing a bitmap on the canvas. After drawing, I am trying to convert into bitmap image.

Can anyone give me a suggestion?

like image 935
satyam Avatar asked Oct 25 '10 10:10

satyam


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.

Is canvas a bitmap?

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). So, Canvas goes along with Bitmap.

What is bitmap image in Android?

A bitmap (or raster graphic) is a digital image composed of a matrix of dots. When viewed at 100%, each dot corresponds to an individual pixel on a display. In a standard bitmap image, each dot can be assigned a different color. In this instance we will simply create a Bitmap directly: Bitmap b = Bitmap.

How do you draw a bitmap?

To draw on a bitmap, use the image control's canvas and attach the mouse-event handlers to the appropriate events in the image control. Typically, you would use region operations (fills, rectangles, polylines, and so on). These are fast and efficient methods of drawing.


2 Answers

Advice depends upon what you are trying to do.

If you are concerned that your controls take a long time to draw, and you want to draw to a bitmap so you can blit the bitmap rather than re-drawing via a canvas, then you don't want to be double-guessing the platform - controls automatically cache their drawing to temporary bitmaps, and these can even be fetched from the control using getDrawingCache()

If you want to draw using a canvas to a bitmap, the usual recipe is:

  1. Create a bitmap of the correct size using Bitmap.createBitmap()
  2. Create a canvas instance pointing that this bitmap using Canvas(Bitmap) constructor
  3. Draw to the canvas
  4. Use the bitmap
like image 152
Will Avatar answered Oct 12 '22 10:10

Will


So you create a new Bitmap, for example:

Bitmap myBitmap = new Bitmap( (int)Width, (int)Height, Config.RGB_565 )

with width and height being the same as your canvas.

Next, use canvas.setBitmap(myBitmap), but not drawBitmap().

After you call setBitmap, all what you draw on canvas is in fact, drawing on your myBitmap going by the example code I have illustrated.

Edit:

You can not create a bitmap directly such as:

Bitmap myBitmap = new Bitmap( (int)Width, (int)Height, Config.RGB_565 ); 

You must use instead:

Bitmap myBitmap = Bitmap.createBitmap( (int)Width, (int)Height, Config.RGB_565 ); 
like image 43
i_Am Avatar answered Oct 12 '22 10:10

i_Am