我创建了一个自定义 View 来画一条线,但要逐步进行。我尝试使用PathMeasure和getSegment,但是效果不起作用。它只是一直用最终尺寸绘制线。

private val paint = Paint().apply {
    isAntiAlias = true
    color = Color.WHITE
    style = Paint.Style.STROKE
    strokeWidth = 10f
}


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

    val path = Path().apply {
        moveTo(width/2.toFloat(), height/2.toFloat())
        lineTo(width/2.toFloat(), height/4.toFloat())
    }

    val measure = PathMeasure(path, false)
    val length = measure.length
    val partialPath = Path()
    measure.getSegment(0.0f, length, partialPath, true)
    partialPath.rLineTo(0.0f, 0.0f)
    canvas!!.drawPath(partialPath, paint)
}

最佳答案

你可以用DashPathEffect做到这一点

DashPathEffect dashPathEffect = new DashPathEffect(new float[]{1000.0f,9999999},0);
Paint.setPathEffect(dashPathEffect);

将长度更改为1000(Dash中的“on”部分)

并将99999999设置为最大值(Dash中的“关闭”部分)

玩这个参数,请阅读此article

android - 使用getSegment逐步绘制路径-LMLPHP

08-04 08:38