Remote Compose Storybook Русский GitHub

RemoteImage · Content scale

A 160×90 bitmap in a box of another shape, with every RemoteContentScale.

RemoteImage · Content scale: the document drawn by the Remote Compose player
Open the live, interactive demo →

Change its controls, read the remote document it produces and the diff after every change.

What it is

Shows a picture, embedded in the screen or loaded from a web address, cropped or fitted into its box.

Good for

  • Product photos and banners
  • Avatars and thumbnails
  • Images the server can change without an app update

Code

The story's source with androidx.compose.remote imports. CI compiles every snippet on this site against AndroidX.

import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteContentScale
import androidx.compose.remote.creation.compose.layout.RemoteImage
import androidx.compose.remote.creation.compose.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.border
import androidx.compose.remote.creation.compose.modifier.size
import androidx.compose.remote.creation.compose.modifier.width
import androidx.compose.remote.creation.compose.state.RemoteImageBitmap
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.rf
import androidx.compose.remote.creation.compose.state.rs
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Canvas
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.Paint

@Composable
@RemoteComposable
fun ImageContentScale(contentScale: RemoteContentScale, width: Int, height: Int, alpha: Float) {
    val bitmap = remember {
        val image = ImageBitmap(160, 90)
        Canvas(image).apply {
            drawRect(0f, 0f, 160f, 90f, Paint().apply { color = Color(0xFF1E88E5) })
            drawCircle(Offset(80f, 45f), 30f, Paint().apply { color = Color(0xFFFFCA28) })
            drawRect(0f, 70f, 160f, 90f, Paint().apply { color = Color(0xFF43A047) })
        }
        RemoteImageBitmap(image)
    }
    RemoteImage(
        bitmap = bitmap,
        contentDescription = "A sun over a field".rs,
        modifier = RemoteModifier.size(width.rdp, height.rdp).border(1.rdp, Color(0xFF9AA0A6).rc),
        contentScale = contentScale,
        alpha = alpha.rf,
    )
}

API

creation.compose.layout.RemoteImage

void RemoteImage(RemoteImageBitmap bitmap, RemoteString? contentDescription, optional RemoteModifier modifier, optional RemoteContentScale contentScale, optional RemoteFloat alpha)

creation.compose.layout.RemoteContentScale

value class RemoteContentScale
  RemoteContentScale.Companion Companion
  RemoteContentScale Crop
  RemoteContentScale FillBounds
  RemoteContentScale FillHeight
  RemoteContentScale FillWidth
  RemoteContentScale Fit
  RemoteContentScale Inside
  RemoteContentScale None

creation.compose.state.RemoteImageBitmap

class RemoteImageBitmap extends BaseRemoteState<java.lang.Object>
RemoteImageBitmap RemoteImageBitmap(ImageBitmap value)
  RemoteImageBitmap createNamedRemoteImageBitmap(String name, String url, optional RemoteState.Domain domain)
  Object? constantValueOrNull
  RemoteFloat height
  RemoteFloat width
  RemoteImageBitmap.Companion Companion
  RemoteImageBitmap createNamedRemoteImageBitmap(String name, String url, optional RemoteState.Domain domain)
RemoteImageBitmap RemoteImageBitmap(String url)

Reference

Other stories of this component

More in Content