Compose Modifier
Common
fun Modifier.paint(
    painter: Painter,
    sizeToIntrinsics: Boolean = true,
    alignment: Alignment = Alignment.Center,
    contentScale: ContentScale = ContentScale.Inside,
    alpha: Float = DefaultAlpha,
    colorFilter: ColorFilter? = null,
) =
    this then
        PainterElement(
            painter = painter,
            sizeToIntrinsics = sizeToIntrinsics,
            alignment = alignment,
            contentScale = contentScale,
            alpha = alpha,
            colorFilter = colorFilter,
        )

Paint the content using painter.

Parameters

painter Painter to be drawn by this Modifier
sizeToIntrinsics true to size the element relative to Painter.intrinsicSize
alignment specifies alignment of the painter relative to content
contentScale strategy for scaling painter if its size does not match the content size
alpha opacity of painter
colorFilter optional ColorFilter to apply to painter

Code Examples

PainterModifierSample

@Composable
fun PainterModifierSample() {
    class CustomPainter : Painter() {
        override val intrinsicSize: Size
            get() = Size(300.0f, 300.0f)
        override fun DrawScope.onDraw() {
            drawCircle(center = center, radius = size.minDimension / 2.0f, color = Color.Red)
        }
    }
    Box(
        modifier =
            Modifier.background(color = Color.Gray)
                .padding(30.dp)
                .background(color = Color.Yellow)
                .paint(CustomPainter())
    ) {
        /** intentionally empty */
    }
}