Function

PolygonShape

Creates a PolygonShape from geometry returned by builder.

CustomPolygonShapeSample

@Sampled
@Composable
fun CustomPolygonShapeSample() {
    // A diamond built from pixel coordinates derived from the resolved layout size, with an
    // exact 8dp corner rounding.
    val diamond = remember {
        PolygonShape {
            polygon(
                vertices =
                    listOf(
                        Offset(size.width / 2f, 0f),
                        Offset(size.width, size.height / 2f),
                        Offset(size.width / 2f, size.height),
                        Offset(0f, size.height / 2f),
                    ),
                rounding = CornerRounding.dp(radius = 8.dp),
            )
        }
    }
    Box(Modifier.size(96.dp).clip(diamond).background(Color(0xFFFF5722)))
}

UnitSpacePolygonShapeSample

@Sampled
@Composable
fun UnitSpacePolygonShapeSample() {
    // A kite authored in the unit square [0..1] with per-vertex rounding as fractions of the
    // bounds, scaled and centered into the container via transform { scaleToFit() }.
    val kite = remember {
        val geometry =
            PolygonShapeGeometry(
                vertices =
                    listOf(Offset(0.5f, 0f), Offset(1f, 0.4f), Offset(0.5f, 1f), Offset(0f, 0.4f)),
                perVertexRounding =
                    listOf(
                        CornerRounding.fraction(0.1f),
                        CornerRounding.fraction(0.15f),
                        CornerRounding.fraction(0.05f),
                        CornerRounding.fraction(0.15f),
                    ),
            )
        PolygonShape { geometry }.apply { transform { scaleToFit() } }
    }

    Box(Modifier.size(width = 120.dp, height = 84.dp).clip(kite).background(Color(0xFF673AB7)))
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: