Android creating a circle bitmap without drawing it on canvas - android

My question is: is there any way to create a circle and then make it bitmap and not to display it on canvas??For example make a black circle then convert it to a bitmap but not using the canvas.setBitmap(Bitmap bitmap).Thanks in advance.

You can easily get a bitmap object using Bitmap static method (Bitmap.create) and then use that object to draw anything on it by instantiating a canvas object with bitmapObject.
your code should look something like this:
Bitmap bitmap = Bitmap.create(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
//draw on your canvas.

Related

Canvas to Bitmap

My program is a simple flood-fill game application for android, namely, the user can paint and draw something on the canvas. Now I want to provide a sharing option to the users. I guess that I must begin with copying the canvas to a bitmap object. I could not find a satisfactory answer because it is generally suggested "to create a new canvas, then..." but I got a canvas like that,
Canvas canvas = holder.lockCanvas();
then I use it. So, how can I copy my current canvas to a bitmap object?
Thanks
As suggested Maulik.J, I looked at converting a canvas into bitmap image in android again.
I could not understand from a bit close expression of this link. But, then I saw below text taken from http://developer.android.com/guide/topics/graphics/2d-graphics.html#draw-with-canvas . So, I solved this problem with the help of Maulik.J and the following text:
The Bitmap is always required for a Canvas. You can set up a new Canvas like this:
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
Now your Canvas will draw onto the defined Bitmap. After drawing upon it with the Canvas, you can then carry your Bitmap to another Canvas with one of the Canvas.drawBitmap(Bitmap,...) methods. It's recommended that you ultimately draw your final graphics through a Canvas offered to you by View.onDraw() or SurfaceHolder.lockCanvas()
I created a second Canvas object having a bitmap which has same dimensions with my real canvas, I drew all my illustrations on this second temporary canvas. Then, I drew its bitmap on my real canvas.
Thanks.

How to copy one canvas into another canvas?

I have drawn some rects into a canvas and I want to copy those rects into another canvas.The commonly discussed solution of using bitmaps will not work because I didn't use a bitmap in this canvas, no images only those rects.
How do I do that?
Have you tried saving the attributes given to the rectangles upon drawing to the first canvas, and then redrawing them on the second canvas based on these attributes? Perhaps you could try creating a Rect-object for the first canvas and passing it to Canvas.drawRect() on the second canvas?
You can create a bitmap using a canvas and you draw on that bitmap.
bitmap = Bitmap.createBitmap(50,50,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// drawn some rects into canvas
Now you can use the bitmap to draw on any canvas you want;
Canvas canvas1 = new Canvas(bitmap);
Canvas canvas2 = new Canvas(bitmap);
Or if you have a canvas already:
canvas.drawBitmap(bitmap, 0, 0, paint);
This way you draw rects only once and not on each canvas.

create a bitmap with a bitmap inside

I know - the title may sounds strange. Let me explain:
I created an image to show you, what I'm talking about:
I got an image (Bitmap (1)), that as the size of 150w/200h.
Now I need to make the bitmap bigger ((2) 400w/400h), but the original image must have the same size. So that the image is embedded in white background.
I think one way to solve it is this:
* create a big bitmap
* create a canvas for it
* draw the original bitmap on the canvas
* draw the canvas
* generate a bitmap of the canvas
The problem for me is, that it must be done in a background thread without drawing a view.
I hope you understand me.
You can use the code bellow to achive it. Where smallBitmap is your original image and bigBitmap is the final image:
Bitmap bigBitmap = Bitmap.createBitmap(width, height , Bitmap.Config.ARGB_8888);
canvas = new Canvas(bigBitmap);
canvas.drawBitmap(smallBitmap, left, top, new Paint());
Regards.
This should do the trick.
Create a thread and in that thread object:
Create a new bitmap.
Create a canvas based on that bitmap.
Draw your bitmap to that canvas
and voila!
I hope this helps.

can an image be manipulated using canvas

Is it possible to manipulate images using canvas? How do we get the image onto the canvas?
#Override
protected void onDraw(Canvas canvas) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bitmap mBitmap = bitmap.copy(bitmap.getConfig(), true);
canvas = new Canvas(mBitmap);
Matrix matrix = new Matrix();
canvas.drawBitmap(mBitmap, matrix, mPaint);
}
I'm unable to see the image on the screen. canvas.drawBitmap() shouldn't be necessary since I'm using the constructor and passing mBitmap.
Yes you can, but you don't "get the image onto the Canvas," a Canvas is just an interface to draw onto a Bitmap. To do so, simply create a Canvas and give a reference to your mutable Bitmap:
Canvas c = new Canvas(myBitmap);
Very easy :)
You shouldn't do this in your onDraw method. Try to create an ImageView that should display the image and then set the bitmap via setImageBitmap(bitmap).
In general you are doing somethings wrong in your code.
Loading an image every time something should be drawn to the screen will slow down your application a lot. Try to load the image only once and then just edit and set it to the imageview.
You are assigning a new Canvas to the parameter you got. But this won't change anything on the screen. Think of it like somebody puts a paper in front of you and says:"Please write your message on the paper". Now you take another blank paper and write on it. But the other person only knows about his paper and will use this paper to read your message.

how to convert a paint image to bitmap

I am able to save the source image but not able save the image with colorfilter:
paint.setColorFilter(new ColorMatrixColorFilter(cm));
If this image is converted into bitmap it can be saved easily,
but I don't know how to do this. Is there anyone to give solution?
Have your original bitmap.
Create a new clean Bitmap that has the same width/height as your original bitmap.
Create a new Canvas using this clean Bitmap.
Set your paint object, etc for this new Canvas.
Draw your original bitmap into this new Canvas.
Since this new Canvas is backed by a Bitmap (point 3.), any drawing you do in this Canvas will become part of the new Bitmap (point 2.). Now just call 'compress' on this Bitmap from point 2. and the bitmap will be saved as a jpg/png.

Categories

Resources