简体   繁体   English

Android:将四贝塞尔曲线圆角添加到“视图”

[英]Android: Add quad bezier rounded corners to View

I want to have an ImageView containing an Image with rounded curves, but not circular (which is way easier of course).. 我想有一个ImageView包含一个带有圆角曲线的图像,但不是圆形的(当然这很容易)。

在此处输入图片说明

The X marked areas should be black, the rest should stay blue. X标记的区域应为黑色,其余区域应保持蓝色。 It should look (kind of) like this 它看起来应该像这样

What I tried: I spent hours fighting with Path.quadTo and Path.cubicTo with the help of some tools but I didn't have any success yet. 我尝试过的方法:在某些工具的帮助下,我花了数小时与Path.quadToPath.cubicTo战斗,但是我还没有取得任何成功。 I just don't really get the usage to be honest. 老实说,我只是没有真正的用法。

What my code currently looks like: 我的代码当前如下所示:

override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val paint = Paint()
        paint.color = Color.BLACK
        paint.strokeWidth = 1f
        paint.strokeCap = Paint.Cap.ROUND
        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP)

        paint.style = Paint.Style.FILL

        val fHeight = canvas.height.toFloat()
        val startEndHeight = canvas.height / 1.18f
        val fWidth = canvas.width.toFloat()
        val halfWidth = (fWidth / 2)

        val path = Path()
        //X = Left side, Y = close to bottom
        val ptStart = PointF(0f, startEndHeight)
        //X = Middle, Y = Bottom
        val ptMiddle = PointF(halfWidth, fHeight + 95)
        // X = Right Side, Y = close to bottom
        val ptEnd = PointF(fWidth, startEndHeight)

        path.moveTo(ptStart.x, ptStart.y)
        path.quadTo(ptMiddle.x, ptMiddle.y, ptEnd.x, ptEnd.y)

        path.close()

        canvas.drawPath(path, paint)
    }

It can't be that hard, right? 不会那么难吧? Is it possible to colorize the red marked areas and leave everything else untouched? 是否可以将红色标记的区域着色,并保持其他所有内容不变?

Fixed! 固定! This is my final solution which applies a quad bezier to the ImageView. 这是我的最终解决方案,将四倍贝塞尔曲线应用于ImageView。 I had to add 2 lines to the path to get the result I wanted. 我必须在路径上添加2行才能获得想要的结果。

class HeaderImageView : AppCompatImageView {
    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    lateinit var paint: Paint

    private fun init() {
        paint = Paint()
        paint.color = Color.WHITE
        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP)
        paint.style = Paint.Style.FILL
    }

    @SuppressLint("CanvasSize")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        val fHeight = canvas.height.toFloat()
        val startEndHeight = canvas.height / 1.18f
        val fWidth = canvas.width.toFloat()
        val halfWidth = (fWidth / 2)

        val path = Path()
        //X = Left side, Y = close to bottom
        val ptStart = PointF(0f, startEndHeight)
        //X = Middle, Y = Bottom
        val ptMiddle = PointF(halfWidth, fHeight + 95)
        // X = Right Side, Y = close to bottom
        val ptEnd = PointF(fWidth, startEndHeight)

        path.moveTo(ptStart.x, ptStart.y)
        path.quadTo(ptMiddle.x, ptMiddle.y, ptEnd.x, ptEnd.y)
        path.lineTo(fWidth, fHeight)
        path.lineTo(0f, fHeight)

        path.close()

        canvas.drawPath(path, paint)
    }
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM