Composable Function

painterResource

Create a Painter from an Android resource id.

PainterResourceSample

@Sampled
@Composable
fun PainterResourceSample() {
    // Sample showing how to render a Painter based on a different resource (vector vs png)
    // Here a Vector asset is used in the portrait orientation, however, a png is used instead
    // in the landscape orientation based on the res/drawable and res/drawable-land-hdpi folders
    Image(
        painterResource(R.drawable.ic_vector_or_png),
        contentDescription = null,
        modifier = Modifier.requiredSize(50.dp),
    )
}

AndroidDrawableInDrawScopeSample

@Sampled
@Composable
fun AndroidDrawableInDrawScopeSample() {
    val drawable =
        LocalResources.current.getDrawable(R.drawable.sample_drawable, LocalContext.current.theme)
    Box(
        modifier =
            Modifier.requiredSize(100.dp).drawBehind {
                drawIntoCanvas { canvas ->
                    drawable?.let {
                        it.setBounds(0, 0, size.width.roundToInt(), size.height.roundToInt())
                        it.draw(canvas.nativeCanvas)
                    }
                }
            }
    )
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: