Google is committed to advancing racial equity for Black communities. See how.

Canvas

interface Canvas
androidx.compose.ui.graphics.Canvas

Summary

Public methods
abstract Unit
clipPath(path: Path, clipOp: ClipOp = ClipOp.Intersect)

Reduces the clip region to the intersection of the current clip and the given Path.

open Unit
clipRect(rect: Rect, clipOp: ClipOp = ClipOp.Intersect)

Reduces the clip region to the intersection of the current clip and the given rectangle.

abstract Unit
clipRect(left: Float, top: Float, right: Float, bottom: Float, clipOp: ClipOp = ClipOp.Intersect)

Reduces the clip region to the intersection of the current clip and the given bounds.

abstract Unit
concat(matrix: Matrix)

Multiply the current transform by the specified 4⨉4 transformation matrix specified as a list of values in column-major order.

abstract Unit

Disables Z support, preventing any layers drawn after this point from being visually reordered or having shadows rendered.

open Unit
drawArc(rect: Rect, startAngle: Float, sweepAngle: Float, useCenter: Boolean, paint: Paint)

Draw an arc scaled to fit inside the given rectangle.

abstract Unit
drawArc(left: Float, top: Float, right: Float, bottom: Float, startAngle: Float, sweepAngle: Float, useCenter: Boolean, paint: Paint)

Draw an arc scaled to fit inside the given rectangle.

open Unit
drawArcRad(rect: Rect, startAngleRad: Float, sweepAngleRad: Float, useCenter: Boolean, paint: Paint)

Draw an arc scaled to fit inside the given rectangle.

abstract Unit
drawCircle(center: Offset, radius: Float, paint: Paint)

Draws a circle centered at the point given by the first argument and that has the radius given by the second argument, with the Paint given in the third argument.

abstract Unit
drawImage(image: ImageAsset, topLeftOffset: Offset, paint: Paint)

Draws the given ImageAsset into the canvas with its top-left corner at the given Offset.

abstract Unit
drawImageRect(image: ImageAsset, srcOffset: IntOffset = IntOffset.Zero, srcSize: IntSize = IntSize(image.width, image.height), dstOffset: IntOffset = IntOffset.Zero, dstSize: IntSize = srcSize, paint: Paint)

Draws the subset of the given image described by the src argument into the canvas in the axis-aligned rectangle given by the dst argument.

abstract Unit
drawLine(p1: Offset, p2: Offset, paint: Paint)

Draws a line between the given points using the given paint.

open Unit
drawOval(rect: Rect, paint: Paint)

Draws an axis-aligned oval that fills the given axis-aligned rectangle with the given Paint.

abstract Unit
drawOval(left: Float, top: Float, right: Float, bottom: Float, paint: Paint)

Draws an axis-aligned oval that fills the given bounds provided with the given Paint.

abstract Unit
drawPath(path: Path, paint: Paint)

Draws the given Path with the given Paint.

abstract Unit
drawPoints(pointMode: PointMode, points: List<Offset>, paint: Paint)

Draws a sequence of points according to the given PointMode.

abstract Unit
drawRawPoints(pointMode: PointMode, points: FloatArray, paint: Paint)

Draws a sequence of points according to the given PointMode.

open Unit
drawRect(rect: Rect, paint: Paint)

Draws a rectangle with the given Paint.

abstract Unit
drawRect(left: Float, top: Float, right: Float, bottom: Float, paint: Paint)

Draws a rectangle with the given Paint.

abstract Unit
drawRoundRect(left: Float, top: Float, right: Float, bottom: Float, radiusX: Float, radiusY: Float, paint: Paint)

Draws a rounded rectangle with the given Paint.

abstract Unit
drawVertices(vertices: Vertices, blendMode: BlendMode, paint: Paint)

abstract Unit

Enables Z support which defaults to disabled.

abstract Unit

Pops the current save stack, if there is anything to pop.

abstract Unit
rotate(degrees: Float)

Add a rotation (in degrees clockwise) to the current transform

abstract Unit

Saves a copy of the current transform and clip on the save stack.

abstract Unit
saveLayer(bounds: Rect, paint: Paint)

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.

abstract Unit
scale(sx: Float, sy: Float = sx)

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.

abstract Unit
skew(sx: Float, sy: Float)

Add an axis-aligned skew to the current transform, with the first argument being the horizontal skew in degrees clockwise around the origin, and the second argument being the vertical skew in degrees clockwise around the origin.

open Unit
skewRad(sxRad: Float, syRad: Float)

Add an axis-aligned skew to the current transform, with the first argument being the horizontal skew in radians clockwise around the origin, and the second argument being the vertical skew in radians clockwise around the origin.

abstract Unit
translate(dx: Float, dy: Float)

Add a translation to the current transform, shifting the coordinate space horizontally by the first argument and vertically by the second argument.

Extension functions
From androidx.compose.ui.graphics
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.

Extension properties
From androidx.compose.ui.graphics
actual NativeCanvas

Return an instance of the native primitive that implements the Canvas interface

Public methods

clipPath

abstract fun clipPath(
    path: Path,
    clipOp: ClipOp = ClipOp.Intersect
): Unit

Reduces the clip region to the intersection of the current clip and the given Path.

If Paint.isAntiAlias is true, then the clip will be anti-aliased. If multiple draw commands intersect with the clip boundary, this can result in incorrect blending at the clip boundary. See saveLayer for a discussion of how to address that.

clipRect

open fun clipRect(
    rect: Rect,
    clipOp: ClipOp = ClipOp.Intersect
): Unit

Reduces the clip region to the intersection of the current clip and the given rectangle.

If the clip is not axis-aligned with the display device, and Paint.isAntiAlias is true, then the clip will be anti-aliased. If multiple draw commands intersect with the clip boundary, this can result in incorrect blending at the clip boundary. See saveLayer for a discussion of how to address that.

Use ClipOp.Difference to subtract the provided rectangle from the current clip.

clipRect

abstract fun clipRect(
    left: Float,
    top: Float,
    right: Float,
    bottom: Float,
    clipOp: ClipOp = ClipOp.Intersect
): Unit

Reduces the clip region to the intersection of the current clip and the given bounds.

If the clip is not axis-aligned with the display device, and Paint.isAntiAlias is true, then the clip will be anti-aliased. If multiple draw commands intersect with the clip boundary, this can result in incorrect blending at the clip boundary. See saveLayer for a discussion of how to address that.

Use ClipOp.Difference to subtract the provided rectangle from the current clip.

Parameters
left: Float Left bound of the clip region
top: Float Top bound of the clip region
right: Float Right bound of the clip region
bottom: Float Bottom bound of the clip region

concat

abstract fun concat(matrix: Matrix): Unit

Multiply the current transform by the specified 4⨉4 transformation matrix specified as a list of values in column-major order.

disableZ

abstract fun disableZ(): Unit

Disables Z support, preventing any layers drawn after this point from being visually reordered or having shadows rendered. This is not impacted by any save or restore calls as it is not considered part of the matrix or clip.

See Also

drawArc

open fun drawArc(
    rect: Rect,
    startAngle: Float,
    sweepAngle: Float,
    useCenter: Boolean,
    paint: Paint
): Unit

Draw an arc scaled to fit inside the given rectangle. It starts from startAngle degrees around the oval up to startAngle + sweepAngle degrees around the oval, with zero degrees being the point on the right hand side of the oval that crosses the horizontal line that intersects the center of the rectangle and with positive angles going clockwise around the oval. If useCenter is true, the arc is closed back to the center, forming a circle sector. Otherwise, the arc is not closed, forming a circle segment.

This method is optimized for drawing arcs and should be faster than Path.arcTo.

drawArc

abstract fun drawArc(
    left: Float,
    top: Float,
    right: Float,
    bottom: Float,
    startAngle: Float,
    sweepAngle: Float,
    useCenter: Boolean,
    paint: Paint
): Unit

Draw an arc scaled to fit inside the given rectangle. It starts from startAngle degrees around the oval up to startAngle + sweepAngle degrees around the oval, with zero degrees being the point on the right hand side of the oval that crosses the horizontal line that intersects the center of the rectangle and with positive angles going clockwise around the oval. If useCenter is true, the arc is closed back to the center, forming a circle sector. Otherwise, the arc is not closed, forming a circle segment.

This method is optimized for drawing arcs and should be faster than Path.arcTo.

Parameters
left: Float Left bound of the arc
top: Float Top bound of the arc
right: Float Right bound of the arc
bottom: Float Bottom bound of the arc
startAngle: Float Starting angle of the arc relative to 3 o'clock
sweepAngle: Float Sweep angle in degrees clockwise
useCenter: Boolean Flag indicating whether or not to include the center of the oval in the arc, and close it if it is being stroked. This will draw a wedge.

drawArcRad

open fun drawArcRad(
    rect: Rect,
    startAngleRad: Float,
    sweepAngleRad: Float,
    useCenter: Boolean,
    paint: Paint
): Unit

Draw an arc scaled to fit inside the given rectangle. It starts from startAngle radians around the oval up to startAngle + sweepAngle radians around the oval, with zero radians being the point on the right hand side of the oval that crosses the horizontal line that intersects the center of the rectangle and with positive angles going clockwise around the oval. If useCenter is true, the arc is closed back to the center, forming a circle sector. Otherwise, the arc is not closed, forming a circle segment.

This method is optimized for drawing arcs and should be faster than Path.arcTo.

drawCircle

abstract fun drawCircle(
    center: Offset,
    radius: Float,
    paint: Paint
): Unit

Draws a circle centered at the point given by the first argument and that has the radius given by the second argument, with the Paint given in the third argument. Whether the circle is filled or stroked (or both) is controlled by Paint.style.

drawImage

abstract fun drawImage(
    image: ImageAsset,
    topLeftOffset: Offset,
    paint: Paint
): Unit

Draws the given ImageAsset into the canvas with its top-left corner at the given Offset. The image is composited into the canvas using the given Paint.

drawImageRect

abstract fun drawImageRect(