Skip to content

Commit 13f0b5f

Browse files
authoredOct 1, 2020
Merge pull request android#211 from android/nb/owl_gradient
[Owl] Update scrim to use `drawWithCache`
2 parents 5ae7fbd + 641e934 commit 13f0b5f

File tree

1 file changed

+9
-18
lines changed
  • Owl/app/src/main/java/com/example/owl/ui/utils

1 file changed

+9
-18
lines changed
 

‎Owl/app/src/main/java/com/example/owl/ui/utils/Scrim.kt

+9-18
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,22 @@
1616

1717
package com.example.owl.ui.utils
1818

19-
import androidx.compose.runtime.getValue
20-
import androidx.compose.runtime.mutableStateOf
21-
import androidx.compose.runtime.remember
22-
import androidx.compose.runtime.setValue
2319
import androidx.compose.ui.Modifier
24-
import androidx.compose.ui.composed
25-
import androidx.compose.ui.drawWithContent
20+
import androidx.compose.ui.drawWithCache
2621
import androidx.compose.ui.graphics.Color
2722
import androidx.compose.ui.graphics.VerticalGradient
2823

2924
/**
3025
* A [Modifier] which draws a vertical gradient
3126
*/
32-
fun Modifier.scrim(colors: List<Color>): Modifier = composed {
33-
var height by remember { mutableStateOf(0f) }
34-
val gradient = remember(colors, height) {
35-
VerticalGradient(
36-
colors = colors,
37-
startY = 0f,
38-
endY = height
39-
)
40-
}
41-
drawWithContent {
42-
drawContent()
43-
height = size.height
27+
fun Modifier.scrim(colors: List<Color>): Modifier = drawWithCache {
28+
// Use drawWithCache modifier to create and cache the gradient when size is known or changes.
29+
val gradient = VerticalGradient(
30+
colors = colors,
31+
startY = 0f,
32+
endY = size.height
33+
)
34+
onDraw {
4435
drawRect(brush = gradient)
4536
}
4637
}

0 commit comments

Comments
 (0)
Please sign in to comment.