Android - How to rotate a Rect object?

I have a rectangle: Rect r = new Rect();. I want to rotate the object r45 degrees. I checked the solutions and found that this can be done using matrices:

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

The problem is that the whey skip rto m.mapRect(r);, she complains what rshould be of type RectF. I managed to do this like:

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

But the problem is that I need an object of type Rectnot RectF. Because I am passing an object rto an outer class that accepts an object Rect.

Is there any other way to rotate a rectangle of ra shape type Rectexcept for this method and without rotating the entire canvas (the canvas contains some other elements)?

Thank you in advance!

Sincerely, Dimitar Georgiev

+4
1

. Rect RectF . Matrix.mapRect(), RectF , , .

, . , , , .

canvas.save();
canvas.rotate(45);
canvas.drawRect(r,paint);
canvas.restore();
+8

All Articles