Skip to content

Commit

Permalink
Reduce motion of compass rose for small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0oked committed Nov 17, 2019
1 parent 821dc39 commit 7053a5d
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions app/src/main/java/com/bobek/compass/view/Compass.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class Compass(context: Context, attributeSet: AttributeSet) :
private lateinit var statusCardinalDirectionText: AppCompatTextView
private lateinit var compassRoseImage: AppCompatImageView

private var currentDegrees = 360

init {
inflate(context, R.layout.compass, this)
}
Expand All @@ -58,19 +60,38 @@ class Compass(context: Context, attributeSet: AttributeSet) :
}

fun setDegrees(degrees: Float) {
statusDegreesText.text = context.getString(R.string.degrees, degrees.roundToInt())
val roundedDegrees = degrees.roundToInt()

if (currentDegrees != roundedDegrees) {
currentDegrees = roundedDegrees
updateView()
}
}

private fun updateView() {
updateStatusDegreesText()
updateStatusDirectionText()

val rotation = currentDegrees.unaryMinus().toFloat()
rotateCompassRoseImage(rotation)
rotateCompassRoseTexts(rotation)
}

private fun updateStatusDegreesText() {
statusDegreesText.text = context.getString(R.string.degrees, currentDegrees)
}

val cardinalDirection = CompassUtils.determineCardinalDirection(degrees)
private fun updateStatusDirectionText() {
val cardinalDirection = CompassUtils.determineCardinalDirection(currentDegrees.toFloat())
statusCardinalDirectionText.text =
context.getString(cardinalDirection.abbreviationResourceId)
}

val rotation = degrees.unaryMinus()
private fun rotateCompassRoseImage(rotation: Float) {
compassRoseImage.rotation = rotation

rotateTexts(rotation)
}

private fun rotateTexts(rotation: Float) {
private fun rotateCompassRoseTexts(rotation: Float) {
val constraintSet = ConstraintSet()
constraintSet.clone(this)

Expand Down

0 comments on commit 7053a5d

Please sign in to comment.