用画布画

时间:2019-04-25 11:24:18

标签: android kotlin android-canvas

如何使用画布绘制具有蓝色背景的透明颜色的选定字段?

My header

在官方文档中,我发现类似this的东西。 但是仍然存在如何实现它的问题。

例如,我试图画线:

private fun someFun(canvas: Canvas, parent: RecyclerView) {
    val radius: Float = parent.context.resources.getDimension(R.dimen.dimen_12)
    val paint = Paint()
    paint.color = ContextCompat.getColor(parent.context, R.color.primary)
    paint.style = Paint.Style.FILL

    val path = Path()
    path.moveTo(0f, 0f)
    path.lineTo(0f, radius)
    path.arcTo(RectF(0f, 0f, radius * 2, radius * 2), 270f, -90f)
    path.close()
    canvas.drawPath(path, paint)
}

并获得如下信息: enter image description here

1 个答案:

答案 0 :(得分:0)

决定采用另一种方法:

 private fun drawRoundedCorners(canvas: Canvas, parent: RecyclerView) {
    val radius: Float = parent.context.resources.getDimension(R.dimen.dimen_12)
    val paint = Paint()
    paint.color = ContextCompat.getColor(parent.context, R.color.primary)
    paint.style = Paint.Style.FILL

    val path = Path()
    path.moveTo(0f, 0f)
    path.lineTo(150f, 0f)
    path.arcTo(RectF(0f, 0f, radius * 2, radius * 2), 270f, -90f)

    path.moveTo(parent.width.toFloat(), 0f)
    path.lineTo(parent.width.toFloat(), 0f)
    path.arcTo(RectF(parent.width.toFloat() - radius * 2, 0f, parent.width.toFloat(),
            radius * 2), 270f, 90f)
    path.close()
    canvas.drawPath(path, paint)
}