Can we update the part of canvas without updating entire canvas? - android

I am extending the ImageView to display the images. The input stream for images is getting from socket along with their top left coordinates.
I am able to display the part of image,where as the remaining screen is white.My problem is total canvas is overridden with the current bitmap and the previous drawings are erased with white screen.
I searched for the solution,I come across to known the information about canvas is "Every pixel of canvas is drawn when canvas.drawBitmap(bitmap,top,left,paint) is called".
I need to update the only dirty part of image(canvas) without updating the entire canvas.
Please suggest me How to update the dirty part of screen(canvas) and previous drawings should not be erased.
Thanks & Regards
Mini.

You can make a canvas from what you already have instead creating a blank on and then draw on it. This is an example of how to achieve it.
yourImageView.setDrawingCacheEnabled(true);
Bitmap bm = yourImageView.getDrawingCache();
final Canvas canvas = new Canvas(bm);
Then you have a canvas, which contains whatever was previously in ImageView and you can update whatever part you want. For that I would recommend having a look at PorterDuff modes.

Related

Drawing to a new Bitmap and then using that in my onDraw

I would like to display several rings with different coloured sections (see below). The colour of the sections, however, cannot be know in advance, so I will need to draw these dynamically.
I know I could draw directly to the canvas but, once I have them, I would like to animate these rings, rotate them, have them overlap etc. It seemed, therefore, that the easiest and possibly least expensive approach would be to create them in advance, in memory, as transparent pngs and then just draw them in onDraw.
My problem is the only methods I can find to do this are setPixel. Is there not a way I could use drawing tools, like in Canvas, to draw to an empty bitmap, once, then use that bitmap with my canvas in onDraw?
I feel like I am missing a piece in the puzzle. Any help would be greatly appreciated.
You can create a Bitmap that is the size you want the ring to be and then create a Canvas the same size. Call setBitmap() on the Canvas and it will draw on to that for you. Then you can build your circle and have a bitmap to hold onto and use just like any other resource.

How can I erase the canvas content?

I´m trying to let the background transparent in a sequence of frames painted in Canvas in Android 2.2.
But it seems that the background does not erase the old frame when a new frame is painted.
How can I erase it?
How do I clear (or redraw) the WHOLE canvas for a new layout (= try at the game) ?
Just call Canvas.drawColor(Color.BLACK), or whatever color you want to clear your Canvas with.
And: how can I update just a part of the screen ?
There is no such method that just update a "part of the screen" since Android OS is redrawing every pixel when updating the screen. But, when you're not clearing old drawings on your Canvas, the old drawings are still on the surface and that is probably one way to "update just a part" of the screen.
So, if you want to "update a part of the screen", just avoid calling Canvas.drawColor() method.
u can't erase a canvas. you'll have to draw a color on it each time before a frame.
Canvas.drawColor(Color.BLACK);
//draw frame
it takes a little practice but youll get the trick ecantually
I use path.reset() it working for me, clean all the screen.

reseting the canvas bitmap doesn't work correctly

i try to paint an arrow png on canvas with a png bitmap behind it. so every time i rotate the arrow i have to reset the background bitmap of the canvas becouse otherwise i would see duplicates of the old arrows.
i reset the background with:
bMapLoad.eraseColor(0);
the problem is that sometimes it erases parts of the new arrow. sow that i sometimes only see a part of the arrow. i rotate it aber every 10-100 ms.
can someone help me?
i also tried:
bMapLoad=bMapcanvasBack.copy(bMapcanvasBack.getConfig(), true);
canvasLoad.setBitmap(bMapLoad);
this works fine but needs more memory. so is there a better way?
edit:
also
canvasLoad.drawColor(0, PorterDuff.Mode.CLEAR);
don't work
Try canvas.drawARGB(0,0,0,0) or, more concise canvas.drawColor(0) to clear it, then draw the bitmap again (canvas.drawBitmap(...))
The partial overdrawing may have a different reason though - synchronization between drawing and display. A common practice is to use double buffering. This means, you would draw on a offscreen canvas, and then exchange the underlying Bitmap with the visible canvas.
EDIT: some useful resources on double buffering with android:
An example here plus the book "beginning android games" also features a section on double buffering. The source code for the book can be downloaded here

Canvas Dynamically change a bitmap's z-index

I am creating an Android app and in my app I have a canvas which I draw numerous bitmaps to the canvas via the canvas.drawBitmap () function. From my understanding the z-index on these bitmaps are set based on the order in which they are being drawn to the canvas. What I am trying to figure out is after drawing these bitmaps if I can dynamically change the z-index on a bitmap to push it to the top? This seems like a very simple problem, but I have had not luck in finding a solution yet.
Not really possible: after you call drawBitmap the contents of the bitmap are rendered onto the canvas, but the canvas does not store any references to the original bitmap, it only stores the results of applying the bitmap's contents to the canvas. There is no way to dynamically say that bitmap you drew 1st out of 50, I want you to make that the 50th bitmap and automatically redraw every single other bitmap to reflect the change.
So you'll need to order your drawing operations before hand.

Application gets too slow while drawing two bitmaps on a Canvas !

I have two bitmap overlapped .The top bitmap is transparent and when user touch to screen i copy pixels from another bitmap to top bitmap.My goal is to give to users the feeling of erasing image with touching to see another image.However it is not working properly especially when user drags his finger too fast on the screen.I made a few tests and i beleive drawing bitmaps to the canvas every time cause the lag but i don't know how to fix it.
It looks like a Canvas or Bitmap isn't redrawn until you wipe it yourself. That means you just have change the alpha value of the pixels in the top Canvas/bitmap/thingy that are being "painted" rather than redrawing the entire canvas for every update.

Categories

Resources