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

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?

Related

Rotate and resize rectangle drawn in canvas in Android

In my Android application I have drawn rectangle in canvas, how can I rotate rectangle and resize the rectangle on touch events ?
canvas.drawRect(300,300,500,500,paint);
This code is used to draw the rectangle.
You can do using below
canvas.save(); // saving current canvas
resize
rect.set(x,y,newWidth, newHeight);
rotate
canvas.rotate(45);
Apply changes
canvas.drawRect(rect, paint);
canvas.restore();

How can I translate canvas after rotation to keep a rectangle in the center of another in android?

I want to be able to draw text and rectangles to the canvas after I have rotated them along their center. So I am making a test case where I have a blue square that does not get rotated, and a red square that should be rotated. THey are the same size, and should share the same center "pivot point". I have the following code:
Paint p = new Paint();
p.setColor(Color.CYAN);
p.setAlpha(200);
canvas.drawRect(new Rect(100,100,300,300), p);
canvas.save();
canvas.rotate(45,250,250);// 250,250 is the center of the blue rectangle
p.setColor(Color.RED);
p.setAlpha(100);
canvas.drawRect(new Rect(100,100,300,300), p);
canvas.restore();
It gives me a result close to what I want, but I am missing some math, because it looks like the canvas also needs a translation applied. Here is the result:
What I am missing so that I can rotate the red rectangle along the center of the blue one, and they end up sharing the same center point like this:
The center of the blue rectangle is wrong.
center(x,y) = (left + (width/2), top + (height/2))
Note: width = 200, height = 200 so, center(x,y) = (200,200)
Change to this, and it works:
canvas.rotate(45,200,200);// 200,200 is the center of the blue rectangle

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

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:)

How to center an shape created through canvas?

I am using android java to create an application that uses the accelerometer, i have created a shape that does kinda move with the accelerometer but it stays in the top left hand corner and moves there, can anyone help me with how can i move it to the center of the screen and set bounds
I am assuming you are using a canvas to draw that stuff if so canvas has a method called getwidth() and also getheight()
what you can do is when you are drawing it to the canvas set the location point x and y coordinates as
canvas.getWidth()/2
and canvas.getHeight()/2
something like this
canvas.drawBitmap(bitmap, canvas.getWidth()/2, canvas.getHeight()/2, null);
I hope you got the idea.
canvas.drawRect(left, top, right, bottom, paint)
use this function if you wanna draw a rectangle
Parameters:
left The left side of the rectangle to be drawn
top The top side of the rectangle to be drawn
right The right side of the rectangle to be drawn
bottom The bottom side of the rectangle to be drawn
paint The paint used to draw the rect
Use the same logic in this method and you are golden ;)

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