Can you draw a Bitmap to a Canvas by telling it to draw Bitmap's X,Y at Canvas' X1,Y1? Rather than drawing Bitmap's 0,0 at Canvas X1, Y1?
That's truly possible the Canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) is here to help. You can specify src region to draw. Look at Canvas docs for more.
Also you may use Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height) to extract that part of original image you want to render. Then just render extracted Bitmap as ususal. More info about the method is available on Google's docs.
Related
I have a 200x200px bitmap. I want to draw the top left 50x50px corner of my bitmap, on my canvas at coordinates 100,100 with a width and height of 50px, by using:
drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
This is what I've tried:
drawBitmap(myBitmap, new Rect(0,0,50,50), new Rect(100,100,150,150) , null);
What am I doing wrong?
From developer.android.com:
Parameters
bitmap The bitmap to be drawn
src May be null. The subset of the bitmap to be drawn
dst The rectangle that the bitmap will be scaled/translated to fit into
paint May be null. The paint used to draw the bitmap
What is missing in my code?
Thanks!
You need to change your rectangles. This is because, as described in the documentation, the first rectangle is the subset of the bitmap you want to draw, the second is the scaling/translating so basically the size of the destination draw (50x50)
So it should look like this:
drawBitmap(myBitmap, new Rect(100,100,150,150), new Rect(0,0,50,50) , null);
Background: Using Canvas, Paint and Path objects I draw several geometries on the canvas, mostly polygons and circles. They fill most of the Android screen.
Question: With Mathematica I can 'fast-copy' Graphics using Translate ( in x and y direction ), after which the resulting image is automatically zoomed out such that all copies are visible. ( For example. Draw a square that fills the entire screen, copy it using (2,2) and four squares appear. ) The premise is that copying is a faster operation. - Is a similar operation possible on Android?
There's nothing as convenient as that, but to achieve the effect you can draw directly to a Bitmap and re-use it - scaling and translating it yourself.
public void onDraw(Canvas canvas) {
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas bmpCanvas = new Canvas(bmp);
// draw into bmpCanvas
// ...
// draw bitmap using
// public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint)
canvas.drawBitmap(bmp, ...);
I'm just a beginner with android. I want to divide a bitmap image into chunks and then display the image in the same way but divided. is this possible?
Instead of trying to break the image up into chunks you should just use:
canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
This will draw parts of the image. So to give the effect of breaking it up you are really just using the src and dest rectangles to draw specified parts of the images. Where src is the rectangle in local image coordinates of the part you want to draw and dest is the rectangle used to position it on the screen.
You can use the following method to get an area of an source bitmap:
Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height)
I want to cut the selected portion of bitmap and i have to paste it in the same bitmap using .. please help me out..
You can do it by generating an second Bitmap with the portion the user selected. Them to draw, just use the Canvas of the first. It's not so hard.
A tip is: The Canvas.drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
This draws a portion of the Bitmap limited by the Rects args.
Ok so I'm making a game and I'm having a problem, that Is I don't have
anyway of going about this, Making a line that looks like a pencil stroke.
I start out with
drawLine(float startX, float startY, float stopX,float stopY, Paint paint);
of course
and then I I use all the stuff involved with Paint() and the paint
class so I set the color and the thickness in the paint...
So What I'm wondering is how do I give my paint a texture that
resembles a pencil stroke. is there a way I can make the paint look
like a repeated bitmap, what are my options with this.
Thanks, Brian
p.s. if you want more detail I can expound on it a little more like
give you the exact code that I'm using and explain more about my
game....
also here are links to the paint class and the canvas class if you
need them.
http://developer.android.com/reference/android/graphics/Paint.html
http://developer.android.com/reference/android/graphics/Canvas.html
Also
void drawBitmap(int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, Paint paint)
Treat the specified array of colors as a bitmap, and draw it."
Can I use that I'm really not sure what its saying.
please even if you have a little information please reply.
"void drawBitmap(int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, Paint paint) Treat the specified array of colors as a bitmap, and draw it." Can I use that I'm really not sure what its saying.
It seems you would want this version of the Canvas.drawBitmap() series of methods: drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint).
Ideally you could make your own texture and either stretch it or tile it.. I'm not sure how to tile, but a link in the "Related" section to the right points at TileMode.REPEAT of class BitmapShader.
If your texture isn't going to have any noise ( or anything else that would distort upon stretching ) you may just want to use Drawable.draw() and use a 9-patch for your pencil-stroke.