Remote Compose Storybook English GitHub

RemoteImage · Content scale

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

RemoteImage · Content scale: документ, нарисованный плеером Remote Compose
Открыть живое интерактивное демо →

Меняйте параметры, смотрите удалённый документ, который получается, и разницу после каждого изменения.

Что это

Показывает картинку — встроенную в экран или по веб-адресу — с обрезкой или вписыванием в рамку.

Подходит для

  • Фото товаров и баннеры
  • Аватары и превью
  • Картинки, которые сервер меняет без обновления приложения

Код

Исходник примера с импортами 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.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)

Справочник

Другие примеры этого компонента

Ещё в разделе «Контент»