I am trying to draw a gradient on the integrity of...
# compose
i
I am trying to draw a gradient on the integrity of the screen as a fixed layer, for some reason I am not getting any preview, am I doing this wrong?
Copy code
Box(modifier = Modifier.height(100.dp).width(100.dp).drawBehind {
        HorizontalGradient(
            0.0f to Color.Red,
            0.5f to Color.Green,
            1.0f to Color.Blue,
            startX = 0.0f,
            endX = 100.0f
        )
    })
never mind the
height
and
width
i am setting on the example
a
HorizontalGradient
is a factory function for a gradient
Brush
. It's returning an object that you are not using. Save it in a
val
and use it as a
Brush
argument to one or more
draw*
functions in the
drawBehind
block.
You can also use
drawWithCache
to give yourself a place to create brushes and similar once, or only when they change, rather than every time it draws:
Copy code
Modifier.drawWithCache {
        val brush = HorizontalGradient(...)
        onDraw {
            drawCircle(brush, ...)
        }
    }
2
s
You can also use
size
from the
DrawScope
for the width or height for your startX or endX
👌 1
i
@Adam Powell that’s exactly what i was looking for. Thanks a lot!
@Se7eN nice trick! love it
🙂 1