Skip to content

Commit

Permalink
Added new bar to compare values
Browse files Browse the repository at this point in the history
  • Loading branch information
kostasantoniou committed Apr 22, 2021
1 parent c745dde commit bf6e304
Show file tree
Hide file tree
Showing 16 changed files with 263 additions and 34 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ android {

dependencies {

implementation(project(":comparingperformancebar"))
implementation(project(':cpbar'))
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import android.view.ViewGroup
import android.widget.Button
import androidx.navigation.fragment.findNavController
import com.cliff.comparingperformancebar.PercentageProgressBar
import com.cliff.comparingperformancebar.ValueProgressBar

/**
* A simple [Fragment] subclass as the default destination in the navigation.
*/
class FirstFragment : Fragment() {

private lateinit var progressBar: PercentageProgressBar

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand All @@ -27,8 +27,11 @@ class FirstFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

progressBar = view.findViewById(R.id.percentageProgressBar)
val progressBar: PercentageProgressBar = view.findViewById(R.id.percentageProgressBar)
progressBar.setProgress(30.0f)

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

}
}
16 changes: 16 additions & 0 deletions app/src/main/res/layout/fragment_first.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,21 @@
app:progressRightColor="#45718B"
app:textSize="18sp" />

<com.cliff.comparingperformancebar.ValueProgressBar
android:id="@+id/valuesProgressBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/percentageProgressBar"
app:vpb_progressLeftColor="#77B9C1"
app:vpb_progressRightColor="#45718B"
app:vpb_textSize="18sp"
app:vpb_valueLeft="240"
app:vpb_valueRight="80" />


</androidx.constraintlayout.widget.ConstraintLayout>
13 changes: 0 additions & 13 deletions comparingperformancebar/src/main/res/values/attrs.xml

This file was deleted.

File renamed without changes.
16 changes: 15 additions & 1 deletion comparingperformancebar/build.gradle → cpbar/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ plugins {
id 'kotlin-android'
}

ext {
PUBLISH_GROUP_ID = 'io.github.cliffgr'
PUBLISH_VERSION = "1.0.0"
PUBLISH_ARTIFACT_ID = 'cpbar'
}

apply from: "${rootProject.projectDir}/scripts/publish-mavencentral.gradle"

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
Expand All @@ -11,7 +19,7 @@ android {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
versionName PUBLISH_VERSION

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand All @@ -30,6 +38,12 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}

libraryVariants.all { variant ->
variant.outputs.all {
outputFileName = "${PUBLISH_ARTIFACT_ID}-${defaultConfig.versionName}-${variant.name}.aar"
}
}
}

dependencies {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class PercentageProgressBar @JvmOverloads constructor(
private fun initAttributes() {
typedArray?.apply {
currentProgressValue =
abs(getInt(R.styleable.PercentageProgressBar_percentage, 50)).coerceIn(0, 100)
abs(getInt(R.styleable.PercentageProgressBar_percentage, 40)).coerceIn(0, 100)
.toFloat()
progressRightPaint.color =
getColor(R.styleable.PercentageProgressBar_progressRightColor, Color.RED)
Expand All @@ -52,7 +52,7 @@ class PercentageProgressBar @JvmOverloads constructor(
Color.WHITE
)

percentageTextPaint.textSize = resources.getDimension(R.dimen.default_text_size)
// percentageTextPaint.textSize = resources.getDimension(R.dimen.default_text_size)
percentageTextPaint.textSize =
getDimensionPixelSize(R.styleable.PercentageProgressBar_textSize, 32).toFloat()

Expand All @@ -62,14 +62,13 @@ class PercentageProgressBar @JvmOverloads constructor(
} else {
percentageTextPaint.typeface = Typeface.create(Typeface.MONOSPACE, Typeface.BOLD);
}


recycle()
}
}

fun setProgress(@FloatRange(from = 0.0, to = 100.0) progressValue: Float = 10f) {
currentProgressValue = progressValue
invalidate()
}


Expand All @@ -88,19 +87,21 @@ class PercentageProgressBar @JvmOverloads constructor(
val textSizeWidth =
percentageTextPaint.measureText("%s%%".format(currentProgressValue.toInt()))

canvas?.drawText(
"%s%%".format(currentProgressValue.toInt()),
containerRectF.left + textSizeWidth / 2,
containerRectF.centerY() + percentageTextPaint.textSize / 2,
percentageTextPaint
)
//if (currentProgressValue >= 90)
canvas?.drawText(
"%s%%".format(currentProgressValue.toInt()),
containerRectF.left + textSizeWidth / 2,
containerRectF.centerY() + percentageTextPaint.textSize / 2,
percentageTextPaint
)

canvas?.drawText(
"%s%%".format(100 - currentProgressValue.toInt()),
containerRectF.right - textSizeWidth - (textSizeWidth / 2),
containerRectF.centerY() + percentageTextPaint.textSize / 2,
percentageTextPaint
)
// if (currentProgressValue <= 10)
canvas?.drawText(
"%s%%".format(100 - currentProgressValue.toInt()),
containerRectF.right - textSizeWidth - (textSizeWidth / 2),
containerRectF.centerY() + percentageTextPaint.textSize / 2,
percentageTextPaint
)
}

private fun drawProgressBar(canvas: Canvas?) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package com.cliff.comparingperformancebar

import android.content.Context
import android.content.res.TypedArray
import android.graphics.*
import android.text.TextPaint
import android.util.AttributeSet
import android.view.View
import androidx.annotation.FloatRange
import androidx.core.content.res.ResourcesCompat
import java.lang.Math.abs

class ValueProgressBar @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

private val progressLeftPaint: Paint = Paint()
private val progressRightPaint: Paint = Paint()
private var percentageTextPaint = TextPaint()

private lateinit var containerRectF: RectF
private var currentLeftValue: Float = 0.0f
private var currentRightValue: Float = 0.0f
private var progressPadding = 5.0f
private var spaceAtCenter = 0.0f

private var typedArray: TypedArray? = null

init {
typedArray = context.theme.obtainStyledAttributes(
attrs,
R.styleable.ValueProgressBar,
0,
0
)
initAttributes()
}

private fun initAttributes() {
typedArray?.apply {
currentLeftValue =
abs(getInt(R.styleable.ValueProgressBar_vpb_valueLeft, 50))
.toFloat()
currentRightValue =
abs(getInt(R.styleable.ValueProgressBar_vpb_valueRight, 50))
.toFloat()
progressRightPaint.color =
getColor(R.styleable.ValueProgressBar_vpb_progressRightColor, Color.RED)
progressLeftPaint.color =
getColor(R.styleable.ValueProgressBar_vpb_progressLeftColor, Color.BLACK)

percentageTextPaint.color = getColor(
R.styleable.ValueProgressBar_vpb_textColor,
Color.WHITE
)


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

val fontId = getResourceId(R.styleable.ValueProgressBar_android_fontFamily, 0)
if (fontId != 0) {
percentageTextPaint.typeface = ResourcesCompat.getFont(context, fontId)
} else {
percentageTextPaint.typeface = Typeface.create(Typeface.MONOSPACE, Typeface.BOLD);
}
recycle()
}
}

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
invalidate()
}


override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
containerRectF = RectF()
containerRectF.set(0f, 0f, w.toFloat(), h.toFloat())
spaceAtCenter = ((0.05 * containerRectF.right) / 2).toFloat()
}

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.centerY() + percentageTextPaint.textSize / 2,
percentageTextPaint
)

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

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

val pathLeft = drawLeftProgress(
containerRectF,
(((currentLeftValue / total) * containerRectF.right)),
progressPadding, spaceAtCenter
)

val pathRight = drawRightProgress(
containerRectF,
((currentRightValue / total) * containerRectF.right),
progressPadding, spaceAtCenter
)
canvas?.drawPath(pathLeft, progressLeftPaint)
canvas?.drawPath(pathRight, progressRightPaint)
}


private fun drawLeftProgress(
rectF: RectF,
width: Float,
padding: Float,
spaceInTheMiddle: Float
): Path {
val path = Path()
with(rectF) {
path.moveTo(left + padding, padding)
path.lineTo(left + padding, bottom - padding)
path.lineTo(
left + width + (right * 0.05f) - spaceInTheMiddle,
bottom - padding
)
path.lineTo(
left + width - (right * 0.05f) - spaceInTheMiddle,
padding
)
}
path.close()
return path
}

private fun drawRightProgress(
rectF: RectF,
width: Float,
padding: Float,
spaceInTheMiddle: Float
): Path {
val path = Path()
with(rectF) {
path.moveTo(right - padding, padding)
path.lineTo(right - padding, bottom - padding)
path.lineTo(
right - width + (right * 0.05f) + spaceInTheMiddle,
bottom - padding
)
path.lineTo(
right - width - (right * 0.05f) + spaceInTheMiddle,
padding
)
}
path.close()
return path
}
}
23 changes: 23 additions & 0 deletions cpbar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="PercentageProgressBar">
<attr name="percentage" format="integer" />
<attr name="progressLeftColor" format="color" />
<attr name="progressRightColor" format="color" />
<attr name="progressBackgroundColor" format="color" />
<attr name="textColor" format="color"/>
<attr name="textSize" format="dimension"/>
<attr name="android:fontFamily" />
</declare-styleable>

<declare-styleable name="ValueProgressBar">
<attr name="vpb_valueLeft" format="integer" />
<attr name="vpb_valueRight" format="integer" />
<attr name="vpb_progressLeftColor" format="color" />
<attr name="vpb_progressRightColor" format="color" />
<attr name="vpb_progressBackgroundColor" format="color" />
<attr name="vpb_textColor" format="color"/>
<attr name="vpb_textSize" format="dimension"/>
<attr name="android:fontFamily" />
</declare-styleable>
</resources>
File renamed without changes.
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
include ':comparingperformancebar'
include ':cpbar'
include ':app'
rootProject.name = "Sample"

0 comments on commit bf6e304

Please sign in to comment.