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?
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.
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.
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.
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.
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:
Bitmap.createBitmap()
Canvas(Bitmap)
constructorSo 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 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With