在Android Canvas上绘制时,角度不正确。

4
我是一个有用的助手,可以为您翻译文本。
我有一个自定义视图,我在Android上创建了一个圆并将其分成几个部分。
以下是onDraw的代码:
int w = Width;
int h = Height;

int pl = PaddingLeft;
int pr = PaddingRight;
int pt = PaddingTop;
int pb = PaddingBottom;

int usableWidth = w - (pl + pr);
int usableHeight = h - (pt + pb);

int radius = Math.Min(usableWidth, usableHeight) / 2;
int cx = pl + (usableWidth / 2);
int cy = pt + (usableHeight / 2);

int lineLenght = radius - (pl * 2) - (pr * 2);

paint.Color = Color.Black;
paint.SetStyle(Paint.Style.Stroke);
canvas.DrawCircle(cx, cy, radius, paint);

//Move to top of the circle
float pointAngle = 360 / noOfJoints;
for (float angle = 0; angle < 361; angle = angle + pointAngle)
{   //move round the circle to each point
    float x = cx + ((float)Math.Cos(radians(angle)) * radius); //convert angle to radians for x and y coordinates
    float y = cy + ((float)Math.Sin(radians(angle)) * radius);
    canvas.DrawLine(cx, cy, x, y, paint); //draw a line from center point back to the point
}

但是当我运行它时,它提供了以下视图:View Layout 这已经接近我的要求,但是节的开始应该从中间开始。如何使它从0度角开始(第一个分隔线应该是从上到下的直线)。
首选圆形如下所示:enter image description here
1个回答

9

试试这个:

for (float angle = 0; angle < 361; angle = angle + pointAngle)
{   //move round the circle to each point
    float displacedAngle = angle - 90;
    float x = cx + ((float)Math.Cos(radians(displacedAngle)) * radius); //convert angle to radians for x and y coordinates
    float y = cy + ((float)Math.Sin(radians(displacedAngle)) * radius);
    canvas.DrawLine(cx, cy, x, y, paint); //draw a line from center point back to the point
}

0度角是圆的最右侧点,减去90度会移动到顶部点。

另外,一个建议是尽可能避免在onDraw方法中创建变量和实例化对象。这会严重影响性能。


谢谢,我以为0度角应该在顶部。感谢您的迅速回复。 - progrAmmar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接