New Compose Multiplatform components arrived on Composables UICheck it out →

Modifier in Compose Ui

paint

Common

Paint the content using [painter].

Last updated:

Installation

dependencies {
   implementation("androidx.compose.ui:ui:1.7.0-beta04")
}

Overloads


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

Parameters

namedescription
painter[Painter] to be drawn by this [Modifier]
sizeToIntrinsicstrue to size the element relative to [Painter.intrinsicSize]
alignmentspecifies alignment of the [painter] relative to content
contentScalestrategy for scaling [painter] if its size does not match the content size
alphaopacity of [painter]
colorFilteroptional [ColorFilter] to apply to [painter]

Code Example

PainterModifierSample

@Composable
@Sampled
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 */ }
}