diff options
author | Allan Wang <me@allanwang.ca> | 2019-12-31 19:56:44 -0800 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2019-12-31 19:56:44 -0800 |
commit | b0209fa6bdac6beffd8b0259e68e52dfadbcb9a7 (patch) | |
tree | 85ef35ec64462a0d65cbb865a377ded53ed383ad /colorpicker | |
parent | 1d71665d55876980ae0926d55b537db62cba4b17 (diff) | |
download | kau-b0209fa6bdac6beffd8b0259e68e52dfadbcb9a7.tar.gz kau-b0209fa6bdac6beffd8b0259e68e52dfadbcb9a7.tar.bz2 kau-b0209fa6bdac6beffd8b0259e68e52dfadbcb9a7.zip |
Allow option to disable CircleView selected ring
Diffstat (limited to 'colorpicker')
-rw-r--r-- | colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt | 27 | ||||
-rw-r--r-- | colorpicker/src/main/res-public/values/attrs.xml | 6 |
2 files changed, 29 insertions, 4 deletions
diff --git a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt index 27849bc..d2cda83 100644 --- a/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt +++ b/colorpicker/src/main/kotlin/ca/allanwang/kau/colorpicker/CircleView.kt @@ -49,7 +49,11 @@ import kotlin.math.roundToInt * An extension of MaterialDialog's CircleView with animation selection * [https://github.com/afollestad/material-dialogs/blob/master/commons/src/main/java/com/afollestad/materialdialogs/color/CircleView.java] */ -class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : +class CircleView @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0 +) : FrameLayout(context, attrs, defStyleAttr) { private val borderWidthMicro: Float = context.getDip(1f) @@ -70,10 +74,22 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet invalidate() } } + var selectedRing: Boolean = true init { update(Color.DKGRAY) setWillNotDraw(false) + context.theme.obtainStyledAttributes( + attrs, + R.styleable.CircleView, + 0, 0 + ).apply { + try { + selectedRing = getBoolean(R.styleable.CircleView_selectedRing, selectedRing) + } finally { + recycle() + } + } } private fun update(@ColorInt color: Int) { @@ -123,10 +139,13 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet } fun animateSelected(selected: Boolean) { - if (this.selected == selected) return + if (!this.selectedRing || this.selected == selected) return this.selected = selected // We need to draw the other bands val range = - if (selected) Pair(-borderWidthSmall, borderWidthLarge) else Pair(borderWidthLarge, -borderWidthSmall) + if (selected) Pair(-borderWidthSmall, borderWidthLarge) else Pair( + borderWidthLarge, + -borderWidthSmall + ) ValueAnimator.ofFloat(range.first, range.second).apply { reverse() duration = 150L @@ -148,7 +167,7 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet val centerWidth = (measuredWidth / 2).toFloat() val centerHeight = (measuredHeight / 2).toFloat() if (withBorder) canvas.drawCircle(centerWidth, centerHeight, centerWidth, whitePaint) - if (selected) { + if (selected && selectedRing) { val whiteRadius = centerWidth - whiteOuterBound val innerRadius = whiteRadius - borderWidthSmall if (whiteRadius >= centerWidth) { diff --git a/colorpicker/src/main/res-public/values/attrs.xml b/colorpicker/src/main/res-public/values/attrs.xml new file mode 100644 index 0000000..f208ebf --- /dev/null +++ b/colorpicker/src/main/res-public/values/attrs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<resources xmlns:tools="http://schemas.android.com/tools"> + <declare-styleable name="CircleView" tools:ignore="ResourceName"> + <attr format="boolean" name="selectedRing" /> + </declare-styleable> +</resources> |