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

androidx.compose.ui.draw

Extension functions summary

For Modifier
Modifier

Clip the content to shape.

Modifier

Clip the content to the bounds of a layer defined at this modifier.

Modifier
Modifier.drawOpacity(@FloatRange(0.0, 1.0) opacity: Float)

Draw content with modified opacity (alpha) that may be less than 1.

Modifier
Modifier.drawShadow(elevation: Dp, shape: Shape = RectangleShape, clip: Boolean = elevation > 0.dp, @FloatRange(0.0, 1.0) opacity: Float = 1f)

Creates a DrawLayerModifier that draws the shadow.

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

Paint the content using painter.

Extension functions

clip

@Stable fun Modifier.clip(shape: Shape): Modifier

Clip the content to shape.

Parameters
shape: Shape the content will be clipped to this Shape.

clipToBounds

@Stable fun Modifier.clipToBounds(): Modifier

Clip the content to the bounds of a layer defined at this modifier.

drawOpacity

@Stable fun Modifier.drawOpacity(@FloatRange(0.0, 1.0) opacity: Float): Modifier

Draw content with modified opacity (alpha) that may be less than 1.

Example usage:

import androidx.compose.foundation.Box
import androidx.compose.foundation.layout.preferredSize
import androidx.compose.ui.draw.drawOpacity

Box(Modifier.preferredSize(100.dp).drawOpacity(opacity = 0.5f), backgroundColor = Color.Red)
Parameters
opacity: Float the fraction of children's alpha value.

drawShadow

@Stable fun Modifier.drawShadow(
    elevation: Dp,
    shape: Shape = RectangleShape,
    clip: Boolean = elevation > 0.dp,
    @FloatRange(0.0, 1.0) opacity: Float = 1f
): Modifier

Creates a DrawLayerModifier that draws the shadow. The elevation defines the visual depth of the physical object. The physical object has a shape specified by shape.

Note that elevation is only affecting the shadow size and doesn't change the drawing order. Use zIndex modifier if you want to draw the elements with larger elevation after all the elements with a smaller one.

Example usage:

import androidx.compose.foundation.Box
import androidx.compose.foundation.layout.preferredSize
import androidx.compose.ui.draw.drawShadow

Box(
    Modifier.drawShadow(12.dp, RectangleShape)
        .preferredSize(100.dp, 100.dp)
)
Parameters
elevation: Dp The elevation for the shadow in pixels
shape: Shape = RectangleShape Defines a shape of the physical object
clip: Boolean = elevation > 0.dp When active, the content drawing clips to the shape.
opacity: Float = 1f The opacity of the layer, including the shadow.

paint

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

Paint the content using painter.

import androidx.compose.foundation.Box
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.padding
import androidx.compose.ui.draw.paint
import androidx.compose.ui.geometry.Size

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 **/ }
Parameters
sizeToIntrinsics: Boolean = true true to size the element relative to Painter.intrinsicSize
alignment: Alignment = Alignment.Center specifies alignment of the painter relative to content
contentScale: ContentScale = ContentScale.Inside strategy for scaling painter if its size does not match the content size
alpha: Float = DefaultAlpha opacity of painter
colorFilter: ColorFilter? = null optional ColorFilter to apply to painter