Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

711
Views
How to draw rounded corner polygons in Jetpack Compose Canvas?

I'm trying to create a rounded triangle using Canvas in Jetpack Compose.

I try this code for drawing triangle:

@Composable
fun RoundedTriangle() {
    Canvas(modifier = Modifier.size(500.dp)) {
        val trianglePath = Path().apply {
            val height = size.height
            val width = size.width
            moveTo(width / 2.0f, 0f)
            lineTo(width, height)
            lineTo(0f, height)
        }
            drawPath(trianglePath, color = Color.Blue)
    }
}

But I don't know how to round the triangle corners. I also tried to use arcTo, but I was unable to get a suitable result.

How can I draw something like the figure below?

enter image description here

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

For Stroke you can specify rounding like this:

drawPath(
    ...
    style = Stroke(
        width = 2.dp.toPx(),
        pathEffect = PathEffect.cornerPathEffect(4.dp.toPx())
    )
)

Yet Fill seems lack of support rounding. I've created a feature request, please star it.

But Canvas has drawOutline function, which accepts both Outline, which can wrap a Path, and Paint, for which you can specify pathEffect:

Canvas(modifier = Modifier.fillMaxWidth().aspectRatio(1f)) {
    val rect = Rect(Offset.Zero, size)
    val trianglePath = Path().apply {
        moveTo(rect.topCenter)
        lineTo(rect.bottomRight)
        lineTo(rect.bottomLeft)
        close()
    }

    drawIntoCanvas { canvas ->
        canvas.drawOutline(
            outline = Outline.Generic(trianglePath),
            paint = Paint().apply {
                color = Color.Black
                pathEffect = PathEffect.cornerPathEffect(rect.maxDimension / 3)
            }
        )
    }
}

Path helpers:

fun Path.moveTo(offset: Offset) = moveTo(offset.x, offset.y)
fun Path.lineTo(offset: Offset) = lineTo(offset.x, offset.y)

Result:

over 4 years ago · Santiago Trujillo Report

0

Based on @philip-dukhov answer, if anyone is interested in appliying this to a square

@Composable
fun SquirclePath(
    modifier: Modifier,
    smoothingFactor: Int = 60,
    color: Color,
    strokeWidth: Float,
) {
    Canvas(
        modifier = modifier
    ) {
        val rect = Rect(Offset.Zero, size)
        val percent = smoothingFactor.percentOf(rect.minDimension)
        val squirclePath = Path().apply {
            with(rect) {
                lineTo(topRight)
                lineTo(bottomRight)
                lineTo(bottomLeft)
                lineTo(topLeft)
                // this is where the path is finally linked together
                close()
            }
        }

        drawIntoCanvas { canvas ->
            canvas.drawOutline(
                outline = Outline.Generic(squirclePath),
                paint = Paint().apply {
                    this.color = color
                    this.style = PaintingStyle.Fill
                    this.strokeWidth = strokeWidth
                    pathEffect = PathEffect.cornerPathEffect(percent)
                }
            )
        }
    }
}

fun Int.percentOf(target:Float) = (this.toFloat() / 100) * target
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!