这个问题在这里已经有了答案:
How do I load url into Image into DrawImage in Compose UI Android Jetpack?
(12 个回答)
去年关闭。
好吧,我正在研究 Compose UI,我坚持做一些基本的事情。
其中之一是使用 Glide 显示来自 URL 的图像。
我已经尝试了下面的代码,但没有调用委托(delegate)(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
)
}
}
最佳答案
您可以使用 Coil 进行撰写:
添加依赖:
implementation("io.coil-kt:coil-compose:2.0.0-rc01")
并像这样使用它:Image(
painter = rememberAsyncImagePainter("https://www.example.com/image.jpg"),
contentDescription = null,
modifier = Modifier.size(128.dp)
)
关于android - 如何从 Jetpack Compose 中的 URL 加载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66957702/