Set CircularProgressView ovalsize onLayout with custom attribute

This commit is contained in:
Hafiz 2022-06-20 03:01:57 -04:00
parent b819f9c372
commit b4e012e15a
4 changed files with 28 additions and 12 deletions

View file

@ -38,12 +38,10 @@ class StatsActivity : BaseActivity<ActivityStatsBinding, StatsViewModel>() {
}
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<ActivityStatsBinding, StatsViewModel>() {
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) {

View file

@ -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)
}

View file

@ -1,22 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.habitrpg.wearos.habitica.ui.views.CircularProgressView
android:id="@+id/hp_bar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
app:offset="10"/>
<com.habitrpg.wearos.habitica.ui.views.CircularProgressView
android:id="@+id/exp_bar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
app:offset="28"/>
<com.habitrpg.wearos.habitica.ui.views.CircularProgressView
android:id="@+id/mp_bar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
app:offset="46"/>
<LinearLayout
android:layout_width="wrap_content"

View file

@ -4,4 +4,7 @@
<attr name="drawable" format="integer" />
<attr name="drawFromTop" format="boolean" />
</declare-styleable>
<declare-styleable name="CircularProgressView">
<attr name="offset" format="integer" />
</declare-styleable>
</resources>