2012-01-03 38 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