Skip to content

Commit

Permalink
features: add change color programatically
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanArton committed May 5, 2024
1 parent 6d4e77a commit 731e9fd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View

class ArcProgressBar(context: Context, attributes: AttributeSet) : View(context, attributes) {
Expand All @@ -18,8 +17,8 @@ class ArcProgressBar(context: Context, attributes: AttributeSet) : View(context,
private var progressWidth = 20f
private var trackWidth = 20f

private var progressColor = Color.parseColor("#7084ff")
private var trackColor = Color.parseColor("#676767")
private var progressColorValue = Color.parseColor("#7084ff")
private var trackColorValue = Color.parseColor("#676767")

private var progressValue = 50f
private var progressMax = 100f
Expand All @@ -35,10 +34,10 @@ class ArcProgressBar(context: Context, attributes: AttributeSet) : View(context,

val typedArray = context.obtainStyledAttributes(attributes, R.styleable.ArcProgressBar)
try {
progressColor = typedArray.getColor(R.styleable.ArcProgressBar_progressColor, progressColor)
progressColorValue = typedArray.getColor(R.styleable.ArcProgressBar_progressColor, progressColorValue)
progressWidth = typedArray.getDimension(R.styleable.ArcProgressBar_progressWidth, progressWidth)

trackColor = typedArray.getColor(R.styleable.ArcProgressBar_trackColor, trackColor)
trackColorValue = typedArray.getColor(R.styleable.ArcProgressBar_trackColor, trackColorValue)
trackWidth = typedArray.getDimension(R.styleable.ArcProgressBar_trackWidth, trackWidth)

startAngle = typedArray.getFloat(R.styleable.ArcProgressBar_startAngle, startAngle)
Expand All @@ -48,16 +47,15 @@ class ArcProgressBar(context: Context, attributes: AttributeSet) : View(context,
progressMin = typedArray.getFloat(R.styleable.ArcProgressBar_progressMin, progressMin)
progressValueTmp = typedArray.getFloat(R.styleable.ArcProgressBar_progress, progressValue)
progressValue = scale(typedArray.getFloat(R.styleable.ArcProgressBar_progress, progressValue), sweepAngle, progressMax, progressMin)
Log.d("test", progressValue.toString())
} finally {
typedArray.recycle()
}

progressArc.strokeWidth = progressWidth
progressArc.color = progressColor
progressArc.color = progressColorValue

trackArc.strokeWidth = trackWidth
trackArc.color = trackColor
trackArc.color = trackColorValue

progressArc.strokeCap = Paint.Cap.ROUND
trackArc.strokeCap = Paint.Cap.ROUND
Expand All @@ -66,8 +64,6 @@ class ArcProgressBar(context: Context, attributes: AttributeSet) : View(context,
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)

Log.d("start", startAngle.toString())
Log.d("sweep", sweepAngle.toString())
canvas.drawArc(progressRectf, startAngle, sweepAngle, false, trackArc)

if (progressValueTmp in progressMin..progressMax) {
Expand Down Expand Up @@ -104,4 +100,18 @@ class ArcProgressBar(context: Context, attributes: AttributeSet) : View(context,
progressValue = scale(value, sweepAngle, progressMax, progressMin)
invalidate()
}

var progressColor: Int
get() = progressArc.color
set(value) {
progressArc.color = value
invalidate()
}

var trackColor: Int
get() = trackArc.color
set(value) {
trackArc.color = value
invalidate()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.juanarton.arcprogressbar

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
Expand Down

0 comments on commit 731e9fd

Please sign in to comment.