如何绘制带圆角的路径

bei*_*rad 4 android custom-view android-custom-view android-canvas

我应该像这个镜头一样绘制一个 CustomView。在此输入图像描述

但它们并不相同。角的笔划不同。

我使用 2 个分隔线Path来绘制顶部形状:第一个用于黄色背景:

   private val paint = Paint().apply {
        isAntiAlias = false                    // pass true does not make change
        color = Color.YELLOW
        style = Paint.Style.FILL_AND_STROKE    // pass only FILL does not make change
        }
Run Code Online (Sandbox Code Playgroud)

第二个是:

private val strokePaint = Paint().apply {
        isAntiAlias = false                   // pass true does not make change
        color = Color.BLACK
        strokeWidth = 2.toPx().toFloat()
        style = Paint.Style.STROKE
    }
Run Code Online (Sandbox Code Playgroud)

onDraw()函数中我用它们来绘制:

override fun onDraw(canvas: Canvas) {
        drawPath()

        canvas.drawPath(path, paint)
        canvas.drawPath(path, strokePaint)

        // at the end, draw text and default things to avoid overlapping with background
        super.onDraw(canvas)

    }
Run Code Online (Sandbox Code Playgroud)

更新:现在我画了这个,它的指针有两个侧面。 在此输入图像描述

并使用此路径进行绘制:

 private fun drawPath() {
    path.run {
        moveTo(left + radius, top)

        if (_side == SIDE_TOP) {
            lineTo(pointerX - pointerSize / 2, top)
            lineTo(pointerX, rect.top)
            lineTo(pointerX + pointerSize / 2, top)
        }
        lineTo(right - radius, top)

        arcTo(topRightRect, 270F, 90F, false)
        lineTo(right, bottom - radius)
        arcTo(bottomRightRect, 0F, 90F, false)

        if (_side == SIDE_BOTTOM) {
            lineTo(pointerX + pointerSize / 2, bottom)
            lineTo(pointerX, rect.bottom)
            lineTo(pointerX - pointerSize / 2, bottom)
        }
        lineTo(left + radius, bottom)

        arcTo(bottomLeftRect, 90F, 90F, false)
        lineTo(left, top + radius)
        arcTo(topLeftRect, 180F, 90F, false)
        close()
    }
}
Run Code Online (Sandbox Code Playgroud)

Sae*_*umi 7

Canvas有一些预定义的方法用于绘制圆形和矩形等常见形状。在您的场景中,您可以使用drawRoundRectwhich need aRectF来绘制矩形。

这是一个例子:

在此输入图像描述

class RoundedRect @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val roundCorner = 32f

    private val paint = Paint().apply {
        color = Color.YELLOW
        style = Paint.Style.FILL
        isAntiAlias = true
    }

    private val strokePaint = Paint().apply {
        color = Color.BLACK
        strokeWidth = 4f
        style = Paint.Style.STROKE
        isAntiAlias = true
    }

    private var rect = RectF(0f, 0f, 0f, 0f)

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)

        rect = RectF(0f, 0f, w.toFloat(), h.toFloat())
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        canvas.drawRoundRect(rect, roundCorner, roundCorner, paint)
        canvas.drawRoundRect(rect, roundCorner, roundCorner, strokePaint)
    }
}
Run Code Online (Sandbox Code Playgroud)

顺便说一句,如果你想使用路径绘制圆角,你必须pathEffect设置CornerPathEffect

这是一个例子: 在此输入图像描述


class RoundedRectUsingPath @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val roundCorner = 32f

    private val paint = Paint().apply {
        color = Color.YELLOW
        isAntiAlias = true
        pathEffect = CornerPathEffect(roundCorner)
        strokeCap = Paint.Cap.ROUND
    }

    private val strokePaint = Paint().apply {
        color = Color.BLACK
        strokeWidth = 4f
        isAntiAlias = true
        style = Paint.Style.STROKE
        pathEffect = CornerPathEffect(roundCorner)
    }

    private var path = Path()
    private val offset = 50f

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        path = Path().apply {
            moveTo(offset, offset)
            lineTo(w.toFloat() - offset, offset)
            lineTo(w.toFloat() - offset, h.toFloat() - offset)
            lineTo(offset, h.toFloat() - offset)
        }
        path.close()

    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        canvas.drawPath(path, paint)
        canvas.drawPath(path, strokePaint)
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

我想我已经为你找到了解决方案

我花了相当长的时间才弄清楚那里发生了什么。

请注意,我的解决方案仅绘制圆角矩形,而不使用路径,而是使用drawRoundRect预定义的方法Canvas

我正在创建一个带有边框/描边的自定义进度条。我首先想到的是创建一个Paint实例,将绘画风格设置为Paint.Style.STROKE,然后绘制一个圆角矩形。

override fun dispatchDraw(canvas: Canvas?) {
    // I do the actual initialisations outside of dispatchDraw(). This is just an example.
    val paint: Paint = Paint()
    val strokeWidth = 4f
    val cornerRadius = 10f
    val strokeColor = Color.RED
    val strokeRect = RectF().apply {
        set(0f, 0f, width.toFloat(), height.toFloat())
    }
    
    paint.style = Paint.Style.STROKE
    paint.strokeWidth = strokeWidth
    paint.color = strokeColor
    canvas?.drawRoundRect(strokeRect, cornerRadius, cornerRadius, paint)
}
Run Code Online (Sandbox Code Playgroud)

注意:上面的代码只绘制了描边,如果你想在里面也绘制进度,你可以创建一个新的矩形并设置paint的样式Paint.Style.FILL。做起来应该非常简单。

好吧,这就是我使用上面的代码绘制进度笔划时得到的结果,看起来不太好。

初始版本带描边的进度条

首先,我认为角没有正确绘制,我也许应该增加角半径,但这不是解决方案。在做了一些研究和测试不同的实现之后,我发现问题与角落无关,而是与视图本身有关。我的中风刚刚被切断,不完全可见。

添加插图是解决我的问题的关键,而且做起来非常简单。

这是修改后的代码:

override fun dispatchDraw(canvas: Canvas?) {
    // I do the actual initialisations outside of dispatchDraw(). This is just an example.
    val paint: Paint = Paint()
    val strokeWidth = 2f
    val cornerRadius = 10f
    val strokeColor = Color.RED
    val strokeRect = RectF().apply {
        set(0f, 0f, width.toFloat(), height.toFloat())
        inset(paint.strokeWidth / 2, paint.strokeWidth / 2)
    }
    
    paint.style = Paint.Style.STROKE
    paint.strokeWidth = strokeWidth
    paint.color = strokeColor
    canvas?.drawRoundRect(strokeRect, cornerRadius, cornerRadius, paint)
}
Run Code Online (Sandbox Code Playgroud)

我仅添加了一条线,将插图设置为笔画宽度的一半,这是剪切的确切尺寸。

inset(paint.strokeWidth / 2, paint.strokeWidth / 2)
Run Code Online (Sandbox Code Playgroud)

我还对其进行了strokeWidth一些修改,使其看起来不错(2f而不是4f)。您可以稍后更改它以满足您的设计要求。

瞧,你就中风了,这正是你所期望的。

最终版本的带描边的进度条

希望我的解决方案能够帮助您节省时间和精力!