How can I change the LinearGradient shader, I am using LinearGradientShader but I don’t know how to pass it to drawRect()?
n
Nader Jawad
10/26/2020, 11:09 PM
Linear gradient is immutable but each draw call exposed on DrawScope can take either a color or a brush
j
jean pokou
10/26/2020, 11:36 PM
ok I am trying to achieve this using the linearGradient but Android Studio does not recognize the overload of LinearGradient taking vararg of ColorStop
n
Nader Jawad
10/26/2020, 11:51 PM
Copy code
@Composable
fun gradientDemo() {
val orange = Color(0xFFFFFFA500)
Box(modifier = Modifier.drawWithCache {
val gradient = LinearGradient(
0.0f to Color.Red,
0.2f to Color.Red,
0.2f to orange,
0.4f to orange,
0.4f to Color.Yellow,
0.6f to Color.Yellow,
0.6f to Color.Green,
0.8f to Color.Green,
0.8f to Color.Blue,
1.0f to Color.Blue,
startX = 0.0f,
endX = size.width,
startY = 0f,
endY = 0f
)
onDraw {
drawRect(brush = gradient)
}
})
}
Nader Jawad
10/26/2020, 11:51 PM
The gradient example shown can be written with the above sample code and the following import: