Receiver scope for the PolygonShape builder lambda.

Provides the resolution inputs (size, layoutDirection, and Density) and the factories that create the PolygonShapeGeometry the lambda returns. Coordinates are in pixels in the layout's coordinate space; Dp values resolve with the current density, and percent values are size-proportional.

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.PolygonShape
import androidx.compose.foundation.shape.PolygonShapeGeometry.Companion.CornerRounding
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(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.PolygonShape
import androidx.compose.foundation.shape.PolygonShapeGeometry.Companion.CornerRounding
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(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: PolygonShapeGeometry.CornerRounding
)

Creates polygon geometry from vertices in pixels, with the same rounding at every vertex.

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

Creates polygon geometry from vertices in pixels, with a separate rounding for each vertex.

Cmn
PolygonShapeGeometry
polygon(
    numVertices: Int,
    perVertexRounding: List<PolygonShapeGeometry.CornerRounding>,
    radius: Float,
    center: Offset
)

Creates a regular polygon with numVertices vertices on a circle of radius pixels around center, with a separate rounding for each vertex.

Cmn
PolygonShapeGeometry
polygon(
    numVertices: Int,
    radius: Float,
    center: Offset,
    rounding: PolygonShapeGeometry.CornerRounding
)

Creates a regular polygon with numVertices vertices on a circle of radius pixels around center, with the same rounding at every vertex.

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: PolygonShapeGeometry.CornerRounding = CornerRounding.Unrounded
): PolygonShapeGeometry

Creates polygon geometry from vertices in pixels, with the same rounding at every vertex.

Rounding resolves against the vertex bounds, not the container. A percent radius is a percentage of the bounds' smaller dimension, a Dp radius resolves with the current density, and a float radius is a length in pixels.

Parameters
vertices: List<Offset>

vertex positions in pixels, at least 3

center: Offset = Offset.Unspecified

polygon center in pixels, computed from the geometry when unspecified. An explicit center can be specified as a custom transformation or morphing anchor.

rounding: PolygonShapeGeometry.CornerRounding = CornerRounding.Unrounded

rounding applied to every vertex

Throws
IllegalArgumentException

if vertices has fewer than 3 entries

polygon

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

Creates polygon geometry from vertices in pixels, with a separate rounding for each vertex.

Rounding resolves against the vertex bounds, not the container. A percent radius is a percentage of the bounds' smaller dimension, a Dp radius resolves with the current density, and a float radius is a length in pixels.

Parameters
vertices: List<Offset>

vertex positions in pixels, at least 3

perVertexRounding: List<PolygonShapeGeometry.CornerRounding>

rounding for each vertex, matching vertices in size

center: Offset = Offset.Unspecified

polygon center in pixels, computed from the geometry when unspecified. An explicit center can be specified as a custom transformation or morphing anchor.

Throws
IllegalArgumentException

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

polygon

fun polygon(
    numVertices: Int,
    perVertexRounding: List<PolygonShapeGeometry.CornerRounding>,
    radius: Float = size.minDimension / 2f,
    center: Offset = size.center
): PolygonShapeGeometry

Creates a regular polygon with numVertices vertices on a circle of radius pixels around center, with a separate rounding for each vertex.

Rounding resolves against the polygon itself, not the container. A percent radius is a percentage of radius, a Dp radius resolves with the current density, and a float radius is a length in pixels. Vertex i of perVertexRounding rounds the vertex at angle 2πi/numVertices on the generating circle.

Parameters
numVertices: Int

number of vertices, at least 3

perVertexRounding: List<PolygonShapeGeometry.CornerRounding>

rounding for each vertex, matching numVertices in size

radius: Float = size.minDimension / 2f

radius in pixels of the circle the vertices are placed on

center: Offset = size.center

center of the polygon in pixels, defaults to the container center

Throws
IllegalArgumentException

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

polygon

fun polygon(
    numVertices: Int,
    radius: Float = size.minDimension / 2f,
    center: Offset = size.center,
    rounding: PolygonShapeGeometry.CornerRounding = CornerRounding.Unrounded
): PolygonShapeGeometry

Creates a regular polygon with numVertices vertices on a circle of radius pixels around center, with the same rounding at every vertex.

Rounding resolves against the polygon itself, not the container. A percent radius is a percentage of radius, a Dp radius resolves with the current density, and a float radius is a length in pixels.

Parameters
numVertices: Int

number of vertices, at least 3

radius: Float = size.minDimension / 2f

radius in pixels of the circle the vertices are placed on

center: Offset = size.center

center of the polygon in pixels, defaults to the container center

rounding: PolygonShapeGeometry.CornerRounding = CornerRounding.Unrounded

rounding applied to every vertex

Throws
IllegalArgumentException

if numVertices is less than 3

Public properties

layoutDirection

val layoutDirectionLayoutDirection

Layout direction of the shape.

size

val sizeSize

Size of the shape's layout container in pixels.