Jetpack Compose 中图像的线性渐变
我试图在背景图像上显示线性渐变,以便更轻松地阅读图像上的文本。但由于缺乏文档,要做到这一点并不容易。我想完成类似这个的事情。
这是我的组件:
@Composable
override fun MakeComposable(screen: ScreenID?, onEvent: (ScreenEvent) -> Unit) {
if (screen == null) return
val gradient = Brush.linearGradient(
colors = listOf(Color.Transparent, Color.Black),
start = Offset.Zero,
end = Offset.Infinite,
tileMode = TileMode.Clamp
)
ComposeTestTheme {
Box(modifier = Modifier.background(color = MaterialTheme.colors.surface))
{
screen.backgroundImage?.let { ui.Image(ImageData(url = it), onEvent = onEvent) }
LazyColumn(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.fillMaxSize()) {
this.items(screen.modules) { module ->
ModuleComposable(ui, module, onEvent)
}
}
}
Box(modifier = Modifier.fillMaxSize().background(gradient))
}
}
I'm trying to display a linear gradient over a background image so it's easier to read the text on the image. But do to the lack of documentation it's not easy to do. I want to accomplish something like this.
Here is my component:
@Composable
override fun MakeComposable(screen: ScreenID?, onEvent: (ScreenEvent) -> Unit) {
if (screen == null) return
val gradient = Brush.linearGradient(
colors = listOf(Color.Transparent, Color.Black),
start = Offset.Zero,
end = Offset.Infinite,
tileMode = TileMode.Clamp
)
ComposeTestTheme {
Box(modifier = Modifier.background(color = MaterialTheme.colors.surface))
{
screen.backgroundImage?.let { ui.Image(ImageData(url = it), onEvent = onEvent) }
LazyColumn(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.fillMaxSize()) {
this.items(screen.modules) { module ->
ModuleComposable(ui, module, onEvent)
}
}
}
Box(modifier = Modifier.fillMaxSize().background(gradient))
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Box 创建一个可组合项,将每个可组合项叠加在一起。它看起来像这样(伪代码):
You can create a composable with a Box to pill each composables on top of each other. It would look like this (pseudo code):
您的解决方案位于以下代码中:
Your solution is in below code: