Auto Height Jetpack Compose 线圈图像 [英] Auto Height Jetpack Compose Coil Image

查看:39
本文介绍了Auto Height Jetpack Compose 线圈图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Jetpack Compose Coil 显示具有填充宽度和自动高度的图像.我希望图像采用全宽和自动高度.但是图像只有在我指定固定高度时才会显示.

I am trying to show an image with fill width and auto height using Jetpack Compose Coil. I want the image to take the full width and auto height. But the image only showing when I specify a fixed height.

Image(
    painter = rememberImagePainter(
        data = post.image
    ),
    contentDescription = null,
    contentScale = ContentScale.Crop,
    modifier = Modifier.fillMaxWidth()
)

我尝试了 .fillMaxHeight().fillMaxSize() 但它不起作用.有什么办法可以做到这一点吗?

I tried with .fillMaxHeight() and .fillMaxSize() but it's not working. Is there any way I could achieve this?

推荐答案

当视图 width/height 之一计算为零时会发生这种情况,这意味着它不应该t 显示,无需下载.在撰写跟踪器上的此问题中详细了解原因.

This happens when one of view width/height is calculated as zero, which means it shouldn't be displayed and no need to download it. Check out more about the reasons in this issue on compose tracker.

你应该让你的尺寸不等于零.根据您的布局,它可以通过宽度/高度/纵横比修饰符的变化来完成.

You should make your size not being equal to zero. Depending you your layout it can be done with variations of width/height/aspect ratio modifiers.

如果您想获得原始尺寸的图像,您可以将 size(OriginalSize) 添加到 Painter builder.这将强制图像开始加载.此参数使您的视图下载并放入图像的完整大小的内存中,而无需针对当前视图对其进行优化.所以请谨慎使用它,只有当您确定图像不会太大并且您确实无法添加使用大小修饰符时.

If you'd like to get your image in original size, you can to add size(OriginalSize) to painter builder. This will force image to start loading. This parameter makes your view download and put into the memory full size of the image, without optimizing it for the current view. So use it carefully, only if you're sure the image won't be too big and you really can't add use size modifiers.

Image(
    painter = rememberImagePainter(
        data = post.image,
        builder = {
            size(OriginalSize)
        },
    ),
    contentDescription = null,
    contentScale = ContentScale.Crop,
    modifier = Modifier.fillMaxWidth()
)

这篇关于Auto Height Jetpack Compose 线圈图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆