View的扩展方法
val OvalOutLine = object : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline) {
// View设置为圆形
outline.setOval(0, 0, view.width, view.height)
}
}
fun View?.radius(radius: Float) {
this?.outlineProvider = object : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline) {
outline.setRoundRect(
0, 0, view.width, view.height, radius
)
}
}
this?.clipToOutline = true
}
fun View?.oval() {
this?.outlineProvider = OvalOutLine
this?.clipToOutline = true
}
调用
imageView.oval()//剪裁成圆形
imageView.radius(R.dimen.qb_px_12.px().toFloat())//剪裁出圆角