[英]Rotating a view around a center point
I've got what I thought was a simple question, yet the solution eludes me. 我有一个我认为简单的问题,但是解决方案使我难以为继。 The premise of the problem is simple: I have a view (call it a point) with center vx, vy that I want to rotate around a bigger circle cx, cy.
问题的前提很简单:我有一个中心为vx,vy的视图(称其为点),我想绕一个更大的圆cx,cy旋转。 I've looked into using sin, cos, radians, atan2, all that jazz, yet for some reason when I rotate my views, it looks as if it's rotating in an oval and not a circle.
我已经研究过使用sin,cos,弧度,atan2和所有爵士乐,但是由于某种原因,当我旋转视图时,看起来好像是在椭圆形而不是圆形中旋转。 The math for this eludes me.
数学对此难以理解。
What I have so far: 到目前为止,我有:
public void update(double matrixAngle)
double angleInDegs = Math.toDegrees(mAngle);
double newAngle = (matrixAngle > 0) ? angleInDegs + matrixAngle : angleInDegs - matrixAngle;
double angleInRads = Math.toRadians(newAngle);
float dx = itemCircle.getCenterX() - getCenterX();
float dy = itemCircle.getCenterY() - getCenterY();
double radius = Math.sqrt(dx * dx + dy * dy);
float x = (float) (getCenterX() + radius * Math.cos(angleInRads));
float y = (float) (getCenterY() - radius * Math.sin(angleInRads));
itemCircle.setCenterX(x);
itemCircle.setCenterY(y);
For some clarification: 对于一些澄清:
matrixAngle
parameter of the method is the matrix angle used to rotate the entire view. 该方法的
matrixAngle
参数是用于旋转整个视图的矩阵角。 It ranges from 0-180, then from -180 to 0, where 0 is opening from the right-side of the circle. 它的范围是0-180,然后是-180到0,其中0是从圆的右侧开始的。
mAngle
is the original angle that the view was drawn at. mAngle
是绘制视图的原始角度。 It ranges from 0 to 360. 取值范围是0〜360。
itemCircle
is the circle representing the view's bounds. itemCircle
是表示视图范围的圆圈。 It is a circle (an extended FloatingActionButton, to be exact). 这是一个圆圈(确切地说是扩展的FloatingActionButton)。
getCenterX(), getCenterY()
are the coords of the bigger circle that I want to rotate around. getCenterX(), getCenterY()
是我要旋转的较大圆的坐标。
I'd really appreciate some help here. 非常感谢您的帮助。 This problem has been bugging me for a day now.
这个问题困扰了我一天。
Your object is moving in an ellipse shape because the pixels aren't square. 您的对象正在以椭圆形移动,因为像素不是正方形。 Notice that in class DisplayMetrics , there are actually different values for
xdpi
and ydpi
. 注意,在DisplayMetrics类中,
xdpi
和ydpi
实际上有不同的值。 You'll need to adjust your computation to account for this. 您需要调整计算以解决此问题。
Got it. 得到它了。 Involved looking more closely at cos, sin angles to achieve what I wanted.
通过更仔细地观察cos和正弦角来实现我想要的。 Working code:
工作代码:
public void update(double matrixAngle) {
double angleInRads = Math.toRadians(matrixAngle);
double cosAngle = Math.cos(angleInRads);
double sinAngle = Math.sin(angleInRads);
float dx = mItemCircle.getCenterX() - getCenterX();
float dy = mItemCircle.getCenterY() - getCenterY();
float rx = (float) (dx * cosAngle - dy * sinAngle);
float ry = (float) (dx * sinAngle + dy * cosAngle);
rx += getCenterX();
ry += getCenterY();
mItemCircle.setCenterX(rx);
mItemCircle.setCenterY(ry);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.