侧边栏壁纸
博主头像
Stars-One的杂货小窝博主等级

所有的创作都是有价值的

  • 累计撰写 259 篇文章
  • 累计创建 46 个标签
  • 累计收到 27 条评论

目 录CONTENT

文章目录

Android Textureview实现圆角背景代码记录

Stars-one
2024-04-12 / 0 评论 / 0 点赞 / 13 阅读 / 3581 字

因为要轮播视频,且给视频对应的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)
    }

}
0

评论区