Builds PolygonShapeGeometry for a PolygonShape at layout resolution.

Exposes size, layoutDirection, and Density along with polygon factories. Coordinates are in layout pixels.

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CornerRounding
import androidx.compose.foundation.shape.PolygonShape
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

// A regular hexagon sized to the container's smaller dimension with an exact 16.dp corner
// rounding resolved against the current density.
val hexagon = remember {
    PolygonShape {
        polygon(
            numVertices = 6,
            radius = size.minDimension / 2f,
            center = Offset(size.width / 2f, size.height / 2f),
            rounding = CornerRounding.dp(radius = 16.dp),
        )
    }
}
Box(Modifier.size(96.dp).clip(hexagon).background(Color(0xFF3F51B5)))
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CornerRounding
import androidx.compose.foundation.shape.PolygonShape
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp

// A pointed tag whose arrow tip adapts to the layout size and flips horizontally in RTL.
val tag = remember {
    PolygonShape {
        val isLtr = layoutDirection == LayoutDirection.Ltr
        val tipX = if (isLtr) size.width else 0f
        val baseX = if (isLtr) 0f else size.width
        val indentX = if (isLtr) size.width * 0.2f else size.width * 0.8f
        polygon(
            vertices =
                listOf(
                    Offset(baseX, 0f),
                    Offset(tipX, size.height / 2f),
                    Offset(baseX, size.height),
                    Offset(indentX, size.height / 2f),
                ),
            rounding = CornerRounding.dp(radius = 6.dp),
        )
    }
}
Box(Modifier.size(width = 120.dp, height = 64.dp).clip(tag).background(Color(0xFF4CAF50)))

Summary

Public functions

PolygonShapeGeometry
polygon(vertices: List<Offset>, center: Offset, rounding: CornerRounding)

Creates polygon geometry from vertices with uniform rounding.

Cmn
PolygonShapeGeometry
polygon(
    vertices: List<Offset>,
    perVertexRounding: List<CornerRounding>,
    center: Offset
)

Creates polygon geometry from vertices with per-vertex perVertexRounding.

Cmn
PolygonShapeGeometry
polygon(
    numVertices: @IntRange(from = 3) Int,
    perVertexRounding: List<CornerRounding>,
    radius: @FloatRange(from = 0.0, fromInclusive = false) Float,
    center: Offset
)

Creates a regular polygon with numVertices vertices and per-vertex perVertexRounding.

Cmn
PolygonShapeGeometry
polygon(
    numVertices: @IntRange(from = 3) Int,
    radius: @FloatRange(from = 0.0, fromInclusive = false) Float,
    center: Offset,
    rounding: CornerRounding
)

Creates a regular polygon with numVertices vertices on a circle of radius pixels.

Cmn

Public properties

LayoutDirection

Layout direction of the shape.

Cmn
Size

Size of the shape's layout container in pixels.

Cmn

Inherited functions

From androidx.compose.ui.unit.Density
open Int

Convert Dp to Int by rounding

Cmn
open Int

Convert Sp to Int by rounding

Cmn
open Dp

Convert an Int pixel value to Dp.

Cmn
open Dp

Convert a Float pixel value to a Dp

Cmn
open DpSize

Convert a Size to a DpSize.

Cmn
open Float

Convert Dp to pixels.

Cmn
open Float

Convert Sp to pixels.

Cmn
open Rect

Convert a DpRect to a Rect.

Cmn
open Size

Convert a DpSize to a Size.

Cmn
open TextUnit

Convert an Int pixel value to Sp.

Cmn
open TextUnit

Convert a Float pixel value to a Sp

Cmn
From androidx.compose.ui.unit.FontScaling
Dp

Convert Sp to Dp.

Cmn
TextUnit

Convert Dp to Sp.

Cmn

Inherited properties

From androidx.compose.ui.unit.Density
Float

The logical density of the display.

Cmn
From androidx.compose.ui.unit.FontScaling
Float

Current user preference for the scaling factor for fonts.

Cmn

Public functions

polygon

fun polygon(
    vertices: List<Offset>,
    center: Offset = Offset.Unspecified,
    rounding: CornerRounding = CornerRounding.Unrounded
): PolygonShapeGeometry

Creates polygon geometry from vertices with uniform rounding.

Coordinates are in layout pixels ((0f, 0f) to (size.width, size.height)). You can also define vertices in a custom coordinate space (such as a [0f, 1f] unit square) and scale them to the layout bounds using PolygonShapeTransformScope.scaleToFit. Because custom vertices do not have a circle radius, CornerRounding.fraction sets the corner radius as a fraction of the polygon's smaller dimension (min(width, height)).

Parameters
vertices: List<Offset>

vertex positions in pixels (or custom coordinates scaled with PolygonShapeTransformScope.scaleToFit), at least 3

center: Offset = Offset.Unspecified

polygon center in pixels, or Offset.Unspecified to compute from vertices

rounding: CornerRounding = CornerRounding.Unrounded

corner rounding applied to every vertex

Throws
IllegalArgumentException

if vertices has fewer than 3 entries

polygon

fun polygon(
    vertices: List<Offset>,
    perVertexRounding: List<CornerRounding>,
    center: Offset = Offset.Unspecified
): PolygonShapeGeometry

Creates polygon geometry from vertices with per-vertex perVertexRounding.

Coordinates are in layout pixels ((0f, 0f) to (size.width, size.height)). You can also define vertices in a custom coordinate space (such as a [0f, 1f] unit square) and scale them to the layout bounds using PolygonShapeTransformScope.scaleToFit. Because custom vertices do not have a circle radius, CornerRounding.fraction sets the corner radius as a fraction of the polygon's smaller dimension (min(width, height)).

Parameters
vertices: List<Offset>

vertex positions in pixels (or custom coordinates scaled with PolygonShapeTransformScope.scaleToFit), at least 3

perVertexRounding: List<CornerRounding>

rounding for each vertex, matching vertices in size

center: Offset = Offset.Unspecified

polygon center in pixels, or Offset.Unspecified to compute from vertices

Throws
IllegalArgumentException

if vertices has fewer than 3 entries or perVertexRounding differs from vertices in size

polygon

fun polygon(
    numVertices: @IntRange(from = 3) Int,
    perVertexRounding: List<CornerRounding>,
    radius: @FloatRange(from = 0.0, fromInclusive = false) Float = size.minDimension / 2f,
    center: Offset = size.center
): PolygonShapeGeometry

Creates a regular polygon with numVertices vertices and per-vertex perVertexRounding.

perVertexRounding applies to vertices in clockwise order starting from the right (0 degrees). CornerRounding.fraction sets the corner radius as a fraction of radius.

Parameters
numVertices: @IntRange(from = 3) Int

number of vertices, at least 3

perVertexRounding: List<CornerRounding>

rounding for each vertex, matching numVertices in size

radius: @FloatRange(from = 0.0, fromInclusive = false) Float = size.minDimension / 2f

circle radius in pixels

center: Offset = size.center

polygon center in pixels, defaults to Size.center

Throws
IllegalArgumentException

if numVertices is less than 3 or perVertexRounding differs from numVertices in size

polygon

fun polygon(
    numVertices: @IntRange(from = 3) Int,
    radius: @FloatRange(from = 0.0, fromInclusive = false) Float = size.minDimension / 2f,
    center: Offset = size.center,
    rounding: CornerRounding = CornerRounding.Unrounded
): PolygonShapeGeometry

Creates a regular polygon with numVertices vertices on a circle of radius pixels.

CornerRounding.fraction sets the corner radius as a fraction of radius.

Parameters
numVertices: @IntRange(from = 3) Int

number of vertices, at least 3

radius: @FloatRange(from = 0.0, fromInclusive = false) Float = size.minDimension / 2f

circle radius in pixels

center: Offset = size.center

polygon center in pixels, defaults to Size.center

rounding: CornerRounding = CornerRounding.Unrounded

corner rounding applied to every vertex

Throws
IllegalArgumentException

if numVertices is less than 3

Public properties

layoutDirection

val layoutDirection: LayoutDirection

Layout direction of the shape.

size

val size: Size

Size of the shape's layout container in pixels.