How can I change the LinearGradient shader, I am u...
# compose
j
How can I change the LinearGradient shader, I am using LinearGradientShader but I don’t know how to pass it to drawRect()?
n
Linear gradient is immutable but each draw call exposed on DrawScope can take either a color or a brush
j
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
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)
        }
    })
}
The gradient example shown can be written with the above sample code and the following import:
import androidx.compose.ui.graphics.LinearGradient
❤️ 1
j
Thanks
👍 1