如何剪裁图像以删除其填充



我正在显示YouTube上的缩略图。这些图像的顶部和底部都有黑色矩形。

我想显示没有矩形的图像。如何设置垂直";填充";以移除黑色矩形。

Image(
painter = rememberImagePainter("https://img.youtube.com/vi/RS6By_pE7uo/0.jpg"),
modifier = Modifier
.width(itemWidth)
.height(photoImageHeight)
contentScale = ContentScale.FillBounds
)

将图像放在一个框中,该框的宽度与图像相同,但其高度减少了图像上方和下方黑条使用的高度。然后使用cropToBounds:

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val w = 480f / LocalDensity.current.density * 2.7f
val h = 360 / LocalDensity.current.density * 2.7f
Box(
modifier = Modifier
.requiredWidth(w.dp)
.requiredHeight(h.dp - 70.dp)
.clip(shape= RoundedCornerShape(30.dp))
) {
Image(
modifier = Modifier
.requiredWidth(w.dp)
.requiredHeight(h.dp),
painter = rememberImagePainter(
data = "https://img.youtube.com/vi/RS6By_pE7uo/0.jpg",
),
contentDescription = null,
contentScale = ContentScale.FillWidth,
)
}
}
}
}

最新更新