旋转矩形改变位置 [英] Rotated rect changes location
问题描述
我试图旋转一个矩形,我必须旋转画布来做到这一点,但矩形改变其位置,例如,如果矩形是在屏幕中间的旋转后,它会去侧面和i
I am trying to rotate a rect and i have to rotate the canvas to do so but the rect changes its location for example, if the rect is in the middle of the screen after the rotation it will go to the side and i cannot figure out how to get it back to the middle.
这里是我使用的代码:
canvas.save();
canvas.rotate(45);
canvas.drawRect(width/2,height/2, width/2+200, height/2+200, paint);
canvas.restore();
有没有办法旋转矩形而不旋转画布,所以它的位置将保持不变? / p>
Is there a way to rotate the rect without rotating the canvas so its location will stay the same?
推荐答案
您的矩形中心与画布的中心点不同。您可以尝试使用其他旋转方法指定枢轴点的x / y坐标。如:
Your rectangle's center differs from the pivot point of the canvas. You can try using the other rotate method to specify the x/y coordinates of the pivot point. As in:
int offset = 200;
int left = width/2;
int top = height/2;
int right = left + offset;
int bottom = top + offset;
canvas.save();
canvas.rotate(45, left + offset/2, top + offset/2);
canvas.drawRect(left, top, right, bottom, paint);
canvas.restore();
编辑:
只是测试了上面的代码,并验证它的工作原理(给了一些调整,我刚才添加,上面)。
Just tested the code above and verified that it works (given a few tweaks that I just added, above).
这篇关于旋转矩形改变位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!