2012-01-03 96 views
2

我有一個view我想要設置一個drawable圖像,這是很容易的。圖像設置正確,但角落是方形的。如果我嘗試使在xml drawable使用shapeattribute圓潤邊角,然後角落是圓的,但我不能找到一種方法來設置圖像background作爲view的背景屬性是用來設置XML drawable拐角。設置自定義形狀和PNG圖像的背景

有沒有辦法將background作爲圖像以及圓角。我知道像使用圓角圖像的解決方案,但我不想打擾圖形團隊這樣的小問題,我想有一個通用的解決方案。

非常感謝您的幫助。

回答

1

重寫你的觀點的dispatchDraw方法(在案件或onDraw它不是一個視圖層次)

private final Path mClipPath; 
....... 
mClipPath = new Path(); 
mClipPath.addRoundRect(new RectF(x,y,z,t),radius,radius,Direction.CW); 
........ 
@Override 
protected void dispatchDraw(final Canvas canvas){ 
    canvas.clipPath(mClipPath); // clip the canvas so that we can draw only in the boundaries specified by mClipPath 
    super.dispatchDraw(canvas); //now draw the view on the clipped canvas 
    ..... //do other drawing 
} 
+0

對不起,我的延遲反應.. 是的,它的工作原理。但我只是想知道是否有辦法設置自定義形狀以及bg背景,而不是創建自定義視圖。反正它解決了我的問題。 – Arunkumar 2012-01-09 10:09:27

相關問題