I have an ImageView loaded with bitmap. When user touches on the bitmap, I draw a circle using canvas at the touched position on the bitmap. Now, I need to undo the drawn circle. Everywhere, I find snippets to undo paths drawn and not a circle directly. Does anyone have a solution to undo the previously drawn circle ?
There are two ways to erase the previous drawings
If you want to continue with the bitmap just assign null to the bitmap object when you want to remove the drawings in the bitmap. When you assign null the entire bitmap will cleared.
bitmapobj = null;
Another method is using the canvas for drawing, then each time you call the invalidate() the entire canvas will redraw and erase the previous drawing.
(Invalidate() is the default method that call the onDraw() which draws the shapes).
Related
I need to save a number of formatted circles to a Bitmap whenever a button is pushed, and then draw those circles to the canvas. I was looking around, but had a great deal of trouble finding resources on how to save a shape I had created that was not an already created resource file. Any advice on how to go about this would be appreciated.
Say you are in your custom view, but it should work also with a OnTouchListener in any view.
Create a mutable bitmap with Bitmap.createBitmap() with the same width and height of your view
use your logic for getting information of the circles, for example you can use onTouchEvent() to get the coordinates of you finger(s)
with this information you can use Bitmap.setPixel() to save it to the bitmap
optional, onDraw() you can draw your bitmap with Canvas.drawBitmap()
In case you need to draw shapes to the bitmap you can create a new Canvas(bitmap) and draw on this canvas: the pixels will be set on the bitmap.
I'm using a SurfaceView and canvas. Over time The canvas draws the same bitmap image but at different position. So every time the position of the bitmap moves, any drawings on the bitmap has to be be moved as well. So is there a way to just have the drawings on the bitmap image stay so that when the image moves(when canvas redraws the bitmap at a different location on screen), any drawings on the image moves with it as well?
You can setup a Bitmap to be the backing buffer for your canvas so that drawing to the canvas is, effective, drawing to that Bitmap. This way you can keep it around and do whatever you want with it.
Is there a way to do it? I d like to save the canvas screen as a Bitmap object, and then draw it again, to the screen.
You can construct a Canvas passing in a Bitmap onto which the canvas will draw. See http://developer.android.com/reference/android/graphics/Canvas.html#Canvas(android.graphics.Bitmap)
You need to construct a Bitmap appropriately sized to the view into which you want to draw.
Just guessing here, but if you are doing this because you want to draw off screen then draw the bitmap on all at once you might also look at SurfaceHolder/SurfaceView.
I have an ImageView from which I want to copy a piece of it in the shape of a circle that I can then re-display to the user at a larger than original size to simulate a zoom effect. Would I use bitmaps for that? Thanks.
Yes.... you can try to get a Bitmap that represents the ImageView you are working with. Before, you have to enable the caching (by invoking the setDrawingCacheEnabled(true) method). To get the Bitmap representation of the image, you can use the getDrawingCache() method.
Once you get that Bitmap, you can resize it or make any other changes you want. Then, you can create a Rect object with the portion of the bitmap you want to draw, then you draw that rect using this method: drawBitmap(Bitmap, Rect origin, Rect destination, Paint).
I'm used to handle graphics with old-school libraries (allegro, GD, pygame), where if I want to copy a part of a bitmap into another... I just use blit.
I'm trying to figure out how to do that in android, and I got very confused.
So... we have these Canvas that are write-only, and Bitmaps that are read-only? It seems too stupid to be real, there must be something I'm missing, but I really can't figure it out.
edit: to be more precise... if bitmaps are read only, and canvas are write only, I can't blit A into B, and then B into C?
The code to copy one bitmap into another is like this:
Rect src = new Rect(0, 0, 50, 50);
Rect dst = new Rect(50, 50, 200, 200);
canvas.drawBitmap(originalBitmap, src, dst, null);
That specifies that you want to copy the top left corner (50x50) of a bitmap, and then stretch that into a 150x150 Bitmap and write it 50px offset from the top left corner of your canvas.
You can trigger drawing via invalidate() but I recommend using a SurfaceView if you're doing animation. The problem with invalidate is that it only draws once the thread goes idle, so you can't use it in a loop - it would only draw the last frame. Here are some links to other questions I've answered about graphics, they might be of use to explain what I mean.
How to draw a rectangle (empty or filled, and a few other options)
How to create a custom SurfaceView for animation
Links to the code for an app with randomly bouncing balls on the screen, also including touch control
Some more info about SurfaceView versus Invalidate()
Some difficulties with manually rotating things
In response to the comments, here is more information:
If you get the Canvas from a SurfaceHolder.lockCanvas() then I don't think you can copy the residual data that was in it into a Bitmap. But that's not what that control is for - you only use than when you've sorted everything out and you're ready to draw.
What you want to do is create a canvas that draws into a bitmap using
Canvas canvas = new Canvas(yourBitmap)
You can then do whatever transformations and drawing ops you want. yourBitmap will contain all the newest information. Then you use the surface holder like so:
Canvas someOtherCanvas = surfaceHolder.lockCanvas()
someOtherCanvas.drawBitmap(yourBitmap, ....)
That way you've always got yourBitmap which has whatever information in it you're trying to preserve.
In android you draw to the canvas, and when you want it to update you call invalidate which will the redraw this canvas to the screen. So I'm guessing you have overridden the onDraw method of your view so just add invalidate();
#Override
public void onDraw(Canvas canvas) {
// Draw a bitmap to the canvas at 0,0
canvas.drawBitmap(mBitmap, 0, 0, null);
// Add in your drawing functions here
super.onDraw(canvas);
// Call invalidate to draw to screen
invalidate();
}
The above code simply redraws the bitmap constantly, of course you want to add in extra thing to draw and consider using a timing function that calls invalidate so that it is not constantly running. I'd advice having a look at the lunarlander sources.