@@ -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 ) {
0 commit comments