From b4e012e15a173a1c032b13bad2ae1007101ada28 Mon Sep 17 00:00:00 2001 From: Hafiz Date: Mon, 20 Jun 2022 03:01:57 -0400 Subject: [PATCH] Set CircularProgressView ovalsize onLayout with custom attribute --- .../habitica/ui/activities/StatsActivity.kt | 5 ----- .../habitica/ui/views/CircularProgressView.kt | 20 ++++++++++++++++--- wearos/src/main/res/layout/activity_stats.xml | 12 +++++++---- wearos/src/main/res/values/attrs.xml | 3 +++ 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/activities/StatsActivity.kt b/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/activities/StatsActivity.kt index df28f5c9c..9b55bf167 100644 --- a/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/activities/StatsActivity.kt +++ b/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/activities/StatsActivity.kt @@ -38,12 +38,10 @@ class StatsActivity : BaseActivity() { } private fun updateBarViews(stats: Stats, height: Int) { - binding.hpBar.ovalSize = ((height / 2) - 10) binding.hpBar.setBarColor(R.color.hp_bar_color) binding.hpBar.setPercentageValues(stats.hp?.toInt() ?: 0, stats.maxHealth ?: 0) binding.hpBar.animateProgress() - binding.expBar.ovalSize = ((height / 2) - 28) binding.expBar.setBarColor(R.color.exp_bar_color) binding.expBar.setPercentageValues(stats.exp?.toInt() ?: 0, stats.toNextLevel ?: 0) binding.expBar.animateProgress() @@ -51,13 +49,10 @@ class StatsActivity : BaseActivity() { if (stats.lvl ?: 0 < 10) { binding.mpBar.visibility = View.GONE } else { - binding.mpBar.ovalSize = ((height / 2) - 46) binding.mpBar.setBarColor(R.color.mpColor) binding.mpBar.setPercentageValues(stats.mp?.toInt() ?: 0, stats.maxMP ?: 0) binding.mpBar.animateProgress() } - - } private fun updateStatViews(stats: Stats) { diff --git a/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/views/CircularProgressView.kt b/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/views/CircularProgressView.kt index 2f3543a76..5159fa39a 100644 --- a/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/views/CircularProgressView.kt +++ b/wearos/src/main/java/com/habitrpg/wearos/habitica/ui/views/CircularProgressView.kt @@ -16,11 +16,19 @@ class CircularProgressView( ) : View(context, attrs) { private val ovalSpace = RectF() private val parentArcColor = context?.resources?.getColor(R.color.bar_background_color, null) ?: Color.GRAY - var fillArcColor = context?.resources?.getColor(R.color.hp_bar_color, null) ?: parentArcColor - var ovalSize = 200 + private var fillArcColor = context?.resources?.getColor(R.color.hp_bar_color, null) ?: parentArcColor + private var ovalSize = (resources.displayMetrics.heightPixels / 2) private var currentPercentage = 55 private var PERCENTAGE_DIVIDER = 180 private val ARC_FULL_ROTATION_DEGREE = 360 + val attributes = context?.theme?.obtainStyledAttributes( + attrs, + R.styleable.CircularProgressView, + 0, 0 + ) + private val offset = attributes?.getInt(R.styleable.CircularProgressView_offset, 0) + + private val parentArcPaint = Paint().apply { style = Paint.Style.STROKE @@ -45,6 +53,12 @@ class CircularProgressView( } } + override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { + super.onLayout(changed, left, top, right, bottom) + offset?.let { ovalSize = (height / 2) - it } + invalidate() + } + private fun setSpace() { val horizontalCenter = (width.div(2)).toFloat() val verticalCenter = (height.div(2)).toFloat() @@ -63,7 +77,7 @@ class CircularProgressView( } private fun drawInnerArc(canvas: Canvas) { - var percentageToFill = getCurrentPercentageToFill() + val percentageToFill = getCurrentPercentageToFill() canvas.drawArc(ovalSpace, 270f, percentageToFill, false, fillArcPaint) } diff --git a/wearos/src/main/res/layout/activity_stats.xml b/wearos/src/main/res/layout/activity_stats.xml index 3ea3d1a4f..7fc930374 100644 --- a/wearos/src/main/res/layout/activity_stats.xml +++ b/wearos/src/main/res/layout/activity_stats.xml @@ -1,22 +1,26 @@ + android:layout_height="match_parent" + xmlns:app="http://schemas.android.com/apk/res-auto"> + android:layout_height="match_parent" + app:offset="10"/> + android:layout_height="match_parent" + app:offset="28"/> + android:layout_height="match_parent" + app:offset="46"/> + + + \ No newline at end of file