如何从 Jetpack Compose 中的 URL 加载图像?

编程入门 行业动态 更新时间:2024-10-24 00:27:42
本文介绍了如何从 Jetpack Compose 中的 URL 加载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

嗯,我正在研究 Compose UI,并且我在学习基本的东西.其中之一是使用 Glide 显示来自 URL 的图像.

Well, I am studying the Compose UI and I am stucking in basic things. One of them is show a image from URL with Glide.

我尝试了以下代码,但没有调用委托(onResourceReady 和 onLoadCleared).

I have tried the below code but the delegates (onResourceReady and onLoadCleared) are not being called.

我错过了什么吗?

@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 来编写:

You can use Coil for compose:

https://coil-kt.github.io/coil/compose/

添加依赖:

implementation("io.coil-kt:coil-compose:1.3.1")

并像这样使用它:

Image(
    painter = rememberImagePainter("https://www.example/image.jpg"),
    contentDescription = null,
    modifier = Modifier.size(128.dp)
)

这篇关于如何从 Jetpack Compose 中的 URL 加载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 03:08:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034445.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   加载   Jetpack   Compose   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!