PolygonShapeGeometry


Defines a polygon's vertices, center, and corner 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)).

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.foundation.shape.PolygonShapeGeometry
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 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)))

Summary

Public constructors

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

Creates polygon geometry with uniform rounding at every vertex.

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

Creates polygon geometry with per-vertex perVertexRounding.

Cmn

Public functions

open operator Boolean
equals(other: Any?)
Cmn
open Int
Cmn
open String
Cmn

Public properties

Offset

polygon center in pixels, or Offset.Unspecified when computed from vertices

Cmn
List<CornerRounding>?

optional per-vertex rounding matching vertices in size

Cmn
CornerRounding

uniform vertex rounding, or CornerRounding.Unrounded when using perVertexRounding

Cmn
List<Offset>

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

Cmn

Public constructors

PolygonShapeGeometry

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

Creates polygon geometry with uniform rounding at every vertex.

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

PolygonShapeGeometry

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

Creates polygon geometry with per-vertex perVertexRounding.

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

Public functions

equals

open operator fun equals(other: Any?): Boolean

hashCode

open fun hashCode(): Int

toString

open fun toString(): String

Public properties

center

val center: Offset

polygon center in pixels, or Offset.Unspecified when computed from vertices

perVertexRounding

val perVertexRounding: List<CornerRounding>?

optional per-vertex rounding matching vertices in size

rounding

val rounding: CornerRounding

uniform vertex rounding, or CornerRounding.Unrounded when using perVertexRounding

vertices

val vertices: List<Offset>

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