Using method -canvas.drawBitmap(bitmap, src, dst, paint) - android

Everytime I use this code nothing is drawn. I need to draw a bitmap inside of a specified rectangle.
canvas.drawBitmap(MyBitmap, null, rectangle, null)
I've looked online but can't find much help.

EDIT
The original answer is incorrect.
You can use the sourceRect to specify a part of a Bitmap to draw.
It may be null, in which case the whole image will be used.
As per the fryer comment he was drawing beneath something, I'll add a note on that.
drawBitmap(bitmap, srcRect, destRect, paint)
does not handle Z ordering (depth) and the order of calling draw on object matters.
If you have 3 shapes to be drawn, square, triangle and circle. If you want the square to be on top then it must be drawn last.
You're not specified any source, so its not drawn anything.
Example:
You have a Bitmap 100x100 pixels. You want to draw the whole Bitmap.
canvas.drawBitmap(MyBitmap, new Rect(0,0,100,100), rectangle, null);
You want to draw only the left half of the bitmap.
canvas.drawBitmap(MyBitmap, new Rect(0,0,50,100), rectangle, null);
You need to specify the source rect, the source rect can be a rectangle anywhere from 0,0 to the width,height of the bitmap.

The main item to remember when defining the Rect is:
left < right and top < bottom
The rect is in screen coordinates (positive Y downward) ...
I find it helpful to think of the Rect arguments
(left, top, right, bottom)
as
(X, Y, X + Width, Y + Height)
where X,Y is the top left corner of the sprite image.
NOTE: If want to center the image on a particular location, remember to offset those values by half the sprite width & height. For example:
int halfWidth = Width/2;
int halfHeight = Height/2
Rect dstRectForRender = new Rect( X - halfWidth, Y - halfHeight, X + halfWidth, Y + halfHeight );
canvas.drawBitmap ( someBitmap, null, dstRectForRender, null );
This uses the whole original image (since src rect is null) and scales it to fit the size and position from dstRectForRender ... and using the default Paint.

I dont know why but this worked for me!
Rect rectangle = new Rect(0,0,100,100);
canvas.drawBitmap(bitmap, null, rectangle, null);
Thanks:)

Related

Find the center of a shape (path) on a canvas (Android Studio)

Is it possible to find the center of a shape on a canvas in Android studio? I've logged the touch points and can't seem to figure it out. It does however appear that the canvas (0,0) point is the top left, but the paths/shapes on the canvas see the center point as (0,0).
For an example of what I'm talking about check out the attached image, I'm trying to find where the green dot is.
Thanks in advance for any help.
IMG
To find the center of a Path use the method computeBounds(bounds,exact) which will set the first argument RectF bounds to the extent of the Path. Then it is just a matter of getting the mean of the left right and top bottom coordinates to get the geometric center of the path.
// mPath is your path. Must contain more than 1 path point
RectF bounds = new RectF();
mPath.computeBounds(bounds, false); // fills rect with bounds
PointF center = new PointF((bounds.left + bounds.right) / 2,
(bounds.top + bounds.bottom) / 2);
No Need for Mathematical Calculations
RectF bounds = new RectF();
mPath.computeBounds(bounds, false);
float centerX = rectF.centerX();
float centerY = rectF.centerY();

Android Canvas.drawBitmap(bmp, left, top, paint) after rotation

Is there a detailed documentation explaining the left/top parameter used in the canvas.drawBitmap method?
I thought it should always be the position relative to left-top corner of the canvas. For example, if I want to draw something at the top left corner, I just draw from origin (0,0).
However, I found this is not the case if the canvas is rotated 90 degree.
canvas.save();
canvas.rotate(90, canvas.getWidth()/2, canvas.getHeight()/2);
canvas.drawBitmap(bmp, 0, 0, null);
canvas.restore();
I was expecting the bitmap should be drawn from the left top corner on the rotated canvas, and displayed from right top corner after I restore the canvas, but it comes out different result.
I have to draw at
x= (canvas.getWidth()-canvas.getHeight())/2
y= (canvas.getHeight()-canvas.getWidth())/2
to get the expected result, why?

In Android, how do I give a Rect an X and Y position?

I'm trying to draw a simple Rect on a Canvas that is at the X coordinate of 360, and the Y coordinate of 0. I can draw my Rect if I make the X coordinate to 0, but when I make it 360, the square becomes distorted and becomes a rectangular shape rather than a square anymore. My screen size is 640px wide, so there should be no problem here. I can draw Bitmaps with the same specifications and it will draw normally. Why is it that Rects don't draw correctly? Is it somehow that the X coordinate is only in DP rather than PX? Then why does that affect the actual size of the Rect? I'm really confused.
Rect square6 = new Rect();
square6.set(360, 0, 60, 60);
You should read the reference to the Rect in Android, The set func of Rect is public void set (int left, int top, int right, int bottom), you set your rect start from (360, 0) and ends at (60, 60), you should change the parmas to (360, 0, 420, 60). It will work.

Set center of a drawable at a point

In a custom view, is there a way I can set the center of a drawable at a certain point on the canvas? (I don't want to deal with figuring out rectangular bounds for the drawable...)
When you make the call to draw the Bitmap, draw it at a point with it's left and top attributes set to the position minus half the image's width and height, respectively. That will center it at the point you want.
IE.
canvas.drawBitmap(bitmap, xPos - bitmap.getWidth()/2, yPos - bitmap.getHeight()/2, new Paint());

draw an image in the center of another rectangle using canvas

In my view I have a big rectangle, the rectangle can move. When the rectangle moves to someplace, I want to draw image in the center of the big rectangle. My question is that I cannot put the center of the image to the center of the rectangle.
I used:
canvas.drawBitmap(rotatedBitmap, matrix, paint)
canvas.drawBitmap(rotatedBitmap, left, top, paint)
but i cannot find canvas.drawBitmap(rotatedBitmap, centerX, centerY, paint), so I want to use matrix, but the matrix also moves image from the left and top start, not from center, can you give some clue to draw the pic in the center of the rectangle?
Try using the bounds of the rectangle as a reference point, then use something like:
imageStartX = (rectStartX + (rectWidth/2)) - (imageWidth/2);
imageStartY = (rectStartY + (rectHeight/2)) - (imageHeight/2);

Categories

Resources