{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

ImageKt

public final class ImageKt


Summary

Public methods

static final void
@Composable
@NonRestartableComposable
Image(
    @NonNull ImageVector imageVector,
    String contentDescription,
    @NonNull Modifier modifier,
    @NonNull Alignment alignment,
    @NonNull ContentScale contentScale,
    float alpha,
    ColorFilter colorFilter
)

A composable that lays out and draws a given ImageVector.

static final void
@Composable
Image(
    @NonNull Painter painter,
    String contentDescription,
    @NonNull Modifier modifier,
    @NonNull Alignment alignment,
    @NonNull ContentScale contentScale,
    float alpha,
    ColorFilter colorFilter
)

Creates a composable that lays out and draws a given Painter.

static final void
@Composable
@NonRestartableComposable
Image(
    @NonNull ImageBitmap bitmap,
    String contentDescription,
    @NonNull Modifier modifier,
    @NonNull Alignment alignment,
    @NonNull ContentScale contentScale,
    float alpha,
    ColorFilter colorFilter,
    @NonNull FilterQuality filterQuality
)

A composable that lays out and draws a given ImageBitmap.

Public methods

Image

@Composable
@NonRestartableComposable
public static final void Image(
    @NonNull ImageVector imageVector,
    String contentDescription,
    @NonNull Modifier modifier,
    @NonNull Alignment alignment,
    @NonNull ContentScale contentScale,
    float alpha,
    ColorFilter colorFilter
)

A composable that lays out and draws a given ImageVector. This will attempt to size the composable according to the ImageVector's given width and height. However, an optional Modifier parameter can be provided to adjust sizing or draw additional content (ex. background). Any unspecified dimension will leverage the ImageVector's size as a minimum constraint.

Parameters
@NonNull ImageVector imageVector

The ImageVector to draw

String contentDescription

text used by accessibility services to describe what this image represents. This should always be provided unless this image is used for decorative purposes, and does not represent a meaningful action that a user can take. This text should be localized, such as by using androidx.compose.ui.res.stringResource or similar

@NonNull Modifier modifier

Modifier used to adjust the layout algorithm or draw decoration content (ex. background)

@NonNull Alignment alignment

Optional alignment parameter used to place the ImageVector in the given bounds defined by the width and height

@NonNull ContentScale contentScale

Optional scale parameter used to determine the aspect ratio scaling to be used if the bounds are a different size from the intrinsic size of the ImageVector

float alpha

Optional opacity to be applied to the ImageVector when it is rendered onscreen

ColorFilter colorFilter

Optional ColorFilter to apply for the ImageVector when it is rendered onscreen

Image

@Composable
public static final void Image(
    @NonNull Painter painter,
    String contentDescription,
    @NonNull Modifier modifier,
    @NonNull Alignment alignment,
    @NonNull ContentScale contentScale,
    float alpha,
    ColorFilter colorFilter
)

Creates a composable that lays out and draws a given Painter. This will attempt to size the composable according to the Painter's intrinsic size. However, an optional Modifier parameter can be provided to adjust sizing or draw additional content (ex. background)

NOTE a Painter might not have an intrinsic size, so if no LayoutModifier is provided as part of the Modifier chain this might size the Image composable to a width and height of zero and will not draw any content. This can happen for Painter implementations that always attempt to fill the bounds like ColorPainter

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.unit.dp

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)
)
Parameters
@NonNull Painter painter

to draw

String contentDescription

text used by accessibility services to describe what this image represents. This should always be provided unless this image is used for decorative purposes, and does not represent a meaningful action that a user can take. This text should be localized, such as by using androidx.compose.ui.res.stringResource or similar

@NonNull Modifier modifier

Modifier used to adjust the layout algorithm or draw decoration content (ex. background)

@NonNull Alignment alignment

Optional alignment parameter used to place the Painter in the given bounds defined by the width and height.

@NonNull ContentScale contentScale

Optional scale parameter used to determine the aspect ratio scaling to be used if the bounds are a different size from the intrinsic size of the Painter

float alpha

Optional opacity to be applied to the Painter when it is rendered onscreen the default renders the Painter completely opaque

ColorFilter colorFilter

Optional colorFilter to apply for the Painter when it is rendered onscreen

Image

@Composable
@NonRestartableComposable
public static final void Image(
    @NonNull ImageBitmap bitmap,
    String contentDescription,
    @NonNull Modifier modifier,
    @NonNull Alignment alignment,
    @NonNull ContentScale contentScale,
    float alpha,
    ColorFilter colorFilter,
    @NonNull FilterQuality filterQuality
)

A composable that lays out and draws a given ImageBitmap. This will attempt to size the composable according to the ImageBitmap's given width and height. However, an optional Modifier parameter can be provided to adjust sizing or draw additional content (ex. background). Any unspecified dimension will leverage the ImageBitmap's size as a minimum constraint.

The following sample shows basic usage of an Image composable to position and draw an ImageBitmap on screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.size
import androidx.compose.ui.graphics.ImageBitmap

val ImageBitmap = createTestImage()
// Lays out and draws an image sized to the dimensions of the ImageBitmap
Image(bitmap = ImageBitmap, contentDescription = "Localized description")

For use cases that require drawing a rectangular subset of the ImageBitmap consumers can use overload that consumes a Painter parameter shown in this sample

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.size
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntSize

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"
)
Parameters
@NonNull ImageBitmap bitmap

The ImageBitmap to draw

String contentDescription

text used by accessibility services to describe what this image represents. This should always be provided unless this image is used for decorative purposes, and does not represent a meaningful action that a user can take. This text should be localized, such as by using androidx.compose.ui.res.stringResource or similar

@NonNull Modifier modifier

Modifier used to adjust the layout algorithm or draw decoration content (ex. background)

@NonNull Alignment alignment

Optional alignment parameter used to place the ImageBitmap in the given bounds defined by the width and height

@NonNull ContentScale contentScale

Optional scale parameter used to determine the aspect ratio scaling to be used if the bounds are a different size from the intrinsic size of the ImageBitmap

float alpha

Optional opacity to be applied to the ImageBitmap when it is rendered onscreen

ColorFilter colorFilter

Optional ColorFilter to apply for the ImageBitmap when it is rendered onscreen

@NonNull FilterQuality filterQuality

Sampling algorithm applied to the bitmap when it is scaled and drawn into the destination. The default is FilterQuality.Low which scales using a bilinear sampling algorithm