From b0209fa6bdac6beffd8b0259e68e52dfadbcb9a7 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Tue, 31 Dec 2019 19:56:44 -0800 Subject: Allow option to disable CircleView selected ring --- .../ca/allanwang/kau/colorpicker/CircleView.kt | 27 ++++++++++++++++++---- colorpicker/src/main/res-public/values/attrs.xml | 6 +++++ .../main/kotlin/ca/allanwang/kau/kotlin/LazyUi.kt | 17 +++++++++++++- .../kotlin/ca/allanwang/kau/utils/ViewUtils.kt | 3 ++- .../src/main/res/layout/kau_pref_color.xml | 4 +++- 5 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 colorpicker/src/main/res-public/values/attrs.xml 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 @@ + + + + + + diff --git a/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyUi.kt b/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyUi.kt index 28994e4..d3ac0f5 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyUi.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyUi.kt @@ -1,6 +1,21 @@ +/* + * Copyright 2019 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.kotlin /** * Shortcut for unsynchronized lazy block */ -fun lazyUi(initializer: () -> T): Lazy = lazy(LazyThreadSafetyMode.NONE, initializer) \ No newline at end of file +fun lazyUi(initializer: () -> T): Lazy = lazy(LazyThreadSafetyMode.NONE, initializer) diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt index a1b96f5..4757a00 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt @@ -104,7 +104,8 @@ fun View.snackbar( } fun View.snackbar( - @StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG, + @StringRes textId: Int, + duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {} ) = snackbar(context.string(textId), duration, builder) diff --git a/kpref-activity/src/main/res/layout/kau_pref_color.xml b/kpref-activity/src/main/res/layout/kau_pref_color.xml index fbb049b..4bd98ca 100644 --- a/kpref-activity/src/main/res/layout/kau_pref_color.xml +++ b/kpref-activity/src/main/res/layout/kau_pref_color.xml @@ -1,6 +1,8 @@ \ No newline at end of file + app:selectedRing="false" /> \ No newline at end of file -- cgit v1.2.3