Last active
June 22, 2020 14:51
-
-
Save zsoltk/cbd32e4466c29bec944b9ce4fae28dd7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val rotation = FloatPropKey() | |
private fun createDefinition(duration: Int) = transitionDefinition { | |
state(0) { this[rotation] = 0f } | |
state(1) { this[rotation] = 360f } | |
transition { | |
rotation using repeatable { | |
animation = tween { | |
easing = LinearEasing | |
this.duration = duration | |
} | |
iterations = Infinite | |
} | |
} | |
} | |
@Composable | |
fun RotateIndefinitely(durationPerRotation: Int, children: @Composable() () -> Unit) { | |
Transition(definition = createDefinition(durationPerRotation), initState = 0, toState = 1) { | |
Rotate(it[rotation], children) | |
} | |
} | |
@Composable | |
fun Rotate(degree: Float, children: @Composable() () -> Unit) { | |
Draw(children = children) { canvas, parent -> | |
val halfWidth = parent.width.value / 2 | |
val halfHeight = parent.height.value / 2 | |
canvas.save() | |
canvas.translate(halfWidth, halfHeight) | |
canvas.rotate(degree) | |
canvas.translate(-halfWidth, -halfHeight) | |
drawChildren() | |
canvas.restore() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment