问题
做直播功能的自定义View,嵌套了很多子View,如何能给整个View 切圆角呢?
方案
摘录部分自定义View的代码如下
class LiveStageItemView(context: Context, attrs: AttributeSet? = null) :
ConstraintLayout(context, attrs) {
private var itemWidth = 147.pt
private var itemHeight = 110.pt
init {
clipCorner(itemWidth, itemHeight)
}
// 切圆角
fun clipCorner(width: Int, height: Int) {
this.outlineProvider = object : ViewOutlineProvider() {
override fun getOutline(view: View?, outline: Outline?) {
outline?.setRoundRect(0, 0, width, height, 4.pt.toFloat())
}
}
this.clipToOutline = true
}
}
亲测可用,重点代码如下:
this.outlineProvider = object : ViewOutlineProvider() {
override fun getOutline(view: View?, outline: Outline?) {
outline?.setRoundRect(0, 0, width, height, 4.pt.toFloat())
}
}
this.clipToOutline = true
谨记备忘!