From 28ea5904360f8e1756003439b9046a972f6734ff Mon Sep 17 00:00:00 2001 From: MB Date: Fri, 15 May 2020 15:30:21 -0400 Subject: [PATCH] Add progress in text in middle - Add ability to show or hide the progress in middle of view as text value - Adjust xml attributes to show or hide the progress, change text color. - Add ability to change type face if OS is larger than 26 - Also adjusted the example to show the changes (show/hide text, progress text size and color) --- .../circularprogressbarsample/MainActivity.kt | 12 ++++ .../src/main/res/layout/activity_main.xml | 35 ++++++++++++ .../src/main/res/values/strings.xml | 1 + .../CircularProgressBar.kt | 57 +++++++++++++++++++ .../src/main/res/values/attrs.xml | 4 ++ 5 files changed, 109 insertions(+) diff --git a/circularprogressbar-example/src/main/java/com/mikhaellopez/circularprogressbarsample/MainActivity.kt b/circularprogressbar-example/src/main/java/com/mikhaellopez/circularprogressbarsample/MainActivity.kt index 6b0660f..0bfef87 100644 --- a/circularprogressbar-example/src/main/java/com/mikhaellopez/circularprogressbarsample/MainActivity.kt +++ b/circularprogressbar-example/src/main/java/com/mikhaellopez/circularprogressbarsample/MainActivity.kt @@ -28,6 +28,7 @@ class MainActivity : AppCompatActivity() { shadeSlider.onColorChanged { circularProgressBar.progressBarColor = it circularProgressBar.backgroundProgressBarColor = adjustAlpha(it, 0.3f) + circularProgressBar.progressTextColor = it } switchRoundBorder.onCheckedChange { circularProgressBar.roundBorder = it } switchProgressDirection.onCheckedChange { @@ -39,6 +40,13 @@ class MainActivity : AppCompatActivity() { // Indeterminate Mode switchIndeterminateMode.onCheckedChange { circularProgressBar.indeterminateMode = it } circularProgressBar.onIndeterminateModeChangeListener = { switchIndeterminateMode.isChecked = it } + + switchShowProgressText.onCheckedChange { + circularProgressBar.shouldShowProgressText = it + } + seekBarTextSize.onProgressChanged { + circularProgressBar.progressTextSize = it + MIN_TEXT_SIZE + } } //region Extensions @@ -91,4 +99,8 @@ class MainActivity : AppCompatActivity() { return Color.argb(alpha, red, green, blue) } + companion object { + const val MIN_TEXT_SIZE = 50 + } + } \ No newline at end of file diff --git a/circularprogressbar-example/src/main/res/layout/activity_main.xml b/circularprogressbar-example/src/main/res/layout/activity_main.xml index b2a6b37..85a8df3 100755 --- a/circularprogressbar-example/src/main/res/layout/activity_main.xml +++ b/circularprogressbar-example/src/main/res/layout/activity_main.xml @@ -165,6 +165,41 @@ + + + + + + + + + + diff --git a/circularprogressbar-example/src/main/res/values/strings.xml b/circularprogressbar-example/src/main/res/values/strings.xml index b85bb0c..57924e2 100755 --- a/circularprogressbar-example/src/main/res/values/strings.xml +++ b/circularprogressbar-example/src/main/res/values/strings.xml @@ -8,5 +8,6 @@ Round border Left Right + Show progress text diff --git a/circularprogressbar/src/main/java/com/mikhaellopez/circularprogressbar/CircularProgressBar.kt b/circularprogressbar/src/main/java/com/mikhaellopez/circularprogressbar/CircularProgressBar.kt index 7033d9f..d70b25e 100755 --- a/circularprogressbar/src/main/java/com/mikhaellopez/circularprogressbar/CircularProgressBar.kt +++ b/circularprogressbar/src/main/java/com/mikhaellopez/circularprogressbar/CircularProgressBar.kt @@ -5,6 +5,7 @@ import android.animation.ValueAnimator import android.content.Context import android.content.res.Resources import android.graphics.* +import android.os.Build import android.os.Handler import android.util.AttributeSet import android.view.View @@ -20,6 +21,7 @@ class CircularProgressBar(context: Context, attrs: AttributeSet? = null) : View( private const val DEFAULT_MAX_VALUE = 100f private const val DEFAULT_START_ANGLE = 270f private const val DEFAULT_ANIMATION_DURATION = 1500L + private const val DEFAULT_TEXT_SIZE = 30 } // Properties @@ -149,6 +151,31 @@ class CircularProgressBar(context: Context, attrs: AttributeSet? = null) : View( } var onProgressChangeListener: ((Float) -> Unit)? = null var onIndeterminateModeChangeListener: ((Boolean) -> Unit)? = null + + private val textPaint = Paint() + private val canvasRect = Rect() + var progressTextColor: Int = Color.BLACK + set(value) { + field = value + textPaint.color = field + invalidate() + } + var progressTextSize: Float = DEFAULT_TEXT_SIZE.toFloat() + set(value) { + field = value + invalidate() + } + + var shouldShowProgressText: Boolean = false + set(value) { + field = value + invalidate() + } + var textTypeFace: Typeface? = null + set(value) { + field = value + invalidate() + } //endregion //region Indeterminate Mode @@ -228,6 +255,17 @@ class CircularProgressBar(context: Context, attrs: AttributeSet? = null) : View( // Indeterminate Mode indeterminateMode = attributes.getBoolean(R.styleable.CircularProgressBar_cpb_indeterminate_mode, indeterminateMode) + //should draw progress text + shouldShowProgressText = attributes.getBoolean(R.styleable.CircularProgressBar_cpb_show_progress_text, false) + progressTextColor = attributes.getInt(R.styleable.CircularProgressBar_cpb_text_color, progressBarColor) + progressTextSize = attributes.getDimensionPixelSize(R.styleable.CircularProgressBar_cpb_text_size, DEFAULT_TEXT_SIZE).toFloat() + val fontResId = attributes.getResourceId(R.styleable.CircularProgressBar_cpb_text_typeface, 0) + if (fontResId != 0) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + textTypeFace = resources.getFont(fontResId) + } + } + attributes.recycle() } @@ -256,6 +294,25 @@ class CircularProgressBar(context: Context, attrs: AttributeSet? = null) : View( val angle = (if (isToRightFromIndeterminateMode || isToRightFromNormalMode) 360 else -360) * realProgress / 100 canvas.drawArc(rectF, if (indeterminateMode) startAngleIndeterminateMode else startAngle, angle, false, foregroundPaint) + drawProgressText(canvas) + } + + private fun drawProgressText(canvas: Canvas) { + if (shouldShowProgressText) { + val progressString = "${progress.toInt()}%" + canvas.getClipBounds(canvasRect) + val cHeight = canvasRect.height() + val cWidth = canvasRect.width() + textPaint.textAlign = Paint.Align.LEFT + textPaint.getTextBounds(progressString, 0, progressString.length, canvasRect) + val xPos = cWidth / 2f - canvasRect.width() / 2f - canvasRect.left + val yPos = cHeight / 2f + canvasRect.height() / 2f - canvasRect.bottom + + textPaint.color = progressTextColor + textPaint.textSize = progressTextSize + textTypeFace?.let { textPaint.typeface = textTypeFace } + canvas.drawText(progressString, xPos, yPos, textPaint) + } } override fun setBackgroundColor(backgroundColor: Int) { diff --git a/circularprogressbar/src/main/res/values/attrs.xml b/circularprogressbar/src/main/res/values/attrs.xml index 62e8321..418155c 100755 --- a/circularprogressbar/src/main/res/values/attrs.xml +++ b/circularprogressbar/src/main/res/values/attrs.xml @@ -29,5 +29,9 @@ + + + + \ No newline at end of file