drawLayer

Function

Common
fun DrawScope.drawLayer(graphicsLayer: GraphicsLayer)

Draw the provided GraphicsLayer into the current DrawScope. The GraphicsLayer provided must have GraphicsLayer.record invoked on it otherwise no visual output will be seen in the rendered result.

Code Examples

GraphicsLayerAlphaSample

fun DrawScope.GraphicsLayerAlphaSample(layer: GraphicsLayer) {
    // Create a layer sized to the destination draw scope that is comprised
    // of an inset red rectangle
    layer.apply {
        record { inset(20f, 20f) { drawRect(Color.Red) } }
        // Renders the content of the layer with 50% alpha when it is drawn
        // into the destination
        alpha = 0.5f
    }
    drawLayer(layer)
}

GraphicsLayerColorFilterSample

fun DrawScope.GraphicsLayerColorFilterSample(layer: GraphicsLayer) {
    // Create a layer with the same configuration as the destination DrawScope
    // and draw a red rectangle in the layer
    layer.apply {
        record { drawRect(Color.Red) }
        // Apply a ColorFilter that will tint the contents of the layer to blue
        // when it is drawn into the destination DrawScope
        colorFilter = ColorFilter.tint(Color.Blue)
    }
    drawLayer(layer)
}

GraphicsLayerRenderEffectSample

@RequiresApi(Build.VERSION_CODES.S)
fun DrawScope.GraphicsLayerRenderEffectSample(layer: GraphicsLayer) {
    // Create a layer sized to the destination draw scope that is comprised
    // of an inset red rectangle
    layer.apply {
        record { inset(20f, 20f) { drawRect(Color.Red) } }
        // Configure a blur to the contents of the layer that is applied
        // when drawn to the destination DrawScope
        renderEffect = BlurEffect(20f, 20f, TileMode.Decal)
    }
    drawLayer(layer)
}

GraphicsLayerRotationX

fun DrawScope.GraphicsLayerRotationX(layer: GraphicsLayer) {
    layer.apply {
        record { drawRect(Color.Yellow) }
        // Rotates the yellow rect 45f clockwise relative to the x axis
        rotationX = 45f
    }
    drawLayer(layer)
}

GraphicsLayerRotationYWithCameraDistance

fun DrawScope.GraphicsLayerRotationYWithCameraDistance(layer: GraphicsLayer) {
    layer.apply {
        record { drawRect(Color.Yellow) }
        // Rotates the yellow rect 45f clockwise relative to the y axis
        rotationY = 45f
        cameraDistance = 5.0f
    }
    drawLayer(layer)
}

GraphicsLayerScaleAndPivotSample

fun DrawScope.GraphicsLayerScaleAndPivotSample(layer: GraphicsLayer) {
    // Create a 200 x 200 pixel layer that has a red rectangle drawn in the lower right
    // corner.
    layer.apply {
        record(size = IntSize(200, 200)) {
            drawRect(Color.Red, topLeft = Offset(size.width / 2f, size.height / 2f))
        }
        // Scale the layer by 1.5x in both the x and y axis relative to the bottom
        // right corner
        scaleX = 1.5f
        scaleY = 1.5f
        pivotOffset = Offset(this.size.width.toFloat(), this.size.height.toFloat())
    }
    // Draw the layer into the provided DrawScope
    drawLayer(layer)
}

GraphicsLayerTopLeftSample

fun DrawScope.GraphicsLayerTopLeftSample(layer: GraphicsLayer) {
    // Build the layer with the density, layout direction and size from the DrawScope
    // and position the top left to be 20 pixels from the left and 30 pixels from the top.
    // This will the bounds of the layer with a red rectangle
    layer.apply {
        record { drawRect(Color.Red) }
        this.topLeft = IntOffset(20, 30)
    }
    // Draw the layer into the provided DrawScope
    drawLayer(layer)
}