PolygonShapeScope
-
Cmn
sealed interface PolygonShapeScope : Density
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(Creates polygon geometry from |
Cmn
|
PolygonShapeGeometry |
polygon(Creates polygon geometry from |
Cmn
|
PolygonShapeGeometry |
polygon(Creates a regular polygon with |
Cmn
|
PolygonShapeGeometry |
polygon(Creates a regular polygon with |
Cmn
|
Public properties |
||
|---|---|---|
LayoutDirection |
Layout direction of the shape. |
Cmn
|
Size |
Size of the shape's layout container in pixels. |
Cmn
|
Inherited functions |
|||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|||||||||||||||||||||||||||||||||
Inherited properties |
|---|
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 |
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 |
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 |
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 |
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 |
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 |