Michal Havryluk
09/03/2023, 5:33 PMBox(
modifier = Modifier
.drawWithCache {
// this section is cached and not called during animation
val colorList = listOf(Color.Red, Color.Green)
val squarePath = Path()
val width = 50f
onDrawBehind {
// this section is called during animation
val offset = Offset(0f, moveAnimation.value.value)
// cannot be outside onDrawBehind because needs to follow square movement
val squareBrush = Brush.verticalGradient(
colors = colorList,
startY = offset.y,
endY = offset.y + width
)
squarePath.reset()
// not using drawRect in this example - there can be more complex Path
squarePath.apply {
moveTo(offset.x, offset.y)
relativeLineTo(0f, width)
relativeLineTo(width, 0f)
relativeLineTo(0f, -width)
}
drawPath(path = squarePath, brush = squareBrush)
}
},
)
romainguy
09/03/2023, 6:25 PMromainguy
09/03/2023, 6:26 PMMichal Havryluk
09/03/2023, 6:54 PMBox(
modifier = Modifier
.drawWithCache {
// this section is cached and not called during animation
val colorList = listOf(Color.Red, Color.Green)
val width = 50f
val squarePath = Path().apply {
moveTo(0f, 0f)
relativeLineTo(0f, width)
relativeLineTo(width, 0f)
relativeLineTo(0f, -width)
}
val squareBrush = Brush.verticalGradient(
colors = colorList,
startY = 0f,
endY = 0f + width
)
onDrawBehind {
// this section is called during animation
val offset = Offset(0f, moveAnimation.value)
squarePath.translate(offset)
drawPath(path = squarePath, brush = squareBrush)
}
},
)
romainguy
09/03/2023, 7:39 PMromainguy
09/03/2023, 7:40 PMromainguy
09/03/2023, 7:40 PMMichal Havryluk
09/03/2023, 7:54 PMBox(
modifier = Modifier
.drawWithCache {
// this section is cached and not called during animation
val colorList = listOf(Color.Red, Color.Green)
val width = 50f
val squarePath = Path().apply {
moveTo(0f, 0f)
relativeLineTo(0f, width)
relativeLineTo(width, 0f)
relativeLineTo(0f, -width)
}
val squareBrush = Brush.verticalGradient(
colors = colorList,
startY = 0f,
endY = 0f + width
)
onDrawBehind {
// this section is called during animation
translate(left = 0f, top = moveAnimation.value) {
drawPath(path = squarePath, brush = squareBrush)
}
}
},
)
romainguy
09/03/2023, 7:55 PMMichal Havryluk
09/03/2023, 8:01 PM