Skip to content

Instantly share code, notes, and snippets.

@vbuberen
Last active February 10, 2020 11:57
Show Gist options
  • Save vbuberen/86710f4f36de9d6342a9a12899dab3bb to your computer and use it in GitHub Desktop.
Save vbuberen/86710f4f36de9d6342a9a12899dab3bb to your computer and use it in GitHub Desktop.
Compass rotation animation for Android View
private var currentAzimuth = TRUE_NORTH
private fun animateArrow(newDegreesValue: Float) {
var from = currentAzimuth
var to = newDegreesValue
val min = from.coerceAtMost(to)
if (abs(to - from) > HALF_CIRCLE) {
if (min == from) {
from += FULL_CIRCLE
} else {
to += FULL_CIRCLE
}
}
currentAzimuth = newDegreesValue
val rotationAngle = (to - from)
imageCompassArrow
.animate()
.rotationBy(rotationAngle)
.setInterpolator(AnticipateOvershootInterpolator())
.setDuration(ANIMATION_DURATION)
.start()
}
companion object {
private const val TRUE_NORTH = 0F
private const val FULL_CIRCLE = 360F
private const val HALF_CIRCLE = 180F
private const val ANIMATION_DURATION = 250L
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment