androidx.graphics.shapes

Morph

Class · from androidx.graphics:graphics-shapes:1.1.0

No hand-written docs yet.

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

class Morph(
    start: RoundedPolygon,
    end: RoundedPolygon,
)

How to get one

Create it

Functions

calculateBounds

fun calculateBounds(
    bounds: FloatArray = FloatArray(4),
    approximate: Boolean = true,
): FloatArray

Calculates the axis-aligned bounds of the object.

calculateMaxBounds

fun calculateMaxBounds(
    bounds: FloatArray = FloatArray(4),
): FloatArray

Like calculateBounds, this function calculates the axis-aligned bounds of the object and returns that rectangle. But this function determines the max dimension of the shape (by calculating the distance from its center to the start and midpoint of each curve) and returns a square which can be used to hold the object in any rotation. This function can be used, for example, to calculate the max size of a UI element meant to hold this shape in any rotation.

asCubics

fun asCubics(progress: Float): List<Cubic>

Returns a representation of the morph object at a given progress value as a list of Cubics. Note that this function causes a new list to be created and populated, so there is some overhead.

forEachCubic

fun forEachCubic(
    progress: Float,
    mutableCubic: MutableCubic = MutableCubic(),
    callback: (MutableCubic) -> Unit,
)

Returns a representation of the morph object at a given progress value, iterating over the cubics and calling the callback. This function is faster than asCubics, since it doesn't allocate new Cubic instances, but to do this it reuses the same MutableCubic instance during iteration.

Library docs

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

Morphing between arbitrary objects can be problematic because it can be difficult to determine how the points of a given shape map to the points of some other shape. Morph simplifies the problem by only operating on RoundedPolygon objects, which are known to have similar, contiguous structures. For one thing, the shape of a polygon is contiguous from start to end (compared to an arbitrary Path object, which could have one or more moveTo operations in the shape). Also, all edges of a polygon shape are represented by Cubic objects, thus the start and end shapes use similar operations. Two Polygon shapes then only differ in the quantity and placement of their curves. The morph works by determining how to map the curves of the two shapes together (based on proximity and other information, such as distance to polygon vertices and concavity), and splitting curves when the shapes do not have the same number of curves or when the curve placement within the shapes is very different.