Skip to content

Commit d9c1309

Browse files
authored
Resolve #6 - Implement min value of the progress (#7)
* Implement min value of the progress * Release version 1.0.4
1 parent fb3ba7c commit d9c1309

File tree

5 files changed

+138
-61
lines changed

5 files changed

+138
-61
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ allprojects {
3232
And add a dependency code to your **module**'s `build.gradle` file.
3333
```gradle
3434
dependencies {
35-
implementation "com.github.skydoves:progressview:1.0.3"
35+
implementation "com.github.skydoves:progressview:1.0.4"
3636
}
3737
```
3838

@@ -54,6 +54,7 @@ Here is a basic example of implementing `ProgressView`.
5454
app:progressView_colorBackground="@color/white" // the color of the container.
5555
app:progressView_colorProgress="@color/skyBlue" // the color of the progress bar.
5656
app:progressView_progress="40" // progress value.
57+
app:progressView_min="15" // min progress value.
5758
app:progressView_max="100" // max progress value.
5859
app:progressView_autoAnimate="true" // starts filling animation automatically when finishing inflate.
5960
app:progressView_radius="12dp" // the corner radius of the progressView and progress bar.
@@ -186,6 +187,7 @@ This is how to create an instance of the `ProgressView` using kotlin dsl.
186187
val myProgressView = progressView(context) {
187188
setSize(300, 35)
188189
setProgress(70f)
190+
setMin(10f)
189191
setMax(100f)
190192
setRadius(12f)
191193
setDuration(1200L)
@@ -207,7 +209,8 @@ Attributes | Type | Default | Description
207209
--- | --- | --- | ---
208210
orientation | ProgressViewOrientation | Horizontal | ProgressView's orientation.
209211
progress | Float | 0f | value of the progress.
210-
max | Float | 100f | value of the maximum progress. the progress value can not over the max value.
212+
min | Float | 0f | value of the minimum progress. The progress value can not under the min value.
213+
max | Float | 100f | value of the maximum progress. The progress value can not over the max value.
211214
radius | Dimension | 8dp | corner radius of the ProgressView.
212215
padding | Dimension | 0dp | padding of the prograssbar.
213216
duration | Long | 1000L | duration of the animation.

app/src/main/res/layout/activity_custom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
app:progressView_labelSpace="10dp"
4141
app:progressView_labelText="70%"
4242
app:progressView_labelTypeface="bold"
43+
app:progressView_min="15"
4344
app:progressView_orientation="vertical"
4445
app:progressView_padding="1dp"
4546
app:progressView_progress="70" />

dependencies.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
ext.versions = [
22
minSdk : 16,
33
compileSdk : 29,
4-
versionCode : 4,
5-
versionName : '1.0.3',
4+
versionCode : 5,
5+
versionName : '1.0.4',
66

7-
gradleBuildTool : '3.5.0',
8-
spotlessGradle : '3.24.2',
7+
gradleBuildTool : '3.5.2',
8+
spotlessGradle : '3.26.0',
99
dokkaGradle : '0.9.17',
1010
bintrayRelease : '0.9.1',
1111

1212
kotlin : '1.3.50',
1313
androidxAppcompat : '1.1.0',
14-
transition : '1.1.0',
14+
transition : '1.2.0',
1515

1616
// for demo
1717
balloon : '1.0.6'

progressview/src/main/java/com/skydoves/progressview/ProgressView.kt

Lines changed: 126 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@ class ProgressView : FrameLayout {
4848

4949
var duration = 1000L
5050
var autoAnimate = true
51+
var min = 0f
5152
var max = 100f
5253
set(value) {
5354
field = value
5455
updateProgressView()
5556
}
5657
var progress = 0f
5758
set(value) {
58-
field = if (value >= max) max
59-
else value
59+
field = when {
60+
value >= max -> max
61+
value <= min -> min
62+
else -> value
63+
}
6064
updateProgressView()
6165
onProgressChangeListener?.onChange(field)
6266
}
@@ -125,7 +129,8 @@ class ProgressView : FrameLayout {
125129
getAttrs(attributeSet)
126130
}
127131

128-
constructor(context: Context, attributeSet: AttributeSet, defStyle: Int) : super(context, attributeSet, defStyle) {
132+
constructor(context: Context, attributeSet: AttributeSet, defStyle: Int) : super(context,
133+
attributeSet, defStyle) {
129134
getAttrs(attributeSet, defStyle)
130135
}
131136

@@ -139,7 +144,8 @@ class ProgressView : FrameLayout {
139144
}
140145

141146
private fun getAttrs(attributeSet: AttributeSet, defStyleAttr: Int) {
142-
val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.ProgressView, defStyleAttr, 0)
147+
val typedArray =
148+
context.obtainStyledAttributes(attributeSet, R.styleable.ProgressView, defStyleAttr, 0)
143149
try {
144150
setTypeArray(typedArray)
145151
} finally {
@@ -149,16 +155,20 @@ class ProgressView : FrameLayout {
149155

150156
private fun setTypeArray(a: TypedArray) {
151157
this.labelText = a.getString(R.styleable.ProgressView_progressView_labelText)
152-
this.labelSize = px2Sp(a.getDimension(R.styleable.ProgressView_progressView_labelSize, labelSize))
158+
this.labelSize =
159+
px2Sp(a.getDimension(R.styleable.ProgressView_progressView_labelSize, labelSize))
153160
this.labelSpace = a.getDimension(R.styleable.ProgressView_progressView_labelSpace, labelSpace)
154-
this.labelColorInner = a.getColor(R.styleable.ProgressView_progressView_labelColorInner, labelColorInner)
155-
this.labelColorOuter = a.getColor(R.styleable.ProgressView_progressView_labelColorOuter, labelColorOuter)
161+
this.labelColorInner =
162+
a.getColor(R.styleable.ProgressView_progressView_labelColorInner, labelColorInner)
163+
this.labelColorOuter =
164+
a.getColor(R.styleable.ProgressView_progressView_labelColorOuter, labelColorOuter)
156165
when (a.getInt(R.styleable.ProgressView_progressView_labelTypeface, Typeface.NORMAL)) {
157166
0 -> this.labelTypeface = Typeface.NORMAL
158167
1 -> this.labelTypeface = Typeface.BOLD
159168
2 -> this.labelTypeface = Typeface.ITALIC
160169
}
161-
when (a.getInt(R.styleable.ProgressView_progressView_orientation, ProgressViewOrientation.HORIZONTAL.value)) {
170+
when (a.getInt(R.styleable.ProgressView_progressView_orientation,
171+
ProgressViewOrientation.HORIZONTAL.value)) {
162172
0 -> {
163173
this.orientation = ProgressViewOrientation.HORIZONTAL
164174
this.highlightView.orientation = ProgressViewOrientation.HORIZONTAL
@@ -168,21 +178,27 @@ class ProgressView : FrameLayout {
168178
this.highlightView.orientation = ProgressViewOrientation.VERTICAL
169179
}
170180
}
181+
this.min = a.getFloat(R.styleable.ProgressView_progressView_min, min)
171182
this.max = a.getFloat(R.styleable.ProgressView_progressView_max, max)
172183
this.progress = a.getFloat(R.styleable.ProgressView_progressView_progress, progress)
173184
this.radius = a.getDimension(R.styleable.ProgressView_progressView_radius, radius)
174-
this.duration = a.getInteger(R.styleable.ProgressView_progressView_duration, duration.toInt()).toLong()
175-
this.colorBackground = a.getColor(R.styleable.ProgressView_progressView_colorBackground, colorBackground)
185+
this.duration =
186+
a.getInteger(R.styleable.ProgressView_progressView_duration, duration.toInt()).toLong()
187+
this.colorBackground =
188+
a.getColor(R.styleable.ProgressView_progressView_colorBackground, colorBackground)
176189
this.autoAnimate = a.getBoolean(R.styleable.ProgressView_progressView_autoAnimate, autoAnimate)
177190
with(this.highlightView) {
178191
alpha = a.getFloat(R.styleable.ProgressView_progressView_highlightAlpha, highlightAlpha)
179192
color = a.getColor(R.styleable.ProgressView_progressView_colorProgress, color)
180-
colorGradientStart = a.getColor(R.styleable.ProgressView_progressView_colorGradientStart, 65555)
193+
colorGradientStart =
194+
a.getColor(R.styleable.ProgressView_progressView_colorGradientStart, 65555)
181195
colorGradientEnd = a.getColor(R.styleable.ProgressView_progressView_colorGradientEnd, 65555)
182196
radius = a.getDimension(R.styleable.ProgressView_progressView_radius, radius)
183197
padding = a.getDimension(R.styleable.ProgressView_progressView_padding, padding)
184-
highlightColor = a.getColor(R.styleable.ProgressView_progressView_highlightColor, highlightColor)
185-
highlightThickness = a.getDimension(R.styleable.ProgressView_progressView_highlightWidth, highlightThickness.toFloat()).toInt()
198+
highlightColor =
199+
a.getColor(R.styleable.ProgressView_progressView_highlightColor, highlightColor)
200+
highlightThickness = a.getDimension(R.styleable.ProgressView_progressView_highlightWidth,
201+
highlightThickness.toFloat()).toInt()
186202
if (!a.getBoolean(R.styleable.ProgressView_progressView_highlighting, !highlighting)) {
187203
highlightThickness = 0
188204
}
@@ -198,7 +214,9 @@ class ProgressView : FrameLayout {
198214
super.onSizeChanged(w, h, oldw, oldh)
199215
path.apply {
200216
reset()
201-
addRoundRect(RectF(0f, 0f, w.toFloat(), h.toFloat()), floatArrayOf(radius, radius, radius, radius, radius, radius, radius, radius), Path.Direction.CCW)
217+
addRoundRect(RectF(0f, 0f, w.toFloat(), h.toFloat()),
218+
floatArrayOf(radius, radius, radius, radius, radius, radius, radius, radius),
219+
Path.Direction.CCW)
202220
}
203221
}
204222

@@ -253,11 +271,13 @@ class ProgressView : FrameLayout {
253271
}
254272

255273
private fun updateLabel() {
256-
var params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)
274+
var params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
275+
ViewGroup.LayoutParams.MATCH_PARENT)
257276
if (!isVertical()) {
258277
this.labelView.gravity = Gravity.CENTER_VERTICAL
259278
} else {
260-
params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
279+
params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
280+
ViewGroup.LayoutParams.WRAP_CONTENT)
261281
this.labelView.gravity = Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL
262282
}
263283
this.labelView.layoutParams = params
@@ -316,28 +336,21 @@ class ProgressView : FrameLayout {
316336

317337
/** animates [ProgressView]'s progress bar. */
318338
fun progressAnimate() {
319-
this.labelView.x = 0f
320-
this.highlightView.updateLayoutParams {
321-
if (isVertical()) {
322-
height = 0
323-
} else {
324-
width = 0
325-
}
326-
}
327-
val animator = ValueAnimator.ofFloat(0f, 1f)
328-
animator.duration = duration
329-
animator.addUpdateListener {
330-
val value = it.animatedValue as Float
331-
setLabelViewPosition(getLabelPosition() * value)
332-
this.highlightView.updateLayoutParams {
333-
if (isVertical()) {
334-
height = (getProgressSize() * value).toInt()
335-
} else {
336-
width = (getProgressSize() * value).toInt()
339+
ValueAnimator.ofFloat(0f, 1f).apply {
340+
duration = this@ProgressView.duration
341+
addUpdateListener {
342+
val value = it.animatedValue as Float
343+
setLabelViewPosition(getLabelPosition() * value)
344+
highlightView.updateLayoutParams {
345+
if (isVertical()) {
346+
height = (getProgressSize() * value).toInt()
347+
} else {
348+
width = (getProgressSize() * value).toInt()
349+
}
337350
}
338351
}
352+
start()
339353
}
340-
animator.start()
341354
}
342355

343356
private fun isVertical(): Boolean {
@@ -372,34 +385,93 @@ class ProgressView : FrameLayout {
372385
class Builder(context: Context) {
373386
private val progressView = ProgressView(context)
374387

375-
fun setSize(width: Int, height: Int): Builder = apply { this.progressView.layoutParams = LayoutParams(progressView.dp2Px(width), progressView.dp2Px(height)) }
388+
fun setSize(width: Int, height: Int): Builder = apply {
389+
this.progressView.layoutParams =
390+
LayoutParams(progressView.dp2Px(width), progressView.dp2Px(height))
391+
}
392+
376393
fun setHeight(value: Int): Builder = apply { this.progressView.layoutParams.height = value }
377394
fun setDuration(value: Long): Builder = apply { this.progressView.duration = value }
378395
fun setAutoAnimate(value: Boolean): Builder = apply { this.progressView.autoAnimate = value }
396+
fun setMin(value: Float): Builder = apply { this.progressView.min = value }
379397
fun setMax(value: Float): Builder = apply { this.progressView.max = value }
380398
fun setProgress(value: Float): Builder = apply { this.progressView.progress = value }
381-
fun setOrientation(value: ProgressViewOrientation): Builder = apply { this.progressView.orientation = value }
382-
fun setColorBackground(value: Int): Builder = apply { this.progressView.colorBackground = value }
399+
fun setOrientation(value: ProgressViewOrientation): Builder = apply {
400+
this.progressView.orientation = value
401+
}
402+
403+
fun setColorBackground(value: Int): Builder = apply {
404+
this.progressView.colorBackground = value
405+
}
406+
383407
fun setRadius(value: Float): Builder = apply { this.progressView.radius = value }
384408
fun setLabelText(value: String): Builder = apply { this.progressView.labelText = value }
385-
fun setLabelSize(value: Float): Builder = apply { this.progressView.labelSize = this.progressView.sp2Px(value) }
409+
fun setLabelSize(value: Float): Builder = apply {
410+
this.progressView.labelSize = this.progressView.sp2Px(value)
411+
}
412+
386413
fun setLabelSpace(value: Float): Builder = apply { this.progressView.labelSpace = value }
387-
fun setLabelColorInner(value: Int): Builder = apply { this.progressView.labelColorInner = value }
388-
fun setLabelColorOuter(value: Int): Builder = apply { this.progressView.labelColorOuter = value }
414+
fun setLabelColorInner(value: Int): Builder = apply {
415+
this.progressView.labelColorInner = value
416+
}
417+
418+
fun setLabelColorOuter(value: Int): Builder = apply {
419+
this.progressView.labelColorOuter = value
420+
}
421+
389422
fun setLabelTypeface(value: Int): Builder = apply { this.progressView.labelTypeface = value }
390-
fun setLabelTypeface(value: Typeface): Builder = apply { this.progressView.labelTypefaceObject = value }
391-
fun setProgressbarAlpha(value: Float): Builder = apply { this.progressView.highlightView.alpha = value }
392-
fun setProgressbarColor(value: Int): Builder = apply { this.progressView.highlightView.color = value }
393-
fun setProgressbarColorGradientStart(value: Int): Builder = apply { this.progressView.highlightView.colorGradientStart = value }
394-
fun setProgressbarColorGradientEnd(value: Int): Builder = apply { this.progressView.highlightView.colorGradientEnd = value }
395-
fun setProgressbarRadius(value: Float): Builder = apply { this.progressView.highlightView.radius = value }
396-
fun setProgressbarPadding(value: Float): Builder = apply { this.progressView.highlightView.padding = value }
397-
fun setHighlightColor(value: Int): Builder = apply { this.progressView.highlightView.highlightColor = value }
398-
fun setHighlighting(value: Boolean): Builder = apply { this.progressView.highlightView.highlighting = value }
399-
fun setHighlightThickness(value: Int): Builder = apply { this.progressView.highlightView.highlightThickness = value }
400-
fun setOnProgressChangeListener(value: OnProgressChangeListener): Builder = apply { this.progressView.onProgressChangeListener = value }
401-
fun setOnProgressClickListener(value: OnProgressClickListener): Builder = apply { this.progressView.onProgressClickListener = value }
402-
fun setTextForm(value: TextForm): Builder = apply { this.progressView.labelView.applyTextForm(value) }
423+
fun setLabelTypeface(value: Typeface): Builder = apply {
424+
this.progressView.labelTypefaceObject = value
425+
}
426+
427+
fun setProgressbarAlpha(value: Float): Builder = apply {
428+
this.progressView.highlightView.alpha = value
429+
}
430+
431+
fun setProgressbarColor(value: Int): Builder = apply {
432+
this.progressView.highlightView.color = value
433+
}
434+
435+
fun setProgressbarColorGradientStart(value: Int): Builder = apply {
436+
this.progressView.highlightView.colorGradientStart = value
437+
}
438+
439+
fun setProgressbarColorGradientEnd(value: Int): Builder = apply {
440+
this.progressView.highlightView.colorGradientEnd = value
441+
}
442+
443+
fun setProgressbarRadius(value: Float): Builder = apply {
444+
this.progressView.highlightView.radius = value
445+
}
446+
447+
fun setProgressbarPadding(value: Float): Builder = apply {
448+
this.progressView.highlightView.padding = value
449+
}
450+
451+
fun setHighlightColor(value: Int): Builder = apply {
452+
this.progressView.highlightView.highlightColor = value
453+
}
454+
455+
fun setHighlighting(value: Boolean): Builder = apply {
456+
this.progressView.highlightView.highlighting = value
457+
}
458+
459+
fun setHighlightThickness(value: Int): Builder = apply {
460+
this.progressView.highlightView.highlightThickness = value
461+
}
462+
463+
fun setOnProgressChangeListener(value: OnProgressChangeListener): Builder = apply {
464+
this.progressView.onProgressChangeListener = value
465+
}
466+
467+
fun setOnProgressClickListener(value: OnProgressClickListener): Builder = apply {
468+
this.progressView.onProgressClickListener = value
469+
}
470+
471+
fun setTextForm(value: TextForm): Builder = apply {
472+
this.progressView.labelView.applyTextForm(value)
473+
}
474+
403475
fun setOnProgressChangeListener(block: (Float) -> Unit): Builder = apply {
404476
this.progressView.onProgressChangeListener = object : OnProgressChangeListener {
405477
override fun onChange(progress: Float) {

progressview/src/main/res/values/attrs_progressview.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<enum name="horizontal" value="0" />
1616
<enum name="vertical" value="1" />
1717
</attr>
18+
<attr name="progressView_min" format="float" />
1819
<attr name="progressView_max" format="float" />
1920
<attr name="progressView_progress" format="float" />
2021
<attr name="progressView_radius" format="dimension" />

0 commit comments

Comments
 (0)