2017-02-13 162 views
-1

我在屏幕的中間畫了一個圓,我想從圓的中心向上向下繪製四條線a,向左和向右 ,問題是我繪製的鏈接我不能確定它們的長度繪製線的座標

enter image description here

@Override 
protected void onDraw (Canvas canvas){ 
    super.onDraw(canvas); 
    int x = getWidth(); 
    int y = getHeight(); 
    int radius; 
    radius = 25; 
    Paint paint = new Paint() ; 
    paint.setStyle(Paint.Style.FILL); 
    paint.setColor(Color.BLACK); 
    canvas.drawPaint(paint); 
    paint.setColor(Color.WHITE); 
    canvas.drawCircle(x/2, y/2, radius, paint); 
    Paint paint1 = new Paint() ; 
    paint1.setStyle(Paint.Style.FILL); 
    paint1.setColor(Color.BLACK); 
    canvas.drawPaint(paint1); 
    paint1.setColor(Color.WHITE); 
    canvas.drawLine(x/2, y/2, x, y/2, paint1); 
    canvas.drawLine(x/2, y/2, -x, y/2, paint1); 
    canvas.drawLine(x/2, y/2, x/2, y, paint1); 
    canvas.drawLine(x/2, y/2, x/2, -y, paint1); 
} 
+0

那麼,這是什麼問題? – Altoyyr

+0

@Altoyyr我想要四條線的座標 – CamlX

+0

所以 - 你想畫4條線?一個從圈子中心向上。一個從圈子的中心向下。一個從圓圈的中心向左。還有一個從圈子的中心向右。那是對的嗎? –

回答

0

所以,你要4線 - 兩條對角線的,一個垂直和水平之一,所有過境通過圓心。

首先,讓我們設置的圓心:

int centerX = x/2; 
int centerY = y/2; 

然後,添加一些輔助變量的左,上,右,下了線。我們加一點餘量,這樣會有餘地額外的線路在對角線的兩端:

int margin = 10; // Arbitrary value, modify as desired 
int top = 0 + margin; 
int bottom = canvas.getHeight() - margin; 
int left = 0 + margin; 
int right = canvas.getWidth() - margin; 

有了這些,我們可以繪製線條容易:

// Vertical line. X coordinate = center, stretching from "top" to "bottom" 
canvas.drawLine(centerX, top, centerX, bottom); 

// Horizontal line. Y coordinate = center, stretching from "left" to "right". 
canvas.drawLine(left, centerY, right, centerY); 

// First diagonal line. Stretching from the top left corner to the bottom right corner. 
canvas.drawLine(top, left, bottom, right); 

// Second diagonal line. Stretching from the top right corner to the bottom left corner. 
canvas.drawLine(top, right, bottom, left); 

現在加入小線段使它看起來像一個曲線:

int delta = 4; // Arbitrary value, determines how long the small lines will be. 
canvas.drawLine(top, left, top + delta, left - delta); 
canvas.drawLine(top, right, top + delta, right + delta); 
canvas.drawLine(bottom, left, bottom - delta, left - delta); 
canvas.drawLine(bottom, right, bottom - delta, right + delta); 

如果你想曲線是圓的,而不是尖銳的,你有兩個選擇。
首先是製作透明物體Image並將其放在畫布上。您可以製作一條曲線的圖像,並將其鏡像以獲取其他三條曲線。
第二個是使用Path.cubicTo,但這是你可能想要稍後嘗試的東西。如果你想了解它,在this Stack Overflow answer中有詳細解釋。

+0

並且對於長度?,我從圓中畫出了四條線,對於其他四條線,它們不是對角線,它們有一定的曲線 – CamlX

+0

@AbbesChouokchou Hm,取決於曲線是如何定義的。如果曲線看起來像一個尖銳的「V」,那麼可以通過爲'left','right','top'和'bottom'設置較小的值來縮短線條。然後你可以從那裏添加一小條額外的線。垂直線條爲 –

+0

,如何更改長度? – CamlX