Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angled gradient background in Jetpack Compose

I am trying to draw a gradient background in Jetpack Compose, and I would like the gradient to have a fixed angle regardless of the shape of the object I'm drawing into.

However, using Modifier.background(brush=...), the best I can find is linearGradient which calculates the angle from a fixed start and end point of a gradient.

For example, is there a way I can specify that I want a 45 degree angle for my gradient without knowing the final size it's going to be?

Edit: I would like a solution that can work for any given angle, not just 45 degrees.

like image 881
machfour Avatar asked Sep 13 '25 14:09

machfour


1 Answers

You can use the parameters start and end to achieve a 45 degree angle.

Something like:

val gradient45 = Brush.linearGradient(
    colors = listOf(Color.Yellow, Color.Red),
    start = Offset(0f, Float.POSITIVE_INFINITY),
    end = Offset(Float.POSITIVE_INFINITY, 0f)
)

enter image description here

like image 63
Gabriele Mariotti Avatar answered Sep 15 '25 03:09

Gabriele Mariotti