【发布时间】:2021-07-01 14:11:28
【问题描述】:
嗯,我正在研究 Compose UI,但我一直在做一些基本的事情。 其中之一是使用 Glide 显示来自 URL 的图像。
我已尝试以下代码,但未调用委托(onResourceReady 和 onLoadCleared)。
我错过了什么吗?
@Composable
fun loadPicture(url: String, contentDescription:String, modifier: Modifier = Modifier) {
val bitmapState = remember { mutableStateOf<Bitmap?>(null) }
Glide.with(LocalContext.current).asBitmap().load(url).into(
object : CustomTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
bitmapState.value = resource
}
override fun onLoadCleared(placeholder: Drawable?) {}
}
)
bitmapState.value?.let {
Image(
contentDescription = contentDescription,
bitmap = it.asImageBitmap(),
modifier = modifier
)
}
}
【问题讨论】:
标签: android android-glide android-jetpack-compose