如何绘制带圆角的路径

问题描述 投票:0回答:3

我应该像这张照片一样画一个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
        }

第二个是:

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

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)

    }

更新: 现在我画了这个,它的指针有两个面。

并使用此路径绘制:

 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()
    }
}
android android-canvas android-custom-view custom-view
3个回答
6
投票

Canvas
有一些预定义的方法来绘制圆形和矩形等常见形状。在您的场景中,您可以使用
drawRoundRect
需要一个
RectF
来绘制一个矩形。

举个例子:

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)
    }
}

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

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)
    }
}

3
投票

我想我有适合你的解决方案

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

请注意,我的解决方案仅绘制了一个圆角矩形,而没有使用 路径,而是

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)
}

注意:上面的代码只画了笔画,如果你想把进度也画在里面,可以新建一个rect,设置样式 油漆

Paint.Style.FILL
。它应该非常简单 去做。

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

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

添加 insets 是解决我的问题的关键,而且非常简单。

这是修改后的代码:

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)
}

我只添加了一条线,将 insets 设置为笔划宽度的一半,这是被切割的确切尺寸。

inset(paint.strokeWidth / 2, paint.strokeWidth / 2)

我还稍微修改了

strokeWidth
使其看起来不错(
2f
而不是
4f
)。你可以 稍后更改它以符合您的设计要求。

在那之后,你得到了你期望的中风。

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


0
投票

只是为了创建一个圆形路径,可以考虑创建一个使用,

import com.google.android.material.shape.ShapeAppearanceModel
import com.google.android.material.shape.ShapeAppearancePathProvider

fun createRoundPath(width: Int, height: Int, roundSize: Float): Path {
    val roundPath = Path()
    val appearanceModel = ShapeAppearanceModel().withCornerSize(roundSize)
    val rect = RectF(0f, 0f, width.toFloat(), height.toFloat())
    ShapeAppearancePathProvider().calculatePath(appearanceModel, 1f, rect, roundPath)
    return roundPath
}

(有趣的是,我从其他地方的 OP 那里学到了这个,当时这个问题已经发布,我猜这不存在)

© www.soinside.com 2019 - 2024. All rights reserved.