2013-04-26 138 views
0

我正在學習Android,並嘗試在畫布上繪製不同的形狀。目前,我堅持用不同的角度橢圓:如何在Android中繪製形狀

enter image description here

我試着使用path.addRoundRect()方法(其中一個需要半徑的陣列),但無法弄清楚怎樣設定我通過那裏以達到這樣的形狀。我也嘗試過使用path.lineTo(),但無法達到這樣的效果(這有點類似,但仍然不是我所需要的)。什麼會是一個很好的解決方案來完成這個?

編輯1:我曾嘗試爲以下幾點:

Path path= new Path(); 
    path.moveTo(x - radius, y - radius/ 1.5f); 
    path.lineTo(x - radius/ 4, y - radius); 
    path.lineTo(x, y - radius); 
    path.lineTo(x + radius/ 2, y - radius); 
    path.lineTo(x + radius, y - radius/ 2); 
    path.lineTo(x, y + radius/ 2); 
    path.lineTo(x - radius/ 2, y + radius/ 1.5f); 
    path.lineTo(x - radius, y + radius/ 4); 
    path.lineTo(x - radius, y - radius/ 1.5f); 
    path.close(); 

Paint pathPaint = new Paint(); 
     pathPaint.setColor(Color.BLACK);      
     pathPaint.setStrokeWidth(2.5f);    
     pathPaint.setDither(true);      
     pathPaint.setStyle(Style.STROKE);  
     pathPaint.setStrokeJoin(Join.ROUND); 
     pathPaint.setStrokeCap(Cap.ROUND);  
     pathPaint.setPathEffect(new CornerPathEffect(20)); 
     pathPaint.setAntiAlias(true); 
     canvas.drawOval(new RectF(x - radius, y - radius+ 2, x + radius-2, y + radius- 2), pathPaint); 
     canvas.drawPath(path, pathPaint); 

X和Y是顯示器和半徑上的一些座標是圓的半徑(我開始用圓圈繪製)。它等於14 px。

我也試着這樣說:

float[] radii = new float[] { 
       5, 
       5, 
       1, 
       1, 
       5, 
       1, 
       1, 
       1, 

     }; 
     path.addRoundRect(new RectF(x - radius, y - radius, x + radius, 

y + radius), 
        radii, Direction.CW); 
canvas.drawPath(path, pathPaint); 
+0

你可以發佈你的代碼嗎? – petey 2013-04-26 17:53:58

+0

http://developer.android.com/reference/android/graphics/drawable/shapes/OvalShape.html – 2013-04-26 17:55:18

回答

0

嘗試Canvas.skew()函數。下面的URL有一個很好的例子。傾斜「傾斜」正在繪製的點。根據畫布的當前中心,您將得到不同的結果,因此可以根據需要使用translate()來獲得所需的結果。

http://css3files.com/transform/

+0

謝謝你的建議。我無法驗證它,因爲我不在這個項目atm上工作。也許其他人可以證實,如果它工作與否。 – 2016-05-17 10:14:00