Jetpack Compose 中图像的线性渐变

发布于 2025-01-13 07:28:12 字数 1083 浏览 2 评论 0原文

我试图在背景图像上显示线性渐变,以便更轻松地阅读图像上的文本。但由于缺乏文档,要做到这一点并不容易。我想完成类似这个的事情。

这是我的组件:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百思不得你姐 2025-01-20 07:28:12

您可以使用 Box 创建一个可组合项,将每个可组合项叠加在一起。它看起来像这样(伪代码):

@Composable
fun ShadowedImage(modifier = Modifier) {
  Box(modifier) {
    Image(modifier = Modifier.fillMaxSize())
    Surface(modifier = Modifier.fillMaxSize().background(brush = gradient))
    Text(text = "Your text")
  }
}

You can create a composable with a Box to pill each composables on top of each other. It would look like this (pseudo code):

@Composable
fun ShadowedImage(modifier = Modifier) {
  Box(modifier) {
    Image(modifier = Modifier.fillMaxSize())
    Surface(modifier = Modifier.fillMaxSize().background(brush = gradient))
    Text(text = "Your text")
  }
}
坏尐絯℡ 2025-01-20 07:28:12

您的解决方案位于以下代码中:

@Composable
override fun MakeComposable(screen: ScreenID?, onEvent: (ScreenEvent) -> Unit) {
    if (screen == null) return

    val gradient = Brush.linearGradient(
        colors = listOf(Color.Transparent, Color.Black), // Transparent to black gradient
        start = Offset(0f, 0f),
        end = Offset(0f, 1000f), // Vertical gradient
        tileMode = TileMode.Clamp
    )

    ComposeTestTheme {
        Box(modifier = Modifier.fillMaxSize()) {
            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)
                }
            }

            // Gradient overlay
            Box(
                modifier = Modifier
                    .fillMaxSize() // Fill the entire screen to cover the image
                    .background(gradient)
            )
        }
    }
}

Your solution is in below code:

@Composable
override fun MakeComposable(screen: ScreenID?, onEvent: (ScreenEvent) -> Unit) {
    if (screen == null) return

    val gradient = Brush.linearGradient(
        colors = listOf(Color.Transparent, Color.Black), // Transparent to black gradient
        start = Offset(0f, 0f),
        end = Offset(0f, 1000f), // Vertical gradient
        tileMode = TileMode.Clamp
    )

    ComposeTestTheme {
        Box(modifier = Modifier.fillMaxSize()) {
            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)
                }
            }

            // Gradient overlay
            Box(
                modifier = Modifier
                    .fillMaxSize() // Fill the entire screen to cover the image
                    .background(gradient)
            )
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文