i am having an image in canvas.i want to get color at particular pixels and replace with user defined color.is it possible to do for abitmap placed in android canvas?
Thanks in advance..
Unfortunately Canvas is more like a write-only object: you can draw on the Bitmap, but you can't really get almost any information on the Bitmap. But if you have access to the Bitmap itself, this is a very easy task: just use Bitmap.getPixel(int x, int y) to get the color of the pixels and then draw whatever you need on the Canvas or even just call Bitmap.setPixel(int x, int y, int color) without creating any Canvas at all.
Sure. just bitmap.getPixel(x, y) and setPixel(x, y, color). Make sure that the bitmap your working on is mutable.
Image in Canvas with touch events
Related
I want to know if there is a way to check if the user touched the bitmap, ofcourse im talking about an image without background, like a circle, a triangle and so on.
Thanks!
image without background, like a circle, a triangle and so on
then you should probably override View's onTouch, retrieve x and y from the touch event you got as parameter and compare it with the coords of the shape you drawn. The easiest way is to use Rect.contains(int x, int y)
I would like to draw text on a canvas using the Canvas.drawText call for a game application, and later be able to scale it as if it was a bitmap object. I do not want to dynamically change its font size, but I want to scale it applying an affine transform.
Is it possible? I cannot find an adequate API call in the documentation.
Thank you!
Using Matrix you can scale it and that apply the matrix tranformation to the canvas with Canvas.concat(Matrix m) . Do this before drawing the text onto the canvas.
Once it's on the Canvas, it's not a separate object that you can do anything to. What you can do is use the Canvas as an API for drawing to a bitmap:
Create a Bitmap.
Create a Canvas backed by that Bitmap.
Draw your text into the Canvas.
Hang on to your Bitmap and do whatever you want with it.
Normally, when you draw a bitmap to the canvas, it is rectangular. Is it possible to make it appear as a parallelogram instead?
Try canvas.skew(20, 0). That skews the drawing matrix in x, but you can obviously do y as well, depending on what you need.
I have several bitmaps (game sprites) which I'd like to draw into another bitmap, however each non-transparent pixel of the source bitmap should be drawn using a single color, ignoring the original pixel color of the source. Basically, I'm trying to use the sprite as a "stamp" of a single color to be drawn into the destination bitmap.
I believe I should be using canvas.drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint), however I'm not exactly sure how I should initialize the paint object. Is this approach correct?
You don't need to perform as many steps as Romain Guy suggests, just initialize your paint with the desired color, and use Paint.setColorFilter() with PorterDuff.Mode.SRC_ATOP
myPaint.setColorFilter(new PorterDuffColorFilter(myColor, PorterDuff.Mode.SRC_ATOP));
If your destination bitmap is transparent, draw all your sprites inside that bitmap normally (you can use a null Paint.) Then, draw a filled rectangle that covers the entire bitmap, using the Porter-Duff xfermode called SrcIn (Source In.)
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).