我正在顯示來自 YouTube 的縮略圖。這些影像的頂部和底部都有黑色矩形。
我想顯示沒有那個矩形的影像。我如何設定垂直“填充”以洗掉黑色矩形。
Image(
painter = rememberImagePainter("https://img.youtube.com/vi/RS6By_pE7uo/0.jpg"),
modifier = Modifier
.width(itemWidth)
.height(photoImageHeight)
contentScale = ContentScale.FillBounds
)
uj5u.com熱心網友回復:
將影像放在一個框內,框的寬度與影像相同,但其高度減少了影像上方和下方黑條使用的高度。然后使用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,
)
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/366490.html
標籤:安卓 安卓工作室 科特林 android-jetpack-compose android-jetpack
