Last active
October 10, 2020 12:59
-
-
Save Skyyo/3c117ba1089bf209801bc5113b36275d to your computer and use it in GitHub Desktop.
Double progress indicators inside circle. #customView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class DoubleProgressCircleIndicator @JvmOverloads constructor( | |
| context: Context, | |
| attrs: AttributeSet? = null, | |
| defStyleAttr: Int = 0 | |
| ) : View(context, attrs, defStyleAttr) { | |
| private val darkGreenPaint = Paint().apply { | |
| style = Paint.Style.FILL | |
| color = ContextCompat.getColor(context, R.color.colorGreenDuskGreen) | |
| } | |
| private val lightGreenPaint = Paint().apply { | |
| style = Paint.Style.FILL | |
| color = ContextCompat.getColor(context, R.color.colorDayNightGreenLight) | |
| } | |
| private val whitePaint = Paint().apply { | |
| style = Paint.Style.STROKE | |
| strokeWidth = 16f | |
| isAntiAlias = true | |
| color = Color.WHITE | |
| } | |
| private val whiteLinePaint = Paint().apply { | |
| style = Paint.Style.STROKE | |
| strokeWidth = 12f | |
| isAntiAlias = true | |
| color = Color.WHITE | |
| } | |
| private val leftProgressBar = Path() | |
| private val rightProgressBar = Path() | |
| private var leftIndicatorHeight = 100f | |
| private var rightIndicatorHeight = 100f | |
| private val margin = 12f | |
| private var leftRectF = RectF() | |
| private val rightRectF = RectF() | |
| private val circlePath = Path() | |
| private val leftProgressBarPath = Path() | |
| private val rightProgressBarPath = Path() | |
| override fun onDraw(canvas: Canvas) { | |
| super.onDraw(canvas) | |
| val centerX = width / 2f | |
| val centerY = height / 2f | |
| val radius: Float = if (width > height) centerY - margin else centerX - margin | |
| val leftProgressBarHeight = leftIndicatorHeight * (height / 100) | |
| val rightProgressBarHeight = rightIndicatorHeight * (height / 100) | |
| leftRectF.apply { | |
| left = centerX | |
| right = 0f | |
| top = leftProgressBarHeight + margin * 2 | |
| bottom = height.toFloat() | |
| } | |
| rightRectF.apply { | |
| left = centerX | |
| right = width.toFloat() | |
| top = rightProgressBarHeight + margin * 2 | |
| bottom = height.toFloat() | |
| } | |
| circlePath.addCircle(centerX, centerY, radius, Path.Direction.CW) | |
| leftProgressBarPath.addRect(leftRectF, Path.Direction.CW) | |
| rightProgressBarPath.addRect(rightRectF, Path.Direction.CW) | |
| //intersecting rectangles with circle | |
| leftProgressBar.op(leftProgressBarPath, circlePath, Path.Op.INTERSECT) | |
| rightProgressBar.op(rightProgressBarPath, circlePath, Path.Op.INTERSECT) | |
| //drawing left & right progress indicators | |
| canvas.drawPath(leftProgressBar, darkGreenPaint) | |
| canvas.drawPath(rightProgressBar, lightGreenPaint) | |
| //drawing white circle with vertical line | |
| canvas.drawCircle(centerX, centerY, radius, whitePaint) | |
| canvas.drawLine(centerX, margin, centerX, height.toFloat() - margin, whiteLinePaint) | |
| } | |
| fun setProgress(leftValue: Float, rightValue: Float) { | |
| leftIndicatorHeight = 100f - leftValue | |
| rightIndicatorHeight = 100f - rightValue | |
| invalidate() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment