How to erase the drawing on image - android

I am trying to draw on image in android. I found one good example from here.
Here they explained how to draw in the plain canvas. I tried to add image in that canvas.
canvasBitmap = BitmapFactory.decodeResource(res, R.drawable.image)
.copy(Bitmap.Config.ARGB_8888, true);
canvasBitmap = Bitmap.createScaledBitmap(canvasBitmap, this.getWidth(),
this.getHeight(), true);
Now I am facing one issue. When I try to erase the drawings it erase the original image also.
I tried lots of solution. But nothing helps me. Please let me know how to erase the drawings in the image without erase the Image.

A simple solution to this is to just draw the image back onto Canvas. Over everything else.
This would leave you with just the origional image -> Drawings removed.

Related

Use an image as the background of a canvas and keep it resized

I'm currently working on an Android app. I have a canvas onto which the user will draw something. I need to have a picture as the background of this canvas.
I already managed to import a pic and set it as the background using
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_background_pic));
However, the picture appears ten times bigger than the screen.
Is it possible to keep the picture resized, so it fills the canvas and not more?
Also, later I will need to add other things to the activity. The canvas will not be the only thing on the screen, so I need the picture to be the size of the canvas, and not the screen.
Thanks a lot.
Nevermind I found it:
canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvasWidth, canvasHeight), null);
By Kai at stackoverflow.com/a/27466127/8205282

Android get rotated bitmap. (Image straightening)

I am trying to rotate a bitmap and crop out a valid Rectangle out of it, and then save it on the disk. What i am trying to do is explained in the following link:
http://i.stack.imgur.com/IIhBw.png
Black is the original image. Red is rotated image (by 15 degrees here) and green is the valid portion of the red image.
This feature is similar to the Image straightening which Instagram app does.
So far, I have tried this:
public static Bitmap getRotatedBitmap2(Bitmap bmp, int rotation){
Matrix matrix = new Matrix();
matrix.preRotate(rotation);
Bitmap bitmap = Bitmap.createBitmap(bmp, 0, 0,
bmp.getWidth(), bmp.getHeight(),
matrix, false);
return bitmap;
}
This rotates the bitmap but it shrinks in size, also the rotated bitmap is enclosed is an black rectangle. i need to get rid of the out-bounding black rectangle. Also, How can I obtain the valid portion of bitmap?
Aviary and ImageMagick are libraries that can help to achieve the results asked in the question, as well perform other Bitmap operations and Image Manipulations.
I know you already solved this question, but if somebody is looking for another solution, you can check this library image-rotator that solves problem like this or very similar.

Save image on Canvas to bitmap

How can I save the image on a canvas to bitmap?
There are several related posts, but I didn't find the one works for me. To make it clear, here is my problem
What's on my canvas:
An bitmap image which was drawn to the canvas using canvas.drawBitmap(oldBitmap, x, y, null);
Some paths drawn overlay to this image on the same canvas using canvas.drawPath.
Both the image and the paths are draw on a customized SurfaceView. And both they are draw to the canvas in the onDraw function. Or more specific, the implementation is in the following link:
Android: custom draw on an given image (with SurfaceView)
Now I need to save the image on canvas (the oldBitmap and paths I drew) to a newBitmap. Could someone tell me how to do that?

How to create a transparent bitmap / drawable in android using drawing lines and circles on a canvas

It seems to be a very trivial thing, but I am not able to find a sample code for it.
I am trying to do a very simple thing. On a canvas I plan to draw a round rect and save that in a drawable which I can then set as a background of a button.
Now I am looking for 2 things, how to draw and create a round rect so that the transparency effect can be created (like in a png drawable)
Second how to save the canvas to an image.
Please help
Please try to use this code.
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.color.transparant);
canvas.drawBitmap(bitmap, matrix, paint);

How to blit() in android?

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.

Categories

Resources