android how to rotate canvas rect
Asked Answered
P

3

11

i create a rectangle in a specific size, and now i want rotate it to 45 degree, i used canvas.rotate, matrix, but not working. how is the proper way to rotate canvas in android? and i'm curios about Path.Direction.CW, is it used for rotation? but i don't see any rotation function in Path()

    paint.setAntiAlias(true);
    paint.setStrokeWidth(2);
    paint.setColor(Color.BLUE);
    paint.setAlpha(75);

    Path path = new Path();
    path.addRect(166, 748, 314, 890, Path.Direction.CW);
    canvas.rotate(45);
    canvas.drawPath(path, paint);
Pard answered 9/12, 2012 at 11:8 Comment(4)
If you're curious about Path.Direction, why not Google? First hit - developer.android.com/reference/android/graphics/…. Why are you rotating the canvas before you draw the path? What are you trying to achieve?Detroit
it says clockwise and counterclockwise,i'm not understand its meaning. simple, i just want to rotate the rectangle i created "path.addRect(166, 748, 314, 890, Path.Direction.CW);". if i remove canvas.rotate(45), it will display a rectangle on screen, now i want to rotate it to 45 degree. already googled it but cannot find any solutionPard
You've rotated the canvas then you draw the path. So only whatever is drawn before the path (looks like nothing) is rotated. Why no draw path then rotate?Detroit
you mean like this? paint.setAntiAlias(true); paint.setStrokeWidth(2); paint.setColor(Color.BLUE); paint.setAlpha(75); Path path = new Path(); path.addRect(166, 748, 314, 890, Path.Direction.CW); canvas.drawPath(path, paint); canvas.rotate(45); its not working, same, nothing changesPard
D
18

To draw a rotated rectangle you need to rotate the canvas before drawing, (then rotate it back to right-side up if you're drawing anything else). Canvas.rotate() just alters the canvas's transformation matrix, which transforms shapes drawn after the call.

canvas.save();
canvas.rotate(45);
canvas.drawRect(166, 748, 314, 890, paint);
canvas.restore();

Path.Direction has nothing to do with rotation transforms. From the docs:

Specifies how closed shapes (e.g. rects, ovals) are oriented when they are added to a path.

Dyslogistic answered 29/10, 2013 at 14:30 Comment(1)
note that canvas.rotate by itself will rotate around the origin. If you want to just rotate the rect about it's center, you will need to call canvas.translate(rectCenterX, rectCenterY) and adjust your rect appropriately.Connie
T
7

If you want to draw something from (x,y) point, you have to rotate the canvas around (x,y) point. For doing this you should use

canvas.rotate(45,x,y);

so,

canvas.save();
canvas.rotate(45,x,y);
//all drawing from (x,y) point
canvas.restore();
Telephotography answered 5/12, 2015 at 11:11 Comment(0)
H
2

Proper way should be something like this:

Path path = new Path();
path.addRect(166, 748, 314, 890, Path.Direction.CW);
canvas.save(); // first save the state of the canvas
canvas.rotate(45); // rotate it
canvas.drawPath(path, paint); // draw on it
canvas.restore(); // restore previous state (rotate it back)
Hotze answered 6/8, 2013 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.