Create a [Painter] from an Android resource id.
AndroidDrawableInDrawScopeSample
@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)
}
}
}
)
}
PainterResourceSample
@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),
)
}