Composable Function

Image

A composable that lays out and draws a given ImageBitmap.

ImageSample

@Sampled
@Composable
fun ImageSample() {
    val ImageBitmap = createTestImage()
    // Lays out and draws an image sized to the dimensions of the ImageBitmap
    Image(bitmap = ImageBitmap, contentDescription = "Localized description")
}

BitmapPainterSubsectionSample

@Sampled
@Composable
fun BitmapPainterSubsectionSample() {
    val ImageBitmap = createTestImage()
    // Lays out and draws an image sized to the rectangular subsection of the ImageBitmap
    Image(
        painter = BitmapPainter(ImageBitmap, IntOffset(10, 12), IntSize(50, 60)),
        contentDescription = "Localized description",
    )
}

BitmapPainterSample

@Sampled
@Composable
fun BitmapPainterSample() {
    val customPainter = remember {
        object : Painter() {

            override val intrinsicSize: Size
                get() = Size(100.0f, 100.0f)

            override fun DrawScope.onDraw() {
                drawRect(color = Color.Cyan)
            }
        }
    }

    Image(
        painter = customPainter,
        contentDescription = "Localized description",
        modifier = Modifier.size(100.dp, 100.dp),
    )
}

/** Helper method to create an ImageBitmap with some content in it */
private fun createTestImage(): ImageBitmap {
    val ImageBitmap = ImageBitmap(100, 100)
    Canvas(ImageBitmap)
        .drawCircle(Offset(50.0f, 50.0f), 50.0f, Paint().apply { this.color = Color.Cyan })
    return ImageBitmap
}
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: