Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -91,4 +99,8 @@ class MainActivity : AppCompatActivity() {
return Color.argb(alpha, red, green, blue)
}

companion object {
const val MIN_TEXT_SIZE = 50
}

}
35 changes: 35 additions & 0 deletions circularprogressbar-example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,41 @@

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:paddingStart="15dp"
android:paddingEnd="15dp">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:text="@string/show_progress_text"
android:textColor="@color/primary"
android:textSize="14sp"
android:textStyle="bold" />

<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switchShowProgressText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

<SeekBar
android:id="@+id/seekBarTextSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:maxHeight="20dp"
android:minHeight="20dp"
android:progress="0" />

</LinearLayout>

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
<string name="round_border">Round border</string>
<string name="left">Left</string>
<string name="right">Right</string>
<string name="show_progress_text">Show progress text</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions circularprogressbar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@
<flag name="to_right" value="1" />
<flag name="to_left" value="2" />
</attr>
<attr name="cpb_show_progress_text" format="boolean" />
<attr name="cpb_text_color" format="color" />
<attr name="cpb_text_size" format="dimension" />
<attr name="cpb_text_typeface" format="reference" />
</declare-styleable>
</resources>