我有一个 view
,我想为其设置一个 drawable
图像,这非常简单。图像设置正确,但角是方形的。如果我尝试通过在 xml drawable
中使用 shape
attribute
使角变圆,那么角是圆的,但我找不到设置方法图像 background
作为 view
的背景属性用于设置角的 xml drawable
。
有没有办法将 background
作为图像以及圆角。我知道使用带圆角的图像等解决方案,但我不想为此类小问题打扰图形团队,我希望有一个通用的解决方案。
非常感谢您的帮助。
最佳答案
覆盖 View 的 dispatchDraw
方法(或 onDraw
如果它不是 View 层次结构)
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
}
关于android - 使用自定义形状和 png 图像设置背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8717093/