Skip to content

Instantly share code, notes, and snippets.

@zsoltk
Last active June 22, 2020 14:51
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsoltk/cbd32e4466c29bec944b9ce4fae28dd7 to your computer and use it in GitHub Desktop.
Save zsoltk/cbd32e4466c29bec944b9ce4fae28dd7 to your computer and use it in GitHub Desktop.
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