2017-06-06 17 views
3

私は円を描いて、セクションに分割してAndroid用に作成したカスタムビューを持っています。ここで 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

答えて

2

は、これを試してください

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 
} 

角度θ90が最上点に移動する減算、円のrightest点です。

また、onDrawメソッドで変数の作成やオブジェクトのインスタンス化をできるだけ避けてください。それは本当のパフォーマンスのキラーです。

+0

ありがとう、私は0の角度が上にあるべきだと思った。迅速な返信をありがとう – progrAmmar

関連する問題