Last active
February 10, 2020 11:57
-
-
Save vbuberen/86710f4f36de9d6342a9a12899dab3bb to your computer and use it in GitHub Desktop.
Compass rotation animation for Android View
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 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