Last active
October 29, 2018 01:06
-
-
Save vlad-kasatkin/43cb6d0bdbc7cb254e6b58f1ce05eddf to your computer and use it in GitHub Desktop.
Power levels(https://imgur.com/a/XmB2pD6) custom view. Not polished, does not provide customisation. Just a good starting point
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
| import android.content.Context | |
| import android.graphics.Canvas | |
| import android.graphics.Color | |
| import android.graphics.Paint | |
| import android.graphics.RectF | |
| import android.util.AttributeSet | |
| import android.view.View | |
| class Circles : View { | |
| constructor(context: Context) : super(context, null) | |
| constructor(context: Context, attributes: AttributeSet) : super(context, attributes) | |
| companion object { | |
| private const val MAX_POWER_LEVEL = 6 | |
| } | |
| private val strokeWidthPx = 10f | |
| private val disabledPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { | |
| color = Color.BLACK | |
| strokeWidth = strokeWidthPx | |
| style = Paint.Style.STROKE | |
| } | |
| private val enabledPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { | |
| color = Color.RED | |
| strokeWidth = strokeWidthPx | |
| style = Paint.Style.STROKE | |
| } | |
| private val center | |
| get() = width / 2 | |
| private val step | |
| get() = center / MAX_POWER_LEVEL | |
| private val initialBox = RectF() | |
| private fun resetInitialBox() { | |
| val boxSize = (width / MAX_POWER_LEVEL) - (strokeWidthPx / 2) | |
| val distanceFromCenter = boxSize / 2 | |
| return initialBox.set( | |
| center - distanceFromCenter, | |
| center - distanceFromCenter, | |
| center + distanceFromCenter, | |
| center + distanceFromCenter | |
| ) | |
| } | |
| override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { | |
| val measuredWidth = MeasureSpec.getSize(widthMeasureSpec) | |
| val measureHeight = MeasureSpec.getSize(heightMeasureSpec) | |
| val squareMeasureSpec = MeasureSpec.makeMeasureSpec(Math.min(measureHeight, measuredWidth), MeasureSpec.EXACTLY) | |
| super.onMeasure(squareMeasureSpec, squareMeasureSpec) | |
| } | |
| var powerLevel = 0 | |
| set(value) { | |
| require(value <= MAX_POWER_LEVEL && value >= -MAX_POWER_LEVEL) { | |
| "Max powerlevel is (-)$MAX_POWER_LEVEL, but received $value" | |
| } | |
| field = value | |
| invalidate() | |
| } | |
| override fun onDraw(canvas: Canvas) { | |
| resetInitialBox() | |
| drawCircleForPowerLevel(canvas, initialBox, 1) | |
| for (power in 1 until MAX_POWER_LEVEL) { | |
| with(initialBox) { | |
| top -= step | |
| left -= step | |
| right += step | |
| bottom += step | |
| } | |
| drawCircleForPowerLevel(canvas, initialBox, power + 1) | |
| } | |
| super.onDraw(canvas) | |
| } | |
| private fun drawCircleForPowerLevel(canvas: Canvas, box: RectF, currentPower: Int) { | |
| val topPaint = if (currentPower in 1..powerLevel) enabledPaint else disabledPaint | |
| canvas.drawArc(box, 225f, 90f, false, topPaint) | |
| val bottomPaint = if (-currentPower in powerLevel..-1) enabledPaint else disabledPaint | |
| canvas.drawArc(box, 45f, 90f, false, bottomPaint) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment