在 Compose 中缩放基本图像时遇到问题

问题描述 投票:0回答:1

我的问题很简单。然而我已经挣扎了好几个小时了。请结束我的疯狂。

我有一排,中间有一个盒子。我想将图像放入框中并使图像接触框的顶部和底部(垂直缩放/填充)。我也同意它填充整个东西(即使长宽比被搞乱)。 I want the actual blue image to stretch and fill to the top and bottom of the white box

这是我的代码:

@Preview(device = "spec:width=1280dp,height=800dp,dpi=310")
@Composable
fun Preview(){
    Row(modifier = Modifier.fillMaxSize(), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.Center) {
        Box(modifier = Modifier
            .fillMaxSize(.5f).background(Color.Red)) {
                BadgeImage()
        }
    }
}

@Composable
fun BadgeImage() {
    Image(painter = painterResource(id = R.drawable.badge_blank),
        contentDescription = null,
        modifier = Modifier.background(Color.White),
        contentScale = ContentScale.FillHeight,
        )
}

我在徽章图像可组合项上尝试了无数修改器。我已经尝试了所有 contentScale。

我什至无法让填充边界工作。请救我脱离痛苦<3.

附注我的图像尺寸是 2000 x 1200。比我的盒子大,我想把它放进去。

更新:Here is my image

android-jetpack-compose image-scaling modifier
1个回答
0
投票

问题不在于代码,而在于图像本身。它在您尝试调整边界的实际图像周围有一个透明区域。我建议导入并使用没有透明区域的图像。

我已经设法删除图像的透明区域。 Here is a link of the cropped image

© www.soinside.com 2019 - 2024. All rights reserved.