android-image-bitmap.md

#Image Bitmap versus Image Vector

The two most common kinds of image formats are raster and vector images.


#Raster Graphics (ImageBitmap)

A raster graphic format contains pixels: tiny individual squares that contain a color (made up of red, green, blue, and alpha values). When placing a lot of pixels together, a very detailed image can be formed, such as a photograph.

Figure 1. JPEG file example: A close-up photograph of a golden retriever dog.

#Implementation in Compose

In Compose, a raster image is often referred to as a Bitmap. It is loaded into an ImageBitmap instance, and a BitmapPainter is responsible for drawing it to the screen.

Basic use case: Use painterResource() to create an ImageBitmap (returns a BitmapPainter).

kotlinImage(
    painter = painterResource(id = R.drawable.dog),
    contentDescription = stringResource(id = R.string.dog_content_description)
)

Advanced use case: If you need access to the ImageBitmap itself for custom implementations:

kotlinval imageBitmap = ImageBitmap.imageResource(R.drawable.dog)

#Vector Images (ImageVector)

Vector images are scalable mathematical representations of a visual element. A vector is a set of commands describing how to draw the image—for example, a line, point, or fill.

Figure 2. Vector example: A simple line-art icon of a shopping cart.

#Implementation in Compose

A VectorPainter is responsible for drawing an ImageVector. While it supports a subset of SVG commands, complex photos cannot be represented as vectors.

Basic use case: painterResource() automatically handles VectorDrawables and returns a VectorPainter.

kotlinImage(
    painter = painterResource(id = R.drawable.baseline_shopping_cart_24),
    contentDescription = stringResource(id = R.string.shopping_cart_content_desc)
)

Advanced use case: To access the ImageVector object directly:

kotlinval imageVector = ImageVector.vectorResource(id = R.drawable.baseline_shopping_cart_24)