androidx.graphics.shapes


Interfaces

MutablePoint

This interface is used refer to Points that can be modified, as a scope to PointTransformer

Cmn
PointTransformer

Interface for a function that can transform (rotate/scale/translate/etc.) points.

Cmn

Classes

CornerRounding

CornerRounding defines the amount and quality around a given vertex of a shape.

Cmn
Cubic

This class holds the anchor and control point data for a single cubic Bézier curve, with anchor points (anchor0X, anchor0Y) and (anchor1X, anchor1Y) at either end and control points (control0X, control0Y) and (control1X, control1Y) determining the slope of the curve between the anchor points.

Cmn
Morph

This class is used to animate between start and end polygons objects.

Cmn
MutableCubic

This is a Mutable version of Cubic, used mostly for performance critical paths so we can avoid creating new Cubics

Cmn
RoundedPolygon

The RoundedPolygon class allows simple construction of polygonal shapes with optional rounding at the vertices.

Cmn

Objects

Type aliases

Top-level functions summary

Cubic
Cubic(
    anchor0X: Float,
    anchor0Y: Float,
    control0X: Float,
    control0Y: Float,
    control1X: Float,
    control1Y: Float,
    anchor1X: Float,
    anchor1Y: Float
)

Create a Cubic that holds the anchor and control point data for a single Bézier curve, with anchor points (anchor0X, anchor0Y) and (anchor1X, anchor1Y) at either end and control points (control0X, control0Y) and (control1X, control1Y) determining the slope of the curve between the anchor points.

Cmn
RoundedPolygon

Creates a copy of the given RoundedPolygon

Cmn
RoundedPolygon
@<Error class: unknown class>
RoundedPolygon(
    vertices: FloatArray,
    rounding: CornerRounding,
    perVertexRounding: List<CornerRounding>?,
    centerX: Float,
    centerY: Float
)

This function takes the vertices (either supplied or calculated, depending on the constructor called), plus CornerRounding parameters, and creates the actual RoundedPolygon shape, rounding around the vertices (or not) as specified.

Cmn
RoundedPolygon
@<Error class: unknown class>
RoundedPolygon(
    numVertices: @IntRange(from = 3) Int,
    radius: Float,
    centerX: Float,
    centerY: Float,
    rounding: CornerRounding,
    perVertexRounding: List<CornerRounding>?
)

This constructor takes the number of vertices in the resulting polygon.

Cmn

Extension functions summary

RoundedPolygon
@<Error class: unknown class>
RoundedPolygon.Companion.circle(
    numVertices: @IntRange(from = 3) Int,
    radius: Float,
    centerX: Float,
    centerY: Float
)

Creates a circular shape, approximating the rounding of the shape around the underlying polygon vertices.

Cmn
RoundedPolygon
@<Error class: unknown class>
RoundedPolygon.Companion.pill(
    width: Float,
    height: Float,
    smoothing: Float,
    centerX: Float,
    centerY: Float
)

A pill shape consists of a rectangle shape bounded by two semicircles at either of the long ends of the rectangle.

Cmn
RoundedPolygon
@<Error class: unknown class>
RoundedPolygon.Companion.pillStar(
    width: Float,
    height: Float,
    numVerticesPerRadius: Int,
    innerRadiusRatio: @FloatRange(from = 0.0, fromInclusive = false, to = 1.0, toInclusive = false) Float,
    rounding: CornerRounding,
    innerRounding: CornerRounding?,
    perVertexRounding: List<CornerRounding>?,
    vertexSpacing: @FloatRange(from = 0.0, to = 1.0) Float,
    centerX: Float,
    centerY: Float
)

A pillStar shape is like a pill except it has inner and outer radii along its pill-shaped outline, just like a star has inner and outer radii along its circular outline.

Cmn
RoundedPolygon
RoundedPolygon.Companion.rectangle(
    width: Float,
    height: Float,
    rounding: CornerRounding,
    perVertexRounding: List<CornerRounding>?,
    centerX: Float,
    centerY: Float
)

Creates a rectangular shape with the given width/height around the given center.

Cmn
RoundedPolygon
@<Error class: unknown class>
RoundedPolygon.Companion.star(
    numVerticesPerRadius: Int,
    radius: Float,
    innerRadius: Float,
    rounding: CornerRounding,
    innerRounding: CornerRounding?,
    perVertexRounding: List<CornerRounding>?,
    centerX: Float,
    centerY: Float
)

Creates a star polygon, which is like a regular polygon except every other vertex is on either an inner or outer radius.

Cmn
Path

Gets a Path representation for a RoundedPolygon shape.

android
Path
Morph.toPath(progress: Float, path: Path)
android
RoundedPolygon

Transforms a RoundedPolygon by the given matrix.

android

Top-level functions

fun Cubic(
    anchor0X: Float,
    anchor0Y: Float,
    control0X: Float,
    control0Y: Float,
    control1X: Float,
    control1Y: Float,
    anchor1X: Float,
    anchor1Y: Float
): Cubic

Create a Cubic that holds the anchor and control point data for a single Bézier curve, with anchor points (anchor0X, anchor0Y) and (anchor1X, anchor1Y) at either end and control points (control0X, control0Y) and (control1X, control1Y) determining the slope of the curve between the anchor points.

The returned instance is immutable.

Parameters
anchor0X: Float

the first anchor point x coordinate

anchor0Y: Float

the first anchor point y coordinate

control0X: Float

the first control point x coordinate

control0Y: Float

the first control point y coordinate

control1X: Float

the second control point x coordinate

control1Y: Float

the second control point y coordinate

anchor1X: Float

the second anchor point x coordinate

anchor1Y: Float

the second anchor point y coordinate

RoundedPolygon

fun RoundedPolygon(source: RoundedPolygon): RoundedPolygon

Creates a copy of the given RoundedPolygon

RoundedPolygon

@<Error class: unknown class>
fun RoundedPolygon(
    vertices: FloatArray,
    rounding: CornerRounding = CornerRounding.Unrounded,
    perVertexRounding: List<CornerRounding>? = null,
    centerX: Float = Float.MIN_VALUE,
    centerY: Float = Float.MIN_VALUE
): RoundedPolygon

This function takes the vertices (either supplied or calculated, depending on the constructor called), plus CornerRounding parameters, and creates the actual RoundedPolygon shape, rounding around the vertices (or not) as specified. The result is a list of Cubic curves which represent the geometry of the final shape.

Parameters
vertices: FloatArray

The list of vertices in this polygon specified as pairs of x/y coordinates in this FloatArray. This should be an ordered list (with the outline of the shape going from each vertex to the next in order of this list), otherwise the results will be undefined.

rounding: CornerRounding = CornerRounding.Unrounded

The CornerRounding properties of all vertices. If some vertices should have different rounding properties, then use perVertexRounding instead. The default rounding value is CornerRounding.Unrounded, meaning that the polygon will use the vertices themselves in the final shape and not curves rounded around the vertices.

perVertexRounding: List<CornerRounding>? = null

The CornerRounding properties of all vertices. If this parameter is not null, then it must have the same size as vertices. If this parameter is null, then the polygon will use the rounding parameter for every vertex instead. The default value is null.

centerX: Float = Float.MIN_VALUE

The X coordinate of the center of the polygon, around which all vertices will be placed. The default center is at (0,0).

centerY: Float = Float.MIN_VALUE

The Y coordinate of the center of the polygon, around which all vertices will be placed. The default center is at (0,0).

Throws
kotlin.IllegalArgumentException

if the number of vertices is less than 3 (the vertices parameter has less than 6 Floats). Or if the perVertexRounding parameter is not null and the size doesn't match the number vertices.

RoundedPolygon

@<Error class: unknown class>
fun RoundedPolygon(
    numVertices: @IntRange(from = 3) Int,
    radius: Float = 1.0f,
    centerX: Float = 0.0f,
    centerY: Float = 0.0f,
    rounding: CornerRounding = CornerRounding.Unrounded,
    perVertexRounding: List<CornerRounding>? = null
): RoundedPolygon

This constructor takes the number of vertices in the resulting polygon. These vertices are positioned on a virtual circle around a given center with each vertex positioned radius distance from that center, equally spaced (with equal angles between them). If no radius is supplied, the shape will be created with a default radius of 1, resulting in a shape whose vertices lie on a unit circle, with width/height of 2. That default polygon will probably need to be rescaled using transformed into the appropriate size for the UI in which it will be drawn.

The rounding and perVertexRounding parameters are optional. If not supplied, the result will be a regular polygon with straight edges and unrounded corners.

Parameters
numVertices: @IntRange(from = 3) Int

The number of vertices in this polygon.

radius: Float = 1.0f

The radius of the polygon, in pixels. This radius determines the initial size of the object, but it can be transformed later by using the transformed function.

centerX: Float = 0.0f

The X coordinate of the center of the polygon, around which all vertices will be placed. The default center is at (0,0).

centerY: Float = 0.0f

The Y coordinate of the center of the polygon, around which all vertices will be placed. The default center is at (0,0).

rounding: CornerRounding = CornerRounding.Unrounded

The CornerRounding properties of all vertices. If some vertices should have different rounding properties, then use perVertexRounding instead. The default rounding value is CornerRounding.Unrounded, meaning that the polygon will use the vertices themselves in the final shape and not curves rounded around the vertices.

perVertexRounding: List<CornerRounding>? = null

The CornerRounding properties of every vertex. If this parameter is not null, then it must have numVertices elements. If this parameter is null, then the polygon will use the rounding parameter for every vertex instead. The default value is null.

Throws
kotlin.IllegalArgumentException

If perVertexRounding is not null and its size is not equal to numVertices.

kotlin.IllegalArgumentException

numVertices must be at least 3.

Extension functions

@<Error class: unknown class>
fun RoundedPolygon.Companion.circle(
    numVertices: @IntRange(from = 3) Int = 8,
    radius: Float = 1.0f,
    centerX: Float = 0.0f,
    centerY: Float = 0.0f
): RoundedPolygon

Creates a circular shape, approximating the rounding of the shape around the underlying polygon vertices.

Parameters
numVertices: @IntRange(from = 3) Int = 8

The number of vertices in the underlying polygon with which to approximate the circle, default value is 8

radius: Float = 1.0f

optional radius for the circle, default value is 1.0

centerX: Float = 0.0f

X coordinate of optional center for the circle, default value is 0

centerY: Float = 0.0f

Y coordinate of optional center for the circle, default value is 0

Throws
kotlin.IllegalArgumentException

numVertices must be at least 3

@<Error class: unknown class>
fun RoundedPolygon.Companion.pill(
    width: Float = 2.0f,
    height: Float = 1.0f,
    smoothing: Float = 0.0f,
    centerX: Float = 0.0f,
    centerY: Float = 0.0f
): RoundedPolygon

A pill shape consists of a rectangle shape bounded by two semicircles at either of the long ends of the rectangle.

Parameters
width: Float = 2.0f

The width of the resulting shape.

height: Float = 1.0f

The height of the resulting shape.

smoothing: Float = 0.0f

the amount by which the arc is "smoothed" by extending the curve from the circular arc on each endcap to the edge between the endcaps. A value of 0 (no smoothing) indicates that the corner is rounded by only a circular arc.

centerX: Float = 0.0f

The X coordinate of the center of the polygon, around which all vertices will be placed. The default center is at (0,0).

centerY: Float = 0.0f

The Y coordinate of the center of the polygon, around which all vertices will be placed. The default center is at (0,0).

Throws
kotlin.IllegalArgumentException

if either width or height are <= 0.

@<Error class: unknown class>
fun RoundedPolygon.Companion.pillStar(
    width: Float = 2.0f,
    height: Float = 1.0f,
    numVerticesPerRadius: Int = 8,
    innerRadiusRatio: @FloatRange(from = 0.0, fromInclusive = false, to = 1.0, toInclusive = false) Float = 0.5f,
    rounding: CornerRounding = CornerRounding.Unrounded,
    innerRounding: CornerRounding? = null,
    perVertexRounding: List<CornerRounding>? = null,
    vertexSpacing: @FloatRange(from = 0.0, to = 1.0) Float = 0.5f,
    centerX: Float = 0.0f,
    centerY: Float = 0.0f
): RoundedPolygon

A pillStar shape is like a pill except it has inner and outer radii along its pill-shaped outline, just like a star has inner and outer radii along its circular outline. The parameters for a pillStar are similar to those of a star except, like pill, it has a width and height to determine the general shape of the underlying pill. Also, there is a subtle complication with the way that inner and outer vertices proceed along the circular ends of the shape, depending on the magnitudes of the rounding, innerRounding, and innerRadiusRatio parameters. For example, a shape with outer vertices that lie along the curved end outline will necessarily have inner vertices that are closer to each other, because of the curvature of that part of the shape. Conversely, if the inner vertices are lined up along the pill outline at the ends, then the outer vertices will be much further apart from each other.

The default approach, reflected by the default value of vertexSpacing, is to use the average of the outer and inner radii, such that each set of vertices falls equally to the other side of the pill outline on the curved ends. Depending on the values used for the various rounding and radius parameters, you may want to change that value to suit the look you want. A value of 0 for vertexSpacing is equivalent to aligning the inner vertices along the circular curve, and a value of 1 is equivalent to aligning the outer vertices along that curve.

Parameters
width: Float = 2.0f

The width of the resulting shape.

height: Float = 1.0f

The height of the resulting shape.

numVerticesPerRadius: Int = 8

The number of vertices along each of the two radii.

innerRadiusRatio: @FloatRange(from = 0.0, fromInclusive = false, to = 1.0, toInclusive = false) Float = 0.5f

Inner radius ratio for this star shape, must be greater than 0 and less than or equal to 1. Note that a value of 1 would be similar to creating a pill, but with more vertices. The default value is .5.

rounding: CornerRounding = CornerRounding.Unrounded

The CornerRounding properties of every vertex. If some vertices should have different rounding properties, then use perVertexRounding instead. The default rounding value is CornerRounding.Unrounded, meaning that the polygon will use the vertices themselves in the final shape and not curves rounded around the vertices.

innerRounding: CornerRounding? = null

Optional rounding parameters for the vertices on the innerRadiusRatio. If null (the default value), inner vertices will use the rounding or perVertexRounding parameters instead.

perVertexRounding: List<CornerRounding>? = null

The CornerRounding properties of every vertex. If this parameter is not null, then it must have the same size as 2 * numVerticesPerRadius. If this parameter is null, then the polygon will use the rounding parameter for every vertex instead. The default value is null.

vertexSpacing: @FloatRange(from = 0.0, to = 1.0) Float = 0.5f

This factor determines how the vertices on the circular ends are laid out along the outline. A value of 0 aligns spaces the inner vertices the same as those along the straight edges, with the outer vertices then being spaced further apart. A value of 1 does the opposite, with the outer vertices spaced the same as the vertices on the straight edges. The default value is .5, which takes the average of these two extremes.

centerX: Float = 0.0f

The X coordinate of the center of the polygon, around which all vertices will be placed. The default center is at (0,0).

centerY: Float = 0.0f

The Y coordinate of the center of the polygon, around which all vertices will be placed. The default center is at (0,0).

Throws
kotlin.IllegalArgumentException

if either width or height are <= 0 or if innerRadiusRatio is outside the range of (0, 1].

fun RoundedPolygon.Companion.rectangle(
    width: Float = 2.0f,
    height: Float = 2.0f,
    rounding: CornerRounding = CornerRounding.Unrounded,
    perVertexRounding: List<CornerRounding>? = null,
    centerX: Float = 0.0f,
    centerY: Float = 0.0f
): RoundedPolygon

Creates a rectangular shape with the given width/height around the given center. Optional rounding parameters can be used to create a rounded rectangle instead.

As with all RoundedPolygon objects, if this shape is created with default dimensions and center, it is sized to fit within the 2x2 bounding box around a center of (0, 0) and will need to be scaled and moved using RoundedPolygon.transformed to fit the intended area in a UI.

Parameters
width: Float = 2.0f

The width of the rectangle, default value is 2

height: Float = 2.0f

The height of the rectangle, default value is 2

rounding: CornerRounding = CornerRounding.Unrounded

The CornerRounding properties of every vertex. If some vertices should have different rounding properties, then use perVertexRounding instead. The default rounding value is CornerRounding.Unrounded, meaning that the polygon will use the vertices themselves in the final shape and not curves rounded around the vertices.

perVertexRounding: List<CornerRounding>? = null

The CornerRounding properties of every vertex. If this parameter is not null, then it must be of size 4 for the four corners of the shape. If this parameter is null, then the polygon will use the rounding parameter for every vertex instead. The default value is null.

centerX: Float = 0.0f

The X coordinate of the center of the rectangle, around which all vertices will be placed equidistantly. The default center is at (0,0).

centerY: Float = 0.0f

The X coordinate of the center of the rectangle, around which all vertices will be placed equidistantly. The default center is at (0,0).

@<Error class: unknown class>
fun RoundedPolygon.Companion.star(
    numVerticesPerRadius: Int,
    radius: Float = 1.0f,
    innerRadius: Float = 0.5f,
    rounding: CornerRounding = CornerRounding.Unrounded,
    innerRounding: CornerRounding? = null,
    perVertexRounding: List<CornerRounding>? = null,
    centerX: Float = 0.0f,
    centerY: Float = 0.0f
): RoundedPolygon

Creates a star polygon, which is like a regular polygon except every other vertex is on either an inner or outer radius. The two radii specified in the constructor must both both nonzero. If the radii are equal, the result will be a regular (not star) polygon with twice the number of vertices specified in numVerticesPerRadius.

Parameters
numVerticesPerRadius: Int

The number of vertices along each of the two radii.

radius: Float = 1.0f

Outer radius for this star shape, must be greater than 0. Default value is 1.

innerRadius: Float = 0.5f

Inner radius for this star shape, must be greater than 0 and less than or equal to radius. Note that equal radii would be the same as creating a RoundedPolygon directly, but with 2 * numVerticesPerRadius vertices. Default value is .5.

rounding: CornerRounding = CornerRounding.Unrounded

The CornerRounding properties of every vertex. If some vertices should have different rounding properties, then use perVertexRounding instead. The default rounding value is CornerRounding.Unrounded, meaning that the polygon will use the vertices themselves in the final shape and not curves rounded around the vertices.

innerRounding: CornerRounding? = null

Optional rounding parameters for the vertices on the innerRadius. If null (the default value), inner vertices will use the rounding or perVertexRounding parameters instead.

perVertexRounding: List<CornerRounding>? = null

The CornerRounding properties of every vertex. If this parameter is not null, then it must have the same size as 2 * numVerticesPerRadius. If this parameter is null, then the polygon will use the rounding parameter for every vertex instead. The default value is null.

centerX: Float = 0.0f

The X coordinate of the center of the polygon, around which all vertices will be placed. The default center is at (0,0).

centerY: Float = 0.0f

The Y coordinate of the center of the polygon, around which all vertices will be placed. The default center is at (0,0).

fun RoundedPolygon.toPath(path: Path = Path()): Path

Gets a Path representation for a RoundedPolygon shape. Note that there is some rounding happening (to the nearest thousandth), to work around rendering artifacts introduced by some points being just slightly off from each other (far less than a pixel). This also allows for a more optimal path, as redundant curves (usually a single point) can be detected and not added to the resulting path.

Parameters
path: Path = Path()

an optional Path object which, if supplied, will avoid the function having to create a new Path object

fun Morph.toPath(progress: Float, path: Path = Path()): Path

transformed

fun RoundedPolygon.transformed(matrix: Matrix): RoundedPolygon

Transforms a RoundedPolygon by the given matrix.

Parameters
matrix: Matrix

The matrix by which the polygon is to be transformed