RemoteImage · From a URL
The document carries only the URL; the player fetches the image. A named bitmap can be swapped by the host.
Открыть живое интерактивное демо →
Меняйте параметры, смотрите удалённый документ, который получается, и разницу после каждого изменения.
Что это
Показывает картинку — встроенную в экран или по веб-адресу — с обрезкой или вписыванием в рамку.
Подходит для
- Фото товаров и баннеры
- Аватары и превью
- Картинки, которые сервер меняет без обновления приложения
Код
Исходник примера с импортами androidx.compose.remote. CI компилирует каждый фрагмент кода на сайте против 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)