2013-04-02 16 views
2

我想畫在畫布上的橢圓形。基本上,我將四條貝塞爾曲線組合成橢圓形。如何關閉畫布中複雜的路徑?

我成功繪製了一個橢圓形,但我不知道如何關閉它。

代碼:

//Paints for bazier curve 
Paint bezierPaint = new Paint(); 
bezierPaint.setColor(Color.RED); 
bezierPaint.setStyle(Paint.Style.STROKE); 
bezierPaint.setStrokeCap(Paint.Cap.ROUND); 
bezierPaint.setStrokeWidth(3.0f); 
bezierPaint.setAntiAlias(true); 

//Oval Path 
Path ovalPath = new Path(); 

//Draw the first curve. from top point to right point 
ovalPath.moveTo(160,0); 
ovalPath.cubicTo(210, 0, 260, 100, 260, 150); 

//Draw the second curve. from right point to bottom point 
ovalPath.moveTo(260, 150); 
ovalPath.cubicTo(260, 200, 210, 300, 160, 300); 

//Draw the thrid curve. from bottom point to left point   
ovalPath.moveTo(160, 300); 
ovalPath.cubicTo(110, 300, 60, 200, 60, 150); 

//Draw the fourth curve. from left point to top point   
ovalPath.moveTo(60, 150); 
ovalPath.cubicTo(60, 100, 110, 0, 160, 0); 

**//I expect this oval close correctly. But in actually, the fourth curve was closed. 
//Please see the image in attachment.How should I close this path as my expectation?** 
ovalPath.close() 

canvas.drawPath(ovalPath, bezierPaint); 

enter image description here

+0

爲什麼你沒有使用Shape(http://developer.android.com/reference/android/graphics/drawable/shapes/OvalShape.html)和resize(x,y)(http://developer.android.com /reference/android/graphics/drawable/shapes/Shape.html#resize(float,float)) – Mario

回答

2

我覺得這是很容易使用下面的函數來繪製一個橢圓:

canvas.drawOval(ovalRect, paint); 

的ovalRect是RectF對象,其中橢圓形將找到它的位置。

3

只需撥打電話path.close()即可。該功能從當前繪製點到第一個繪製點添加一條線段。我認爲跳過這個電話並不會有什麼壞處。

或者,更多地關注細分的方向,使每個細分開始於最後一個結束位置,更重要的是,讓最後一個結束於第一個結束位置。

或者做什麼marnaish說,並使用drawOval()來代替。