Skip to content

Commit

Permalink
Fixes on zero input
Browse files Browse the repository at this point in the history
  • Loading branch information
kostasantoniou committed Apr 28, 2021
1 parent 77ec554 commit 7f86683
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class FirstFragment : Fragment() {
progressBar.setProgress(30.0f)

val valueProgressBar: ValueProgressBar = view.findViewById(R.id.valuesProgressBar)
valueProgressBar.setValues(300.0f,20.0f)
valueProgressBar.setValues(0.0f, 1.0f)


}
}
6 changes: 3 additions & 3 deletions app/src/main/res/layout/fragment_first.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
app:progressLeftColor="#77B9C1"
app:progressRightColor="#45718B"
app:textSize="18sp" />

it
<com.cliff.comparingperformancebar.ValueProgressBar
android:id="@+id/valuesProgressBar"
android:layout_width="match_parent"
Expand All @@ -35,8 +35,8 @@
app:vpb_progressLeftColor="#77B9C1"
app:vpb_progressRightColor="#45718B"
app:vpb_textSize="18sp"
app:vpb_valueLeft="240"
app:vpb_valueRight="80" />
app:vpb_valueLeft="6"
app:vpb_valueRight="10" />


</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ buildscript {
repositories {
google()
jcenter()
mavenCentral()

}
dependencies {
classpath "com.android.tools.build:gradle:4.1.3"
Expand All @@ -18,6 +20,8 @@ allprojects {
repositories {
google()
jcenter()
mavenCentral()

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class ValueProgressBar @JvmOverloads constructor(
private var progressPadding = 5.0f
private var spaceAtCenter = 0.0f

private var textLeftSizeWidth: Float = 0.0f
private var textRightSizeWidth: Float = 0.0f

private var typedArray: TypedArray? = null

init {
Expand All @@ -41,11 +44,17 @@ class ValueProgressBar @JvmOverloads constructor(
private fun initAttributes() {
typedArray?.apply {
currentLeftValue =
abs(getInt(R.styleable.ValueProgressBar_vpb_valueLeft, 50))
kotlin.math.abs(getInt(R.styleable.ValueProgressBar_vpb_valueLeft, 50))
.toFloat()
currentRightValue =
abs(getInt(R.styleable.ValueProgressBar_vpb_valueRight, 50))
kotlin.math.abs(getInt(R.styleable.ValueProgressBar_vpb_valueRight, 50))
.toFloat()

textLeftSizeWidth =
percentageTextPaint.measureText("%d".format(currentLeftValue.toInt()))
textRightSizeWidth =
percentageTextPaint.measureText("%d".format(currentRightValue.toInt()))

progressRightPaint.color =
getColor(R.styleable.ValueProgressBar_vpb_progressRightColor, Color.RED)
progressLeftPaint.color =
Expand All @@ -56,7 +65,6 @@ class ValueProgressBar @JvmOverloads constructor(
Color.WHITE
)


percentageTextPaint.textSize =
getDimensionPixelSize(R.styleable.ValueProgressBar_vpb_textSize, 32).toFloat()

Expand All @@ -70,17 +78,11 @@ class ValueProgressBar @JvmOverloads constructor(
}
}

fun setLeftValue(progressValue: Float = 10f) {
currentLeftValue = progressValue
}

fun setRightValue(progressValue: Float = 10f) {
currentLeftValue = progressValue
}

fun setValues(progressLeftValue: Float = 10f, progressRightValue: Float = 10f) {
currentLeftValue = progressLeftValue
currentRightValue = progressRightValue
textLeftSizeWidth = percentageTextPaint.measureText("%d".format(currentLeftValue.toInt()))
textRightSizeWidth = percentageTextPaint.measureText("%d".format(currentRightValue.toInt()))
invalidate()
}

Expand All @@ -95,45 +97,48 @@ class ValueProgressBar @JvmOverloads constructor(
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
drawProgressBar(canvas)


val textLeftSizeWidth =
percentageTextPaint.measureText("%d".format(currentLeftValue.toInt()))

val textRightSizeWidth =
percentageTextPaint.measureText("%d".format(currentRightValue.toInt()))

canvas?.drawText(
"%d".format(currentLeftValue.toInt()),
containerRectF.left + textLeftSizeWidth / 2,
containerRectF.left + (textLeftSizeWidth / 2) + textLeftSizeWidth,
containerRectF.centerY() + percentageTextPaint.textSize / 2,
percentageTextPaint
)

canvas?.drawText(
"%d".format(currentRightValue.toInt()),
containerRectF.right - textRightSizeWidth - (textRightSizeWidth / 2),
containerRectF.right - textRightSizeWidth * 2 - (textRightSizeWidth * 2),
containerRectF.centerY() + percentageTextPaint.textSize / 2,
percentageTextPaint
)
}

private fun drawProgressBar(canvas: Canvas?) {
val total = currentLeftValue + currentRightValue
var total = currentLeftValue + currentRightValue
var tempLeftValue = currentLeftValue
var tempRightValue = currentRightValue

if (currentRightValue == 0f && currentLeftValue == 0f) {
total = 2f
tempRightValue = 1f
tempLeftValue = 1f
}

val pathLeft = drawLeftProgress(
containerRectF,
(((currentLeftValue / total) * containerRectF.right)),
progressPadding, spaceAtCenter
)
if (tempLeftValue != 0f) {
val pathLeft = drawLeftProgress(
containerRectF,
(((tempLeftValue / total) * containerRectF.right)),
progressPadding, spaceAtCenter
)
canvas?.drawPath(pathLeft, progressLeftPaint)
}

val pathRight = drawRightProgress(
containerRectF,
((currentRightValue / total) * containerRectF.right),
progressPadding, spaceAtCenter
)
canvas?.drawPath(pathLeft, progressLeftPaint)
canvas?.drawPath(pathRight, progressRightPaint)
if (tempRightValue != 0f) {
val pathRight = drawRightProgress(
containerRectF,
((tempRightValue / total) * containerRectF.right),
progressPadding, spaceAtCenter
)
canvas?.drawPath(pathRight, progressRightPaint)
}
}


Expand Down

0 comments on commit 7f86683

Please sign in to comment.