user1998160
user1998160

Reputation: 59

How to rotate a Rect object in Android

Ok so i have created a Rectangle on a canvas using this code

Rect r =new Rect((point.x - rectWidth / 2), (point.y - rectHeight / 2),(point.x-rectWidth / 2),(point.y + rectHeight / 2));

point being the center of the rectangle determined by a touch event. But now I want to be able to rotate this rectangle. Is there any code or method that will allow the user to rotate a shape about its center. I am also interested in getting the corner points of such a rectangle. Any help is appreciated. Thanks.

Upvotes: 4

Views: 10802

Answers (2)

Simptive
Simptive

Reputation: 987

Use following:

Rect r2 = new Rect(r1);
Matrix mat = new Matrix();
mat.setRotate(-90, r2.centerX(), r2.centerY());
mat.mapRect(r2);

Upvotes: 2

Alex North
Alex North

Reputation: 1209

To rotate a rectangle around its own center (as opposed to the origin):

Matrix m = new Matrix();
// point is the point about which to rotate.
m.setRotate(degrees, point.x, point.y);
m.mapRect(r);

You can also use Matrix.mapPoints() to find how the corners are transformed.

Upvotes: 8

Related Questions