PolygonShapeTransformScope


Transforms a PolygonShape's geometry inside PolygonShape.transform.

Exposes the layout resolution inputs (size, layoutDirection, and Density). Operations (rotate, translate, scale, transform, and scaleToFit) concatenate onto the transformation matrix in the exact order they are called (for example, scale followed by translate differs from translate followed by scale).

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

// A square polygon rotated 45 degrees around its center and scaled into the layout bounds in a
// single pass so the rotated diamond stays inscribed without clipping.
val diamond = remember {
    PolygonShape { polygon(numVertices = 4, rounding = CornerRounding.fraction(0.15f)) }
        .apply {
            transform {
                rotate(degrees = 45f)
                scaleToFit(contentScale = ContentScale.Fit)
            }
        }
}
Box(Modifier.size(96.dp).clip(diamond).background(Color(0xFFE91E63)))
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
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.mutableStateOf
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

// Creates an unrotated badge shape and an independent rotated copy using copy() + transform {}.
val badge = remember {
    PolygonShape.star(
        numPoints = 8,
        innerRadiusRatio = 0.75f,
        outerRounding = CornerRounding.fraction(0.4f),
    )
}
val selectedBadge =
    remember(badge) { badge.copy().apply { transform { rotate(degrees = 22.5f) } } }
var selected by remember { mutableStateOf(false) }
val shape = if (selected) selectedBadge else badge
Box(
    Modifier.size(96.dp).clip(shape).background(Color(0xFF00695C)).clickable {
        selected = !selected
    }
)

Summary

Public functions

Unit
rotate(degrees: Float, pivot: Offset)

Rotates the polygon clockwise by degrees around pivot.

Cmn
Unit
scale(scaleX: Float, scaleY: Float, pivot: Offset)

Scales the polygon by scaleX horizontally and scaleY vertically around pivot.

Cmn
Unit
scaleToFit(contentScale: ContentScale, alignment: Alignment)

Scales and aligns the polygon's bounding box into size in call order.

Cmn
Unit
transform(matrix: Matrix)

Multiplies the current transformation by the affine matrix around the polygon's center.

Cmn
Unit
translate(x: Float, y: Float)

Translates the polygon by (x, y) 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

rotate

fun rotate(degrees: Float, pivot: Offset = Offset.Unspecified): Unit

Rotates the polygon clockwise by degrees around pivot.

Parameters
degrees: Float

clockwise rotation in degrees

pivot: Offset = Offset.Unspecified

rotation center in pixels, or Offset.Unspecified for the polygon's center

scale

fun scale(scaleX: Float, scaleY: Float = scaleX, pivot: Offset = Offset.Unspecified): Unit

Scales the polygon by scaleX horizontally and scaleY vertically around pivot.

Parameters
scaleX: Float

horizontal scale factor

scaleY: Float = scaleX

vertical scale factor, defaults to scaleX for uniform scaling

pivot: Offset = Offset.Unspecified

scale center in pixels, or Offset.Unspecified for the polygon's center

scaleToFit

fun scaleToFit(
    contentScale: ContentScale = ContentScale.Fit,
    alignment: Alignment = Alignment.Center
): Unit

Scales and aligns the polygon's bounding box into size in call order.

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

// Inside a wide container the star keeps its aspect ratio and is centered; passing
// ContentScale.FillBounds instead would stretch it to fill the container.
val star = remember {
    PolygonShape.star(
            numPoints = 5,
            innerRadiusRatio = 0.5f,
            outerRounding = CornerRounding.fraction(0.3f),
        )
        .apply { transform { scaleToFit(contentScale = ContentScale.Fit) } }
}
Box(Modifier.size(width = 160.dp, height = 64.dp).clip(star).background(Color(0xFF9C27B0)))
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)))
Parameters
contentScale: ContentScale = ContentScale.Fit

policy for scaling the polygon's bounding box into the layout bounds

alignment: Alignment = Alignment.Center

placement of the scaled polygon within the layout bounds

transform

fun transform(matrix: Matrix): Unit

Multiplies the current transformation by the affine matrix around the polygon's center.

Linear components (rotation, scale, skew) pivot around the polygon's center, while translation components offset the polygon directly.

Parameters
matrix: Matrix

affine transformation matrix to multiply

Throws
IllegalArgumentException

if matrix contains perspective components

translate

fun translate(x: Float = 0.0f, y: Float = 0.0f): Unit

Translates the polygon by (x, y) pixels.

Parameters
x: Float = 0.0f

horizontal offset in pixels

y: Float = 0.0f

vertical offset in pixels

Public properties

layoutDirection

val layoutDirection: LayoutDirection

Layout direction of the shape.

size

val size: Size

Size of the shape's layout container in pixels.