From d9c1309c3ca211d5baa1bbb1ae9b0ccedbb38bc0 Mon Sep 17 00:00:00 2001 From: Jaewoong Eum Date: Mon, 25 Nov 2019 00:06:26 +0900 Subject: [PATCH] Resolve #6 - Implement min value of the progress (#7) * Implement min value of the progress * Release version 1.0.4 --- README.md | 7 +- app/src/main/res/layout/activity_custom.xml | 1 + dependencies.gradle | 10 +- .../com/skydoves/progressview/ProgressView.kt | 180 ++++++++++++------ .../main/res/values/attrs_progressview.xml | 1 + 5 files changed, 138 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index 3c22ce3..e16a6a6 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ allprojects { And add a dependency code to your **module**'s `build.gradle` file. ```gradle dependencies { - implementation "com.github.skydoves:progressview:1.0.3" + implementation "com.github.skydoves:progressview:1.0.4" } ``` @@ -54,6 +54,7 @@ Here is a basic example of implementing `ProgressView`. app:progressView_colorBackground="@color/white" // the color of the container. app:progressView_colorProgress="@color/skyBlue" // the color of the progress bar. app:progressView_progress="40" // progress value. + app:progressView_min="15" // min progress value. app:progressView_max="100" // max progress value. app:progressView_autoAnimate="true" // starts filling animation automatically when finishing inflate. 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. val myProgressView = progressView(context) { setSize(300, 35) setProgress(70f) + setMin(10f) setMax(100f) setRadius(12f) setDuration(1200L) @@ -207,7 +209,8 @@ Attributes | Type | Default | Description --- | --- | --- | --- orientation | ProgressViewOrientation | Horizontal | ProgressView's orientation. progress | Float | 0f | value of the progress. -max | Float | 100f | value of the maximum progress. the progress value can not over the max value. +min | Float | 0f | value of the minimum progress. The progress value can not under the min value. +max | Float | 100f | value of the maximum progress. The progress value can not over the max value. radius | Dimension | 8dp | corner radius of the ProgressView. padding | Dimension | 0dp | padding of the prograssbar. duration | Long | 1000L | duration of the animation. diff --git a/app/src/main/res/layout/activity_custom.xml b/app/src/main/res/layout/activity_custom.xml index b6455ca..a292f39 100644 --- a/app/src/main/res/layout/activity_custom.xml +++ b/app/src/main/res/layout/activity_custom.xml @@ -40,6 +40,7 @@ app:progressView_labelSpace="10dp" app:progressView_labelText="70%" app:progressView_labelTypeface="bold" + app:progressView_min="15" app:progressView_orientation="vertical" app:progressView_padding="1dp" app:progressView_progress="70" /> diff --git a/dependencies.gradle b/dependencies.gradle index 7cbb05b..96bf063 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -1,17 +1,17 @@ ext.versions = [ minSdk : 16, compileSdk : 29, - versionCode : 4, - versionName : '1.0.3', + versionCode : 5, + versionName : '1.0.4', - gradleBuildTool : '3.5.0', - spotlessGradle : '3.24.2', + gradleBuildTool : '3.5.2', + spotlessGradle : '3.26.0', dokkaGradle : '0.9.17', bintrayRelease : '0.9.1', kotlin : '1.3.50', androidxAppcompat : '1.1.0', - transition : '1.1.0', + transition : '1.2.0', // for demo balloon : '1.0.6' diff --git a/progressview/src/main/java/com/skydoves/progressview/ProgressView.kt b/progressview/src/main/java/com/skydoves/progressview/ProgressView.kt index f9b6746..31fa6c2 100644 --- a/progressview/src/main/java/com/skydoves/progressview/ProgressView.kt +++ b/progressview/src/main/java/com/skydoves/progressview/ProgressView.kt @@ -48,6 +48,7 @@ class ProgressView : FrameLayout { var duration = 1000L var autoAnimate = true + var min = 0f var max = 100f set(value) { field = value @@ -55,8 +56,11 @@ class ProgressView : FrameLayout { } var progress = 0f set(value) { - field = if (value >= max) max - else value + field = when { + value >= max -> max + value <= min -> min + else -> value + } updateProgressView() onProgressChangeListener?.onChange(field) } @@ -125,7 +129,8 @@ class ProgressView : FrameLayout { getAttrs(attributeSet) } - constructor(context: Context, attributeSet: AttributeSet, defStyle: Int) : super(context, attributeSet, defStyle) { + constructor(context: Context, attributeSet: AttributeSet, defStyle: Int) : super(context, + attributeSet, defStyle) { getAttrs(attributeSet, defStyle) } @@ -139,7 +144,8 @@ class ProgressView : FrameLayout { } private fun getAttrs(attributeSet: AttributeSet, defStyleAttr: Int) { - val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.ProgressView, defStyleAttr, 0) + val typedArray = + context.obtainStyledAttributes(attributeSet, R.styleable.ProgressView, defStyleAttr, 0) try { setTypeArray(typedArray) } finally { @@ -149,16 +155,20 @@ class ProgressView : FrameLayout { private fun setTypeArray(a: TypedArray) { this.labelText = a.getString(R.styleable.ProgressView_progressView_labelText) - this.labelSize = px2Sp(a.getDimension(R.styleable.ProgressView_progressView_labelSize, labelSize)) + this.labelSize = + px2Sp(a.getDimension(R.styleable.ProgressView_progressView_labelSize, labelSize)) this.labelSpace = a.getDimension(R.styleable.ProgressView_progressView_labelSpace, labelSpace) - this.labelColorInner = a.getColor(R.styleable.ProgressView_progressView_labelColorInner, labelColorInner) - this.labelColorOuter = a.getColor(R.styleable.ProgressView_progressView_labelColorOuter, labelColorOuter) + this.labelColorInner = + a.getColor(R.styleable.ProgressView_progressView_labelColorInner, labelColorInner) + this.labelColorOuter = + a.getColor(R.styleable.ProgressView_progressView_labelColorOuter, labelColorOuter) when (a.getInt(R.styleable.ProgressView_progressView_labelTypeface, Typeface.NORMAL)) { 0 -> this.labelTypeface = Typeface.NORMAL 1 -> this.labelTypeface = Typeface.BOLD 2 -> this.labelTypeface = Typeface.ITALIC } - when (a.getInt(R.styleable.ProgressView_progressView_orientation, ProgressViewOrientation.HORIZONTAL.value)) { + when (a.getInt(R.styleable.ProgressView_progressView_orientation, + ProgressViewOrientation.HORIZONTAL.value)) { 0 -> { this.orientation = ProgressViewOrientation.HORIZONTAL this.highlightView.orientation = ProgressViewOrientation.HORIZONTAL @@ -168,21 +178,27 @@ class ProgressView : FrameLayout { this.highlightView.orientation = ProgressViewOrientation.VERTICAL } } + this.min = a.getFloat(R.styleable.ProgressView_progressView_min, min) this.max = a.getFloat(R.styleable.ProgressView_progressView_max, max) this.progress = a.getFloat(R.styleable.ProgressView_progressView_progress, progress) this.radius = a.getDimension(R.styleable.ProgressView_progressView_radius, radius) - this.duration = a.getInteger(R.styleable.ProgressView_progressView_duration, duration.toInt()).toLong() - this.colorBackground = a.getColor(R.styleable.ProgressView_progressView_colorBackground, colorBackground) + this.duration = + a.getInteger(R.styleable.ProgressView_progressView_duration, duration.toInt()).toLong() + this.colorBackground = + a.getColor(R.styleable.ProgressView_progressView_colorBackground, colorBackground) this.autoAnimate = a.getBoolean(R.styleable.ProgressView_progressView_autoAnimate, autoAnimate) with(this.highlightView) { alpha = a.getFloat(R.styleable.ProgressView_progressView_highlightAlpha, highlightAlpha) color = a.getColor(R.styleable.ProgressView_progressView_colorProgress, color) - colorGradientStart = a.getColor(R.styleable.ProgressView_progressView_colorGradientStart, 65555) + colorGradientStart = + a.getColor(R.styleable.ProgressView_progressView_colorGradientStart, 65555) colorGradientEnd = a.getColor(R.styleable.ProgressView_progressView_colorGradientEnd, 65555) radius = a.getDimension(R.styleable.ProgressView_progressView_radius, radius) padding = a.getDimension(R.styleable.ProgressView_progressView_padding, padding) - highlightColor = a.getColor(R.styleable.ProgressView_progressView_highlightColor, highlightColor) - highlightThickness = a.getDimension(R.styleable.ProgressView_progressView_highlightWidth, highlightThickness.toFloat()).toInt() + highlightColor = + a.getColor(R.styleable.ProgressView_progressView_highlightColor, highlightColor) + highlightThickness = a.getDimension(R.styleable.ProgressView_progressView_highlightWidth, + highlightThickness.toFloat()).toInt() if (!a.getBoolean(R.styleable.ProgressView_progressView_highlighting, !highlighting)) { highlightThickness = 0 } @@ -198,7 +214,9 @@ class ProgressView : FrameLayout { super.onSizeChanged(w, h, oldw, oldh) path.apply { reset() - addRoundRect(RectF(0f, 0f, w.toFloat(), h.toFloat()), floatArrayOf(radius, radius, radius, radius, radius, radius, radius, radius), Path.Direction.CCW) + addRoundRect(RectF(0f, 0f, w.toFloat(), h.toFloat()), + floatArrayOf(radius, radius, radius, radius, radius, radius, radius, radius), + Path.Direction.CCW) } } @@ -253,11 +271,13 @@ class ProgressView : FrameLayout { } private fun updateLabel() { - var params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT) + var params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.MATCH_PARENT) if (!isVertical()) { this.labelView.gravity = Gravity.CENTER_VERTICAL } else { - params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT) this.labelView.gravity = Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL } this.labelView.layoutParams = params @@ -316,28 +336,21 @@ class ProgressView : FrameLayout { /** animates [ProgressView]'s progress bar. */ fun progressAnimate() { - this.labelView.x = 0f - this.highlightView.updateLayoutParams { - if (isVertical()) { - height = 0 - } else { - width = 0 - } - } - val animator = ValueAnimator.ofFloat(0f, 1f) - animator.duration = duration - animator.addUpdateListener { - val value = it.animatedValue as Float - setLabelViewPosition(getLabelPosition() * value) - this.highlightView.updateLayoutParams { - if (isVertical()) { - height = (getProgressSize() * value).toInt() - } else { - width = (getProgressSize() * value).toInt() + ValueAnimator.ofFloat(0f, 1f).apply { + duration = this@ProgressView.duration + addUpdateListener { + val value = it.animatedValue as Float + setLabelViewPosition(getLabelPosition() * value) + highlightView.updateLayoutParams { + if (isVertical()) { + height = (getProgressSize() * value).toInt() + } else { + width = (getProgressSize() * value).toInt() + } } } + start() } - animator.start() } private fun isVertical(): Boolean { @@ -372,34 +385,93 @@ class ProgressView : FrameLayout { class Builder(context: Context) { private val progressView = ProgressView(context) - fun setSize(width: Int, height: Int): Builder = apply { this.progressView.layoutParams = LayoutParams(progressView.dp2Px(width), progressView.dp2Px(height)) } + fun setSize(width: Int, height: Int): Builder = apply { + this.progressView.layoutParams = + LayoutParams(progressView.dp2Px(width), progressView.dp2Px(height)) + } + fun setHeight(value: Int): Builder = apply { this.progressView.layoutParams.height = value } fun setDuration(value: Long): Builder = apply { this.progressView.duration = value } fun setAutoAnimate(value: Boolean): Builder = apply { this.progressView.autoAnimate = value } + fun setMin(value: Float): Builder = apply { this.progressView.min = value } fun setMax(value: Float): Builder = apply { this.progressView.max = value } fun setProgress(value: Float): Builder = apply { this.progressView.progress = value } - fun setOrientation(value: ProgressViewOrientation): Builder = apply { this.progressView.orientation = value } - fun setColorBackground(value: Int): Builder = apply { this.progressView.colorBackground = value } + fun setOrientation(value: ProgressViewOrientation): Builder = apply { + this.progressView.orientation = value + } + + fun setColorBackground(value: Int): Builder = apply { + this.progressView.colorBackground = value + } + fun setRadius(value: Float): Builder = apply { this.progressView.radius = value } fun setLabelText(value: String): Builder = apply { this.progressView.labelText = value } - fun setLabelSize(value: Float): Builder = apply { this.progressView.labelSize = this.progressView.sp2Px(value) } + fun setLabelSize(value: Float): Builder = apply { + this.progressView.labelSize = this.progressView.sp2Px(value) + } + fun setLabelSpace(value: Float): Builder = apply { this.progressView.labelSpace = value } - fun setLabelColorInner(value: Int): Builder = apply { this.progressView.labelColorInner = value } - fun setLabelColorOuter(value: Int): Builder = apply { this.progressView.labelColorOuter = value } + fun setLabelColorInner(value: Int): Builder = apply { + this.progressView.labelColorInner = value + } + + fun setLabelColorOuter(value: Int): Builder = apply { + this.progressView.labelColorOuter = value + } + fun setLabelTypeface(value: Int): Builder = apply { this.progressView.labelTypeface = value } - fun setLabelTypeface(value: Typeface): Builder = apply { this.progressView.labelTypefaceObject = value } - fun setProgressbarAlpha(value: Float): Builder = apply { this.progressView.highlightView.alpha = value } - fun setProgressbarColor(value: Int): Builder = apply { this.progressView.highlightView.color = value } - fun setProgressbarColorGradientStart(value: Int): Builder = apply { this.progressView.highlightView.colorGradientStart = value } - fun setProgressbarColorGradientEnd(value: Int): Builder = apply { this.progressView.highlightView.colorGradientEnd = value } - fun setProgressbarRadius(value: Float): Builder = apply { this.progressView.highlightView.radius = value } - fun setProgressbarPadding(value: Float): Builder = apply { this.progressView.highlightView.padding = value } - fun setHighlightColor(value: Int): Builder = apply { this.progressView.highlightView.highlightColor = value } - fun setHighlighting(value: Boolean): Builder = apply { this.progressView.highlightView.highlighting = value } - fun setHighlightThickness(value: Int): Builder = apply { this.progressView.highlightView.highlightThickness = value } - fun setOnProgressChangeListener(value: OnProgressChangeListener): Builder = apply { this.progressView.onProgressChangeListener = value } - fun setOnProgressClickListener(value: OnProgressClickListener): Builder = apply { this.progressView.onProgressClickListener = value } - fun setTextForm(value: TextForm): Builder = apply { this.progressView.labelView.applyTextForm(value) } + fun setLabelTypeface(value: Typeface): Builder = apply { + this.progressView.labelTypefaceObject = value + } + + fun setProgressbarAlpha(value: Float): Builder = apply { + this.progressView.highlightView.alpha = value + } + + fun setProgressbarColor(value: Int): Builder = apply { + this.progressView.highlightView.color = value + } + + fun setProgressbarColorGradientStart(value: Int): Builder = apply { + this.progressView.highlightView.colorGradientStart = value + } + + fun setProgressbarColorGradientEnd(value: Int): Builder = apply { + this.progressView.highlightView.colorGradientEnd = value + } + + fun setProgressbarRadius(value: Float): Builder = apply { + this.progressView.highlightView.radius = value + } + + fun setProgressbarPadding(value: Float): Builder = apply { + this.progressView.highlightView.padding = value + } + + fun setHighlightColor(value: Int): Builder = apply { + this.progressView.highlightView.highlightColor = value + } + + fun setHighlighting(value: Boolean): Builder = apply { + this.progressView.highlightView.highlighting = value + } + + fun setHighlightThickness(value: Int): Builder = apply { + this.progressView.highlightView.highlightThickness = value + } + + fun setOnProgressChangeListener(value: OnProgressChangeListener): Builder = apply { + this.progressView.onProgressChangeListener = value + } + + fun setOnProgressClickListener(value: OnProgressClickListener): Builder = apply { + this.progressView.onProgressClickListener = value + } + + fun setTextForm(value: TextForm): Builder = apply { + this.progressView.labelView.applyTextForm(value) + } + fun setOnProgressChangeListener(block: (Float) -> Unit): Builder = apply { this.progressView.onProgressChangeListener = object : OnProgressChangeListener { override fun onChange(progress: Float) { diff --git a/progressview/src/main/res/values/attrs_progressview.xml b/progressview/src/main/res/values/attrs_progressview.xml index ad05ce3..a9a9993 100644 --- a/progressview/src/main/res/values/attrs_progressview.xml +++ b/progressview/src/main/res/values/attrs_progressview.xml @@ -15,6 +15,7 @@ +