Ad

Draw With Canvas

- 1 answer

How to use canvas to draw a selected field of transparent color with blue background?

My header

In official documentation i find smth like this . But there is still a question of how to implement it.

Also for example, i tried to draw line:

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

And get smth like this: enter image description here

Ad

Answer

Decided to the next way:

 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)
}
Ad
source: stackoverflow.com
Ad