2013-04-04 43 views
0

我必須繪製一個由圓形內的三角形組成的自定義shapeDrawable。我可以繪製圓和三角形的問題是,我想三角形是透明的,但是當我給一個三角形的路徑透明的顏色,我會看到圓的顏色insde三角形我想沒有顏色作爲三角背景的圓? 這裏是我使用的代碼:透明三角形裏面一個圓圈我如何覆蓋圓形的顏色

@Override 
    protected void onDraw(Canvas canvas) { 

    Paint p = new Paint(); 

    int x = 1; 
    int y = 1; 
    Rect bounds = canvas.getClipBounds(); 


    p.setColor (color.getBackgroundColor()); 
    p.setStyle(Style.FILL); 
    p.setStrokeWidth (0); 

    Path path = new Path(); 
    Point p1 = new Point(bounds.centerX()-bounds.height()/4, bounds.centerY()-bounds.height()/4); 
    Point p2 = new Point(bounds.centerX()-bounds.height()/4, bounds.centerY()+bounds.height()/4); 
    Point p3 = new Point(bounds.centerX()+bounds.height()/4, bounds.centerY()); 
    path.moveTo(p1.x, p1.y); 
    path.lineTo(p2.x, p2.y); 
    path.lineTo(p3.x, p3.y); 
    path.close(); 

    Paint pTriang = new Paint(); 
    pTriang.setColor(Color.TRANSPARENT); 
    pTriang.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR)); 
    canvas.drawCircle(bounds.exactCenterX(), bounds.exactCenterY(), bounds.height()/2, p); 
    canvas.drawPath(path, pTriang); 

} 

BTW我正在重寫的ImageView的!

+1

看在http://stackoverflow.com/questions/3874424/android-looking-for-a-drawarc-method-with-inner-outer-radius – ngesh 2013-04-04 14:47:58

+0

:在你的構造函數的末尾添加這這個問題,他們沒有給出任何解決方案,因爲我已經使用Xfermode,但我不能有我想要的東西 – 2013-04-04 15:05:58

回答

1

而不是試圖畫出三角形,如何剪裁畫布?

/* set up triangle path... */ 
path.close(); 
canvas.save(); 
canvas.clipPath(path, Region.Op.XOR); 
canvas.drawCircle(...); 
canvas.restore(); 
+0

謝謝你回答@Karakuri,但是這並不顯示任何我做了我的三角形path.close然後你有什麼書面但它只給了一個圓圈!沒有三角形顯示! – 2013-04-04 15:53:36

0

我認爲你需要在Honeycomb和更高版本上設置圖層類型爲軟件。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
    setLayerType(LAYER_TYPE_SOFTWARE, null); 
}