Function
Common
Deprecated Please use a new overload accepting nullable GraphicsLayer
inline fun DrawScope.draw(
    density: Density,
    layoutDirection: LayoutDirection,
    canvas: Canvas,
    size: Size,
    block: DrawScope.() -> Unit,
)
Common
inline fun DrawScope.draw(
    density: Density,
    layoutDirection: LayoutDirection,
    canvas: Canvas,
    size: Size,
    graphicsLayer: GraphicsLayer? = null,
    block: DrawScope.() -> Unit,
)

Draws into the provided Canvas with the commands specified in the lambda with this DrawScope as a receiver

Parameters

density Density used to assist in conversions of density independent pixels to raw pixels to draw
layoutDirection LayoutDirection of the layout being drawn in.
canvas target canvas to render into
size bounds relative to the current canvas translation in which the DrawScope should draw within
graphicsLayer Current GraphicsLayer we are drawing into. Might be null if the canvas is not provided by a GraphicsLayer, for example in the case of a software-accelerated drawing
block lambda that is called to issue drawing commands on this DrawScope

Code Examples

DrawScopeRetargetingSample

@Composable
fun DrawScopeRetargetingSample() {
    Box(
        modifier =
            Modifier.size(120.dp).drawWithCache {
                // Example that shows how to redirect rendering to an Android Picture and then
                // draw the picture into the original destination
                // Note:
                // Canvas#drawPicture is supported with hardware acceleration on Android API 23+
                // Check
                // https://developer.android.com/topic/performance/hardware-accel#drawing-support
                // for details of which drawing operations are supported with hardware acceleration
                val picture = android.graphics.Picture()
                val width = this.size.width.toInt()
                val height = this.size.height.toInt()
                onDrawWithContent {
                    val pictureCanvas =
                        androidx.compose.ui.graphics.Canvas(picture.beginRecording(width, height))
                    draw(this, this.layoutDirection, pictureCanvas, this.size) {
                        [email protected]()
                    }
                    picture.endRecording()
                    drawIntoCanvas { canvas -> canvas.nativeCanvas.drawPicture(picture) }
                }
            }
    )
}