在 Android Jetpack Compose 中,有沒有人知道如何使影像的左側向右側緩慢淡入透明?謝謝!
編輯:抱歉,我的意思是在 Compose 中制作像這樣的影像淡入淡出,可能使用混合模式?但不知道該怎么做..
預期結果:

uj5u.com熱心網友回復:
您可以在影像上放置一個 Box 并向 Box 添加背景漸變。您必須將 Box 的大小設定為與影像相同。在這個例子中,我不打擾正確設定 Box 大小。我會把它留給你:
這是我使用的影像:
https://adelaidevet.com.au/sites/default/files/archive/files/images/cat-ginger-eyes-closed_0.jpg
val screenWidth = LocalConfiguration.current.screenWidthDp
Box(modifier = Modifier.fillMaxWidth().wrapContentHeight()) {
Image(
painterResource(R.drawable.cat),
contentDescription = "",
contentScale = ContentScale.FillWidth,
modifier = Modifier.fillMaxWidth()
)
Box(
modifier = Modifier
.requiredWidth(screenWidth.dp)
.requiredHeight(310.dp)
.background(
brush = Brush.horizontalGradient(
startX = 0f,
endX = 700f,
colors = listOf(
Color.Transparent,
Color(0xD7525252),
)
)
)
) {
}
}
uj5u.com熱心網友回復:
剛剛從如何向 Android Jetpack Compose Column or Row 添加淡入淡出邊緣效果中找到答案?
“Color.Black”表示顯示影像的區域。
謝謝你們!
Image(
painterResource(R.drawable.cat),
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier
// Workaround to enable alpha compositing
.graphicsLayer { alpha = 0.99f }
.drawWithContent {
val colors = listOf(
Color.Black,
Color.Transparent
)
drawContent()
drawRect(
brush = Brush.horizontalGradient(colors),
blendMode = BlendMode.DstIn
)
}
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/367540.html
