2013-04-15 190 views
0

我想提請使用該方法的弧的一部分:.Canvas.drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)繪製的弧是一個圓圈

我對圓,圓心和半徑兩分。

我需要在橢圓矩形中設置什麼以及如何計算startAngle和sweepAngle?

我嘗試下面的代碼:

m_radiusRect.set(x1, Math.min(y1, y2), x2, Math.max(y1,y2)); 
float startAngle = (float)((Math.toDegrees(Math.atan2(x1 - 360.0, 360.0 - y1)) + 360.0) % 360.0); 
float sweepAngle = (float)((Math.toDegrees(Math.atan2(x2 - 360.0, 360.0 - y2)) + 360.0) % 360.0) - startAngle; 

canvas.drawArc(m_radiusRect, startAngle, sweepAngle, false, m_paint); 

回答

0

this答案,並找到中心點的角度。

基於此,找到x1和x2的兩個角度表示a1和a2。

然後,

sweepAngle = a2 - a1; 
startAngle = a1; 

編輯

中給出的鏈接,因爲它看起來不工作不考慮中間的公式。

這裏中心(CX,CY)

float startAngle = (int) ((float) Math.toDegrees(Math.atan2(x1 - cx, y1 - cy))); 
     float sweepAngle = (int) ((float) Math.toDegrees(Math.atan2(x2 - cx, y2 - cy))) - startAngle; 

     Rect rect = new Rect(); 

     rect.left = (int) (cx - radius); 
     rect.top = (int) (cy - radius); 
     rect.right = (int) (cx + radius); 
     rect.bottom = (int) (cy + radius); 
+0

它不工作。查看編輯 – piojo

+0

查看最新答案 –

+0

它無法正常工作。我是否需要將useCenter傳遞給drawArc方法?我嘗試在一個示例項目中繪製一個簡單的弧線,以查看我在點 – piojo