From 7053a5dbf550b46e834de026c33a5887410497af Mon Sep 17 00:00:00 2001 From: Philipp Bobek Date: Sun, 17 Nov 2019 14:09:13 +0100 Subject: [PATCH] Reduce motion of compass rose for small changes --- .../java/com/bobek/compass/view/Compass.kt | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/bobek/compass/view/Compass.kt b/app/src/main/java/com/bobek/compass/view/Compass.kt index 34c82d6..3db6260 100644 --- a/app/src/main/java/com/bobek/compass/view/Compass.kt +++ b/app/src/main/java/com/bobek/compass/view/Compass.kt @@ -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) } @@ -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)