因为要轮播视频,且给视频对应的textureview实现圆角,如果是使用之前的方法会有问题,于是看到了一个布局,可以完美解决问题
每次往里面追加textureview即可 (下面代码中的RoundFrameLayout为下面的链接中的文件内容,复制一份到自己项目即可)
package com.hlfonts.richway.earphone_bluetooth.view
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import com.anythink.basead.ui.component.RoundFrameLayout
class MyRoundFrameLayout : RoundFrameLayout {
constructor(context: Context?) : super(context) {}
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
}
//设置的圆角
val radius = dip2px(this.context, 29.0f)
override fun dispatchDraw(canvas: Canvas?) {
val var2 = canvas!!.saveLayer(
0.0f,
0.0f,
this.width.toFloat(),
this.height.toFloat(),
null,
Canvas.ALL_SAVE_FLAG
)
super.dispatchDraw(canvas)
drawRadiusMask(canvas,this.width,this.height,radius)
canvas.restoreToCount(var2)
}
private fun getRadiusPath(radius: Int, width: Int, height: Int): Path {
val path = Path()
path.moveTo(radius.toFloat(), 0f)
path.lineTo((width - radius).toFloat(), 0f)
path.quadTo(width.toFloat(), 0f, width.toFloat(), radius.toFloat())
path.lineTo(width.toFloat(), (height - radius).toFloat())
path.quadTo(width.toFloat(), height.toFloat(), (width - radius).toFloat(), height.toFloat())
path.lineTo(radius.toFloat(), height.toFloat())
path.quadTo(0f, height.toFloat(), 0f, (height - radius).toFloat())
path.lineTo(0f, radius.toFloat())
path.quadTo(0f, 0f, radius.toFloat(), 0f)
path.close()
return path
}
private fun drawRadiusMask(canvas: Canvas, width: Int, height: Int, radius: Int) {
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
paint.color = Color.WHITE
val maskBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas1 = Canvas(maskBitmap)
canvas1.drawPath(getRadiusPath(radius, width, height), paint)
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
canvas.drawBitmap(maskBitmap, 0f, 0f, paint)
}
}
评论区