Google berkomitmen untuk mendorong terwujudnya keadilan rasial bagi komunitas Kulit Hitam. Lihat caranya.

androidx.ui.graphics

Interfaces

Canvas

ImageAsset

Graphics object that represents a 2 dimensional array of pixel information represented as ARGB values

Path

Shape

Defines a generic shape.

Classes

AndroidPath

Brush

CanvasHolder

Holder class that is used to issue scoped calls to a Canvas from the framework equivalent canvas without having to allocate an object on each draw call

Color

The Color class contains color information to be used while painting in Canvas.

ColorFilter

Creates a color filter that applies the blend mode given as the second argument.

LinearGradient

Brush implementation used to apply a linear gradient on a given Paint

Outline

Defines a simple shape, used for bounding graphical regions.

Paint

PixelMap

Result of a pixel read operation.

RadialGradient

Brush implementation used to apply a radial gradient on a given Paint

ShaderBrush

Brush implementation that wraps and applies a the provided shader to a Paint

Shadow

A single shadow.

SolidColor

Vertices

A set of vertex data used by Canvas.drawVertices.

Type-aliases

ColorStop

Enums

BlendMode

Algorithms to use when painting on the canvas.

ClipOp

Defines how a new clip region should be merged with the existing clip region.

FilterQuality

ImageAssetConfig

Possible ImageAsset configurations.

PaintingStyle

Strategies for painting shapes and paths on a canvas.

PathFillType

Determines the winding rule that decides how the interior of a Path is calculated.

PathOperation

Strategies for combining paths.

PointMode

Used by Canvas.drawPoints.

StrokeCap

StrokeJoin

TileMode

VertexMode

Defines how a list of points is interpreted when drawing a set of triangles.

Top-level constants summary

const Float

Default alpha value used on Paint.

Top-level functions summary

Canvas

Canvas

Create a new Canvas instance that targets its drawing commands to the provided ImageAsset

Color
Color(red: Float, green: Float, blue: Float, alpha: Float = 1f, colorSpace: ColorSpace = ColorSpaces.Srgb)

Create a Color by passing individual red, green, blue, alpha, and colorSpace components.

Color
Color(@ColorInt color: Int)

Creates a new Color instance from an ARGB color int.

Color
Color(color: Long)

Creates a new Color instance from an ARGB color int.

Color
Color(@IntRange(0, 255) red: Int, @IntRange(0, 255) green: Int, @IntRange(0, 255) blue: Int, @IntRange(0, 255) alpha: Int = 0xFF)

Creates a new Color instance from an ARGB color components.

LinearGradient
HorizontalGradient(colors: List<Color>, startX: Float, endX: Float, tileMode: TileMode = TileMode.Clamp)

Creates a horizontal gradient with the given colors evenly dispersed within the gradient

LinearGradient
HorizontalGradient(vararg colorStops: ColorStop, startX: Float, endX: Float, tileMode: TileMode = TileMode.Clamp)

Creates a horizontal gradient with the given colors dispersed at the provided offset defined in the ColorStop

ImageAsset
ImageAsset(width: Int, height: Int, config: ImageAssetConfig = ImageAssetConfig.Argb8888, hasAlpha: Boolean = true, colorSpace: ColorSpace = ColorSpaces.Srgb)

Shader
ImageShader(image: ImageAsset, tileModeX: TileMode = TileMode.Clamp, tileModeY: TileMode = TileMode.Clamp)

LinearGradient
LinearGradient(colors: List<Color>, startX: Float, startY: Float, endX: Float, endY: Float, tileMode: TileMode = TileMode.Clamp)

Creates a linear gradient with the provided colors along the given start and end coordinates.

LinearGradient
LinearGradient(vararg colorStops: ColorStop, startX: Float, startY: Float, endX: Float, endY: Float, tileMode: TileMode = TileMode.Clamp)

Creates a linear gradient with the provided colors along the given start and end coordinates.

Shader
LinearGradientShader(from: Offset, to: Offset, colors: List<Color>, colorStops: List<Float>? = null, tileMode: TileMode = TileMode.Clamp)

Creates a linear gradient from from to to.

actual Path

RadialGradient
RadialGradient(vararg colorStops: ColorStop, centerX: Float, centerY: Float, radius: Float, tileMode: TileMode = TileMode.Clamp)

Creates a radial gradient with the given colors at the provided offset defined in the ColorStop

RadialGradient
RadialGradient(colors: List<Color>, centerX: Float, centerY: Float, radius: Float, tileMode: TileMode = TileMode.Clamp)

Creates a radial gradient with the given colors evenly dispersed within the gradient

Shader
RadialGradientShader(center: Offset, radius: Float, colors: List<Color>, colorStops: List<Float>? = null, tileMode: TileMode = TileMode.Clamp)

Creates a radial gradient centered at center that ends at radius distance from the center.

LinearGradient
VerticalGradient(colors: List<Color>, startY: Float, endY: Float, tileMode: TileMode = TileMode.Clamp)

Creates a vertical gradient with the given colors evenly dispersed within the gradient Ex:

LinearGradient
VerticalGradient(vararg colorStops: ColorStop, startY: Float, endY: Float, tileMode: TileMode = TileMode.Clamp)

Creates a vertical gradient with the given colors at the provided offset defined in the ColorStop Ex:

ImageAsset

Create an ImageAsset from an image file stored in resources for the application

Color
lerp(start: Color, stop: Color, @FloatRange(0.0, 1.0) fraction: Float)

Linear interpolate between two Colors, start and stop with fraction fraction between the two.

Shadow
lerp(start: Shadow, stop: Shadow, fraction: Float)

Linearly interpolate two Shadows.

Top-level properties summary

Shape

A shape describing the rectangle.

Extension functions summary

For android.graphics.Bitmap
ImageAsset

Create an ImageAsset from the given Bitmap.

For android.graphics.Path
Path

Convert the android.graphics.Path instance into a compose compatible Path

For Path
Unit

Adds the outline to the Path.

Path

For ImageAsset
Bitmap

PixelMap
ImageAsset.toPixelMap(startX: Int = 0, startY: Int = 0, width: Int = this.width, height: Int = this.height, buffer: IntArray = IntArray(width * height), bufferOffset: Int = 0, stride: Int = width)

Convenience method to extract pixel information from the given ImageAsset into a PixelMap that supports for querying pixel information based on

For Color
Color
Color.compositeOver(background: Color)

Composites this color on top of background using the Porter-Duff 'source over' mode.

Float

Returns the relative luminance of this color.

Int

Converts this color to an ARGB color int.

Color
Color.useOrElse(block: () -> Color)

If this Color then this is returned, otherwise block is executed and its result is returned.

For DrawScope
Unit
DrawScope.drawOutline(outline: Outline, color: Color, @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DrawScope.DefaultBlendMode)

Draws the Outline on a DrawScope.

Unit
DrawScope.drawOutline(outline: Outline, brush: Brush, @FloatRange(0.0, 1.0) alpha: Float = 1.0f, style: DrawStyle = Fill, colorFilter: ColorFilter? = null, blendMode: BlendMode = DrawScope.DefaultBlendMode)

Draws the Outline on a DrawScope.

For Canvas
Unit
Canvas.drawOutline(outline: Outline, paint: Paint)

Draws the Outline on a Canvas.

Unit
Canvas.rotate(degrees: Float, pivotX: Float, pivotY: Float)

Add a rotation (in degrees clockwise) to the current transform at the given pivot point.

Unit
Canvas.rotateRad(radians: Float, pivotX: Float = 0.0f, pivotY: Float = 0.0f)

Add a rotation (in radians clockwise) to the current transform at the given pivot point.

Unit
Canvas.scale(sx: Float, sy: Float = sx, pivotX: Float, pivotY: Float)

Add an axis-aligned scale to the current transform, scaling by the first argument in the horizontal direction and the second in the vertical direction at the given pivot coordinate.

Unit
Canvas.withSave(block: () -> Unit)

Saves a copy of the current transform and clip on the save stack and executes the provided lambda with the current transform applied.

Unit
Canvas.withSaveLayer(bounds: Rect, paint: Paint, block: () -> Unit)

Saves a copy of the current transform and clip on the save stack, and then creates a new group which subsequent calls will become a part of.

For TileMode
actual NativeTileMode

For VertexMode
actual VertexMode

Extension properties summary

For Color
Boolean

false when this is Color.Unset.

Boolean

true when this is Color.Unset.

Top-level constants

DefaultAlpha

const val DefaultAlpha: Float

Default alpha value used on Paint. This value will draw source content fully opaque.

Value: 1.0f

Top-level functions

Canvas

fun Canvas(c: Canvas): Canvas

Canvas

fun Canvas(image: ImageAsset): Canvas

Create a new Canvas instance that targets its drawing commands to the provided ImageAsset

Color

@Stable fun Color(
    red: Float,
    green: Float,
    blue: Float,
    alpha: Float = 1f,
    colorSpace: ColorSpace = ColorSpaces.Srgb
): Color

Create a Color by passing individual red, green, blue, alpha, and colorSpace components. The default color space is SRGB and the default alpha is 1.0 (opaque). colorSpace must have a ColorSpace.componentCount of

Color

@Stable fun Color(@ColorInt color: Int): Color

Creates a new Color instance from an ARGB color int. The resulting color is in the sRGB color space.

Parameters
color: Int The ARGB color int to create a Color from.
Return
A non-null instance of {@link Color}

Color

@Stable fun Color(color: Long): Color

Creates a new Color instance from an ARGB color int. The resulting color is in the sRGB color space. This is useful for specifying colors with alpha greater than 0x80 in numeric form without using Long.toInt:

    val color = Color(0xFF000080)
Parameters
color: Long The 32-bit ARGB color int to create a Color from
Return
A non-null instance of {@link Color}

Color

@Stable fun Color(
    @IntRange(0, 255) red: Int,
    @IntRange(0, 255) green: Int,
    @IntRange(0, 255) blue: Int,
    @IntRange(0, 255) alpha: Int = 0xFF
): Color

Creates a new Color instance from an ARGB color components. The resulting color is in the sRGB color space. The default alpha value is 0xFF (opaque).

Return
A non-null instance of {@link Color}

HorizontalGradient

fun HorizontalGradient(
    colors: List<Color>,
    startX: Float,
    endX: Float,
    tileMode: TileMode = TileMode.Clamp
): LinearGradient

Creates a horizontal gradient with the given colors evenly dispersed within the gradient

Ex:

 HorizontalGradient(
     listOf(Color.Red, Color.Green, Color.Blue),
     startX = 10.0f,
     endX = 20.0f
)

HorizontalGradient

fun HorizontalGradient(
    vararg colorStops: ColorStop,
    startX: Float,
    endX: Float,
    tileMode: TileMode = TileMode.Clamp
): LinearGradient

Creates a horizontal gradient with the given colors dispersed at the provided offset defined in the ColorStop

Ex:

 HorizontalGradient(
     0.0f to Color.Red,
     0.3f to Color.Green,
     1.0f to Color.Blue,
     startX = 0.0f,
     endX = 100.0f
)

ImageAsset

fun ImageAsset(
    width: Int,
    height: Int,
    config: ImageAssetConfig = ImageAssetConfig.Argb8888,
    hasAlpha: Boolean = true,
    colorSpace: ColorSpace = ColorSpaces.Srgb
): ImageAsset

ImageShader

fun ImageShader(
    image: ImageAsset,
    tileModeX: TileMode = TileMode.Clamp,
    tileModeY: TileMode = TileMode.Clamp
): Shader

LinearGradient

fun LinearGradient(
    colors: List<Color>,
    startX: Float,
    startY: Float,
    endX: Float,
    endY: Float,
    tileMode: TileMode = TileMode.Clamp
): LinearGradient

Creates a linear gradient with the provided colors along the given start and end coordinates. The colors are

 LinearGradient(
     listOf(Color.Red, Color.Green, Color.Blue),
     startX = 0.0f,
     startY = 50.0f,
     endY = 0.0f,
     endY = 100.0f
)

LinearGradient

fun LinearGradient(
    vararg colorStops: ColorStop,
    startX: Float,
    startY: Float,
    endX: Float,
    endY: Float,
    tileMode: TileMode = TileMode.Clamp
): LinearGradient

Creates a linear gradient with the provided colors along the given start and end coordinates. The colors are dispersed at the provided offset defined in the ColorStop

 LinearGradient(
     0.0f to Color.Red,
     0.3f to Color.Green,
     1.0f to Color.Blue,
     startX = 0.0f,
     startY = 50.0f,
     endY = 0.0f,
     endY = 100.0f
)

LinearGradientShader

fun LinearGradientShader(
    from: Offset,
    to: Offset,
    colors: List<Color>,
    colorStops: List<Float>? = null,
    tileMode: TileMode = TileMode.Clamp
): Shader

Creates a linear gradient from from to to.

If colorStops is provided, each value is a number from 0.0 to 1.0 that specifies where the color at the corresponding index in colors begins in the gradient. If colorStops is not provided, then the colors are dispersed evenly

The behavior before from and after to is described by the tileMode argument. For details, see the TileMode enum. If no TileMode is provided the default value of TileMode.Clamp is used

Path

actual fun Path(): Path

RadialGradient

fun RadialGradient(
    vararg colorStops: ColorStop,
    centerX: Float,
    centerY: Float,
    radius: Float,
    tileMode: TileMode = TileMode.Clamp
): RadialGradient

Creates a radial gradient with the given colors at the provided offset defined in the ColorStop

RadialGradient(
     0.0f to Color.Red,
     0.3f to Color.Green,
     1.0f to Color.Blue,
     centerX = side1 / 2.0f,
     centerY = side2 / 2.0f,
     radius = side1 / 2.0f,
     tileMode = TileMode.Repeated
)

RadialGradient

fun RadialGradient(
    colors: List<Color>,
    centerX: Float,
    centerY: Float,
    radius: Float,
    tileMode: TileMode = TileMode.Clamp
): RadialGradient

Creates a radial gradient with the given colors evenly dispersed within the gradient

RadialGradient(
     listOf(Color.Red, Color.Green, Color.Blue),
     centerX = side1 / 2.0f,
     centerY = side2 / 2.0f,
     radius = side1 / 2.0f,
     tileMode = TileMode.Repeated
)

RadialGradientShader

fun RadialGradientShader(
    center: Offset,
    radius: Float,
    colors: List<Color>,
    colorStops: List<Float>? = null,
    tileMode: TileMode = TileMode.Clamp
): Shader

Creates a radial gradient centered at center that ends at radius distance from the center.

If colorStops is provided, each value is a number from 0.0 to 1.0 that specifies where the color at the corresponding index in colors begins in the gradient. If colorStops is not provided, then the colors are dispersed evenly

The behavior before and after the radius is described by the tileMode argument. For details, see the TileMode enum.

The behavior outside of the bounds of center +/- radius is described by the tileMode argument. For details, see the TileMode enum. If no TileMode is provided the default value of TileMode.Clamp is used

VerticalGradient

fun VerticalGradient(
    colors: List<Color>,
    startY: Float,
    endY: Float,
    tileMode: TileMode = TileMode.Clamp
): LinearGradient

Creates a vertical gradient with the given colors evenly dispersed within the gradient Ex:

 VerticalGradient(
     listOf(Color.Red, Color.Green, Color.Blue),
     startY = 0.0f,
     endY = 100.0f
)

VerticalGradient

fun VerticalGradient(
    vararg colorStops: ColorStop,
    startY: Float,
    endY: Float,
    tileMode: TileMode = TileMode.Clamp
): LinearGradient

Creates a vertical gradient with the given colors at the provided offset defined in the ColorStop Ex:

 VerticalGradient(
     0.1f to Color.Red,
     0.3f to Color.Green,
     0.5f to Color.Blue,
     startY = 0.0f,
     endY = 100.0f
)

imageFromResource

fun imageFromResource(
    res: Resources,
    resId: Int
): ImageAsset

Create an ImageAsset from an image file stored in resources for the application

Parameters
res: Resources Resources object to query the image file from
resId: Int Identifier for the image asset to query from res
Return
Loaded image file represented as an ImageAsset

lerp

@Stable fun lerp(
    start: Color,
    stop: Color,
    @FloatRange(0.0, 1.0) fraction: Float
): Color

Linear interpolate between two Colors, start and stop with fraction fraction between the two. The ColorSpace of the result is always the ColorSpace of stop.

lerp

@Stable fun lerp(
    start: Shadow,
    stop: Shadow,
    fraction: Float
): Shadow

Linearly interpolate two Shadows.

Top-level properties

RectangleShape

@Stable val RectangleShape: Shape

A shape describing the rectangle.

Extension functions

addOutline

fun Path.addOutline(outline: Outline): Unit

Adds the outline to the Path.

asAndroidBitmap

fun ImageAsset.asAndroidBitmap(): Bitmap

asAndroidPath

inline fun Path.asAndroidPath(): Path

asComposePath

fun Path.asComposePath(): Path

Convert the android.graphics.Path instance into a compose compatible Path

asImageAsset

fun Bitmap.asImageAsset(): ImageAsset

Create an ImageAsset from the given Bitmap. Note this does not create a copy of the original Bitmap and changes to it will modify the returned ImageAsset

compositeOver

@Stable fun Color.compositeOver(background: Color): Color

Composites this color on top of background using the Porter-Duff 'source over' mode.

Both this and background must not be pre-multiplied, and the resulting color will also not be pre-multiplied.

The ColorSpace of the result is always the ColorSpace of background.

Return
the Color representing this composited on top of background, converted to the color space of background.

drawOutline

fun DrawScope.drawOutline(
    outline: Outline,
    color: Color,
    @FloatRange(0.0, 1.0) alpha: Float = 1.0f,
    style: DrawStyle = Fill,
    colorFilter: ColorFilter? = null,
    blendMode: BlendMode = DrawScope.DefaultBlendMode
): Unit

Draws the Outline on a DrawScope.

Parameters
outline: Outline the outline to draw.
color: Color Color applied to the outline when it is drawn
alpha: Float = 1.0f Opacity to be applied to outline from 0.0f to 1.0f representing fully transparent to fully opaque respectively
style: DrawStyle = Fill Specifies whether the outline is stroked or filled in
colorFilter: ColorFilter? = null : ColorFilter to apply to the color when drawn into the destination
blendMode: BlendMode = DrawScope.DefaultBlendMode : Blending algorithm to be applied to the outline

drawOutline

fun DrawScope.drawOutline(
    outline: Outline,
    brush: Brush,
    @FloatRange(0.0, 1.0) alpha: Float = 1.0f,
    style: DrawStyle = Fill,
    colorFilter: ColorFilter? = null,
    blendMode: BlendMode = DrawScope.DefaultBlendMode
): Unit

Draws the Outline on a DrawScope.

Parameters
outline: Outline the outline to draw.
brush: Brush Brush applied to the outline when it is drawn
alpha: Float = 1.0f Opacity to be applied to outline from 0.0f to 1.0f representing fully transparent to fully opaque respectively
style: DrawStyle = Fill Specifies whether the outline is stroked or filled in
colorFilter: ColorFilter? = null : ColorFilter to apply to the Brush when drawn into the destination
blendMode: BlendMode = DrawScope.DefaultBlendMode : Blending algorithm to be applied to the outline

drawOutline

fun Canvas.drawOutline(
    outline: Outline,
    paint: Paint
): Unit

Draws the Outline on a Canvas.

Parameters
outline: Outline the outline to draw.
paint: Paint the paint used for the drawing.

luminance

@Stable fun Color.luminance(): Float

Returns the relative luminance of this color.

Based on the formula for relative luminance defined in WCAG 2.0, W3C Recommendation 11 December 2008.

Return
A value between 0 (darkest black) and 1 (lightest white)
Exceptions
IllegalArgumentException If the this color's color space does not use the RGB color model

rotate

fun Canvas.rotate(
    degrees: Float,
    pivotX: Float,
    pivotY: Float
): Unit

Add a rotation (in degrees clockwise) to the current transform at the given pivot point. The pivot coordinate remains unchanged by the rotation transformation

Parameters
degrees: Float to rotate clockwise
pivotX: Float The x-coord for the pivot point
pivotY: Float The y-coord for the pivot point

rotateRad

fun Canvas.rotateRad(
    radians: Float,
    pivotX: Float = 0.0f,
    pivotY: Float = 0.0f
): Unit

Add a rotation (in radians clockwise) to the current transform at the given pivot point. The pivot coordinate remains unchanged by the rotation transformation

Parameters
pivotX: Float = 0.0f The x-coord for the pivot point
pivotY: Float = 0.0f The y-coord for the pivot point

scale

fun Canvas.scale(
    sx: Float,
    sy: Float = sx,
    pivotX: Float,
    pivotY: Float
): Unit

Add an axis-aligned scale to the current transform, scaling by the first argument in the horizontal direction and the second in the vertical direction at the given pivot coordinate. The pivot coordinate remains unchanged by the scale transformation.

If sy is unspecified, sx will be used for the scale in both directions.

Parameters
sx: Float The amount to scale in X
sy: Float = sx The amount to scale in Y
pivotX: Float The x-coord for the pivot point
pivotY: Float The y-coord for the pivot point

toArgb

@Stable @ColorInt fun Color.toArgb(): Int

Converts this color to an ARGB color int. A color int is always in the sRGB color space. This implies a color space conversion is applied if needed.

Return
An ARGB color in the sRGB color space

toNativeTileMode

actual fun TileMode.toNativeTileMode(): NativeTileMode

toNativeVertexMode

actual fun VertexMode.toNativeVertexMode(): VertexMode

toPixelMap

fun ImageAsset.toPixelMap(
    startX: Int = 0,
    startY: Int = 0,
    width: Int = this.width,
    height: Int = this.height,
    buffer: IntArray = IntArray(width * height),
    bufferOffset: Int = 0,
    stride: Int = width
): PixelMap

Convenience method to extract pixel information from the given ImageAsset into a PixelMap that supports for querying pixel information based on

Note this method can block so it is recommended to not invoke this method in performance critical code paths

import androidx.ui.graphics.toPixelMap

val imageAsset = createImageAsset()

// Sample a 3 by 2 subsection of the given ImageAsset
// starting at the coordinate (48, 49)
val pixelmap = imageAsset.toPixelMap(
    startX = 48,
    startY = 49,
    width = 3,
    height = 2
)

// create a histogram to count the number of occurrences of a color within the specified
// subsection of the provided ImageAsset
val histogram = HashMap<Color, Int>()
for (x in 0 until pixelmap.width) {
    for (y in 0 until pixelmap.height) {
        val color = pixelmap[x, y]
        val colorCount = histogram[color] ?: 0
        histogram[color] = (colorCount + 1)
    }
}
Parameters
startX: Int = 0 The x-coordinate of the first pixel to read from the ImageAsset
startY: Int = 0 The y-coordinate of the first pixel to read from the ImageAsset
width: Int = this.width The number of pixels to read from each row
height: Int = this.height The number of rows to read
buffer: IntArray = IntArray(width * height) The array to store the ImageAsset's colors. By default this allocates an IntArray large enough to store all the pixel information. Consumers of this API are advised to use the smallest IntArray necessary to extract relevant pixel information
bufferOffset: Int = 0 The first index to write into the buffer array, this defaults to 0
stride: Int = width The number of entries in buffer to skip between rows (must be >= width

useOrElse

inline fun Color.useOrElse(block: () -> Color): Color

If this Color then this is returned, otherwise block is executed and its result is returned.

withSave

inline fun Canvas.withSave(block: () -> Unit): Unit

Saves a copy of the current transform and clip on the save stack and executes the provided lambda with the current transform applied. Once the lambda has been executed, the transformation is popped from the stack, undoing the transformation.

See also:

Canvas.saveLayer, which does the same thing but additionally also groups the commands

withSaveLayer

inline fun Canvas.withSaveLayer(
    bounds: Rect,
    paint: Paint,
    block: () -> Unit
): Unit

Saves a copy of the current transform and clip on the save stack, and then creates a new group which subsequent calls will become a part of. When the lambda is executed and the save stack is popped, the group will be flattened into a layer and have the given paint's Paint.colorFilter and Paint.blendMode applied.

This lets you create composite effects, for example making a group of drawing commands semi-transparent. Without using Canvas.saveLayer, each part of the group would be painted individually, so where they overlap would be darker than where they do not. By using Canvas.saveLayer to group them together, they can be drawn with an opaque color at first, and then the entire group can be made transparent using the Canvas.saveLayer's paint.

Using saveLayer with clips

When a rectangular clip operation (from Canvas.clipRect) is not axis-aligned with the raster buffer, or when the clip operation is not rectalinear (e.g. because it is a rounded rectangle clip created by Canvas.clipPath), the edge of the clip needs to be anti-aliased.

If two draw calls overlap at the edge of such a clipped region, without using Canvas.saveLayer, the first drawing will be anti-aliased with the background first, and then the second will be anti-aliased with the result of blending the first drawing and the background. On the other hand, if Canvas.saveLayer is used immediately after establishing the clip, the second drawing will cover the first in the layer, and thus the second alone will be anti-aliased with the background when the layer is clipped and composited (when lambda is finished executing).

Performance considerations

Generally speaking, Canvas.saveLayer is relatively expensive.

There are a several different hardware architectures for GPUs (graphics processing units, the hardware that handles graphics), but most of them involve batching commands and reordering them for performance. When layers are used, they cause the rendering pipeline to have to switch render target (from one layer to another). Render target switches can flush the GPU's command buffer, which typically means that optimizations that one could get with larger batching are lost. Render target switches also generate a lot of memory churn because the GPU needs to copy out the current frame buffer contents from the part of memory that's optimized for writing, and then needs to copy it back in once the previous render target (layer) is restored.

See also:

Extension properties

isSet

@Stable inline val Color.isSet: Boolean

false when this is Color.Unset.

isUnset

@Stable inline val Color.isUnset: Boolean

true when this is Color.Unset.