RemoteImage · From a URL
The document carries only the URL; the player fetches the image. A named bitmap can be swapped by the host.
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.size
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.rememberNamedRemoteImageBitmap
import androidx.compose.remote.creation.compose.state.rememberRemoteImageBitmap
import androidx.compose.remote.creation.compose.state.rs
import androidx.compose.runtime.Composable
@Composable
@RemoteComposable
fun ImageFromUrl(url: String, named: Boolean) {
// The document carries the URL; the player fetches the image when it draws.
// A named bitmap can also be replaced by the host at play time.
val bitmap =
if (named) rememberNamedRemoteImageBitmap("hero", url) else rememberRemoteImageBitmap(url)
RemoteImage(
bitmap = bitmap,
contentDescription = "Image loaded from a URL".rs,
modifier = RemoteModifier.size(240.rdp, 160.rdp),
contentScale = RemoteContentScale.Crop,
)
}
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 Nonecreation.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)