PolygonShapeGeometry


Geometry describing a polygon's vertices, center, and rounding.

The consumer defines the coordinate space. PolygonShapeScope factories create geometry in pixels in the layout's coordinate space, while geometry passed directly to PolygonShape uses an author-chosen space and is scaled into the layout bounds at resolution time.

Rounding resolves in the vertex coordinate space. A float radius is a length in that space, and a percent radius resolves against the smaller dimension of the vertex bounds.

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
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 kite authored in the unit square [0..1] with per-vertex rounding in the same coordinate
// space. The shape scales and centers into any container preserving its aspect ratio.
val kite = remember {
    PolygonShape(
        PolygonShapeGeometry(
            vertices =
                listOf(Offset(0.5f, 0f), Offset(1f, 0.4f), Offset(0.5f, 1f), Offset(0f, 0.4f)),
            perVertexRounding =
                listOf(
                    CornerRounding(0.1f),
                    CornerRounding(0.15f),
                    CornerRounding(0.05f),
                    CornerRounding(0.15f),
                ),
        )
    )
}

Box(Modifier.size(width = 120.dp, height = 84.dp).clip(kite).background(Color(0xFF673AB7)))

Summary

Nested types

Amount and quality of rounding around a polygon vertex.

Public companion functions

PolygonShapeGeometry.CornerRounding
CornerRounding(percent: @IntRange(from = 0, to = 100) Int, smoothing: Float)

Creates a CornerRounding with a radius as a percent of the geometry it applies to.

Cmn
PolygonShapeGeometry.CornerRounding
CornerRounding(radius: Dp, smoothing: Float)

Creates a CornerRounding with a radius in Dp, resolved with the shape's density.

Cmn
PolygonShapeGeometry.CornerRounding
CornerRounding(radius: Float, smoothing: Float)

Creates a CornerRounding with a radius in the same coordinate space as the vertices.

Cmn

Public constructors

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

Creates polygon geometry with the same rounding at every vertex.

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

Creates polygon geometry with a separate rounding for each vertex.

Cmn

Public functions

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

Public properties

Offset

polygon center, computed from the geometry when unspecified.

Cmn
List<PolygonShapeGeometry.CornerRounding>?

optional per-vertex rounding, matching vertices in size

Cmn
PolygonShapeGeometry.CornerRounding

rounding applied to every vertex, CornerRounding.Unrounded for geometry created with per-vertex rounding

Cmn
List<Offset>

vertex positions, at least 3

Cmn

Public companion functions

CornerRounding

fun CornerRounding(
    percent: @IntRange(from = 0, to = 100) Int,
    smoothing: Float = 0.0f
): PolygonShapeGeometry.CornerRounding

Creates a CornerRounding with a radius as a percent of the geometry it applies to.

The reference is the generating radius for regular polygons and stars, or the smaller dimension of the vertex bounds for vertex-list geometry, so the radius scales with the shape rather than with the layout container.

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.graphics.Color
import androidx.compose.ui.unit.dp

// A pentagon whose corners round to 20% of its generating radius, so the rounding scales
// proportionally with the shape at any size.
val pentagon = remember {
    PolygonShape.regularPolygon(numVertices = 5, rounding = CornerRounding(percent = 20))
}
Box(Modifier.size(96.dp).clip(pentagon).background(Color(0xFF009688)))
Parameters
percent: @IntRange(from = 0, to = 100) Int

rounding radius as a percentage of the geometry, in the range 0 to 100

smoothing: Float = 0.0f

the amount by which the arc is "smoothed" by extending the curve from the inner circular arc to the edge between vertices. A value of 0 (no smoothing) indicates that the corner is rounded by only a circular arc; there are no flanking curves. A value of 1 indicates that there is no circular arc in the center; the flanking curves on either side meet at the middle.

Throws
IllegalArgumentException

if percent is outside the range 0 to 100 or smoothing is outside the range 0 to 1

CornerRounding

fun CornerRounding(radius: Dp, smoothing: Float = 0.0f): PolygonShapeGeometry.CornerRounding

Creates a CornerRounding with a radius in Dp, resolved with the shape's density.

Parameters
radius: Dp

rounding radius of the corner, at least 0

smoothing: Float = 0.0f

the amount by which the arc is "smoothed" by extending the curve from the inner circular arc to the edge between vertices. A value of 0 (no smoothing) indicates that the corner is rounded by only a circular arc; there are no flanking curves. A value of 1 indicates that there is no circular arc in the center; the flanking curves on either side meet at the middle.

Throws
IllegalArgumentException

if radius is negative or unspecified, or smoothing is outside the range 0 to 1

CornerRounding

fun CornerRounding(radius: Float, smoothing: Float = 0.0f): PolygonShapeGeometry.CornerRounding

Creates a CornerRounding with a radius in the same coordinate space as the vertices.

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
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 kite authored in the unit square [0..1] with per-vertex rounding in the same coordinate
// space. The shape scales and centers into any container preserving its aspect ratio.
val kite = remember {
    PolygonShape(
        PolygonShapeGeometry(
            vertices =
                listOf(Offset(0.5f, 0f), Offset(1f, 0.4f), Offset(0.5f, 1f), Offset(0f, 0.4f)),
            perVertexRounding =
                listOf(
                    CornerRounding(0.1f),
                    CornerRounding(0.15f),
                    CornerRounding(0.05f),
                    CornerRounding(0.15f),
                ),
        )
    )
}

Box(Modifier.size(width = 120.dp, height = 84.dp).clip(kite).background(Color(0xFF673AB7)))
Parameters
radius: Float

rounding radius in the vertex coordinate space, at least 0

smoothing: Float = 0.0f

the amount by which the arc is "smoothed" by extending the curve from the inner circular arc to the edge between vertices. A value of 0 (no smoothing) indicates that the corner is rounded by only a circular arc; there are no flanking curves. A value of 1 indicates that there is no circular arc in the center; the flanking curves on either side meet at the middle.

Throws
IllegalArgumentException

if radius is negative or smoothing is outside the range 0 to 1

Public constructors

PolygonShapeGeometry

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

Creates polygon geometry with the same rounding at every vertex.

Parameters
vertices: List<Offset>

vertex positions, at least 3

center: Offset = Offset.Unspecified

polygon center, computed from the geometry when unspecified. An explicit center can be provided 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

PolygonShapeGeometry

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

Creates polygon geometry with a separate rounding for each vertex.

Parameters
vertices: List<Offset>

vertex positions, at least 3

perVertexRounding: List<PolygonShapeGeometry.CornerRounding>

rounding for each vertex, matching vertices in size

center: Offset = Offset.Unspecified

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

Throws
IllegalArgumentException

if vertices has fewer than 3 entries, or if 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 centerOffset

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

perVertexRounding

val perVertexRoundingList<PolygonShapeGeometry.CornerRounding>?

optional per-vertex rounding, matching vertices in size

rounding

val roundingPolygonShapeGeometry.CornerRounding

rounding applied to every vertex, CornerRounding.Unrounded for geometry created with per-vertex rounding

vertices

val verticesList<Offset>

vertex positions, at least 3