diff options
author | Allan Wang <me@allanwang.ca> | 2017-06-11 14:40:51 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2017-06-11 14:40:51 -0700 |
commit | ced1a5b5acb763e914f62ad1ad338c9877e606e6 (patch) | |
tree | de479786e887298d6f2e0db8e06abb9688f81991 | |
parent | 7bf0a19345366bf8339984a4d01a799424b57a1a (diff) | |
download | kau-ced1a5b5acb763e914f62ad1ad338c9877e606e6.tar.gz kau-ced1a5b5acb763e914f62ad1ad338c9877e606e6.tar.bz2 kau-ced1a5b5acb763e914f62ad1ad338c9877e606e6.zip |
Add color preference preview
5 files changed, 34 insertions, 6 deletions
diff --git a/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/CircleView.kt b/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/CircleView.kt index a40895e..1b5e0fe 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/CircleView.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/dialogs/color/CircleView.kt @@ -37,6 +37,7 @@ import ca.allanwang.kau.utils.toHSV */ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) { + private val borderWidthMicro: Float = context.getDip(1f) private val borderWidthSmall: Float = context.getDip(3f) private val borderWidthLarge: Float = context.getDip(5f) private var whiteOuterBound: Float = borderWidthLarge @@ -45,6 +46,14 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet private val whitePaint: Paint = Paint().apply { isAntiAlias = true; color = Color.WHITE } private val innerPaint: Paint = Paint().apply { isAntiAlias = true } private var selected: Boolean = false + var withBorder: Boolean = false + get() = field + set(value) { + if (field != value) { + field = value + invalidate() + } + } init { update(Color.DKGRAY) @@ -134,18 +143,19 @@ class CircleView @JvmOverloads constructor(context: Context, attrs: AttributeSet super.onDraw(canvas) val centerWidth = (measuredWidth / 2).toFloat() val centerHeight = (measuredHeight / 2).toFloat() + if (withBorder) canvas.drawCircle(centerWidth, centerHeight, centerWidth, whitePaint) if (selected) { val whiteRadius = centerWidth - whiteOuterBound val innerRadius = whiteRadius - borderWidthSmall if (whiteRadius >= centerWidth) { canvas.drawCircle(centerWidth, centerHeight, centerWidth, whitePaint) } else { - canvas.drawCircle(centerWidth, centerHeight, centerWidth, outerPaint) + canvas.drawCircle(centerWidth, centerHeight, if (withBorder) centerWidth - borderWidthMicro else centerWidth, outerPaint) canvas.drawCircle(centerWidth, centerHeight, whiteRadius, whitePaint) } canvas.drawCircle(centerWidth, centerHeight, innerRadius, innerPaint) } else { - canvas.drawCircle(centerWidth, centerHeight, centerWidth, innerPaint) + canvas.drawCircle(centerWidth, centerHeight, if (withBorder) centerWidth - borderWidthMicro else centerWidth, innerPaint) } } diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt index fc3b6ff..62f3d45 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBinder.kt @@ -46,7 +46,8 @@ class KPrefAdapterBuilder { enabler: () -> Boolean = { true }, getter: () -> Int, setter: (value: Int) -> Unit, - configs: Builder.() -> Unit = {}) = list.add(KPrefColorPicker(this, title, description, iicon, enabler, getter, setter, configs)) + configs: Builder.() -> Unit = {}, + showPreview: Boolean = true) = list.add(KPrefColorPicker(this, title, description, iicon, enabler, getter, setter, configs, showPreview)) internal val list: MutableList<KPrefItemCore> = mutableListOf() diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt index b590233..f157d1c 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefColorPicker.kt @@ -4,6 +4,7 @@ import android.support.annotation.StringRes import android.view.View import ca.allanwang.kau.R import ca.allanwang.kau.dialogs.color.Builder +import ca.allanwang.kau.dialogs.color.CircleView import ca.allanwang.kau.dialogs.color.colorPickerDialog import ca.allanwang.kau.kpref.KPrefAdapterBuilder import com.mikepenz.iconics.typeface.IIcon @@ -21,11 +22,17 @@ class KPrefColorPicker(builder: KPrefAdapterBuilder, enabler: () -> Boolean = { true }, getter: () -> Int, setter: (value: Int) -> Unit, - val configs: Builder.() -> Unit = {}) : KPrefItemBase<Int>(builder, title, description, iicon, enabler, getter, setter) { + val configs: Builder.() -> Unit = {}, + val showPreview: Boolean = false) : KPrefItemBase<Int>(builder, title, description, iicon, enabler, getter, setter) { override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { super.onPostBindView(viewHolder, textColor, accentColor) //TODO add color circle view + if (showPreview) { + val preview = viewHolder.bindInnerView<CircleView>(R.layout.kau_preference_color_preview) + preview.setBackgroundColor(pref) + preview.withBorder = true + } } @@ -33,7 +40,11 @@ class KPrefColorPicker(builder: KPrefAdapterBuilder, itemView.context.colorPickerDialog { titleRes = this@KPrefColorPicker.title defaultColor = pref - colorCallbacks.add { pref = it } + colorCallbacks.add { + pref = it + if (showPreview) + (innerContent as CircleView).setBackgroundColor(it) + } applyNestedBuilder(configs) }.show() return true diff --git a/library/src/main/res/layout/kau_preference_color_preview.xml b/library/src/main/res/layout/kau_preference_color_preview.xml new file mode 100644 index 0000000..2374971 --- /dev/null +++ b/library/src/main/res/layout/kau_preference_color_preview.xml @@ -0,0 +1,6 @@ +<ca.allanwang.kau.dialogs.color.CircleView xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/kau_pref_inner_content" + android:layout_width="40dp" + android:layout_height="40dp" + android:focusable="false" + android:clickable="false" />
\ No newline at end of file diff --git a/sample/src/main/res/values/strings.xml b/sample/src/main/res/values/strings.xml index 56addfd..d76a9e6 100644 --- a/sample/src/main/res/values/strings.xml +++ b/sample/src/main/res/values/strings.xml @@ -12,5 +12,5 @@ <string name="background_color">Background Color</string> <string name="color_custom">This selector allows custom colors</string> <string name="color_no_custom">This selector does not allow custom colors</string> - <string name="color_custom_alpha">This selector allows for custom colors with alpha values</string> + <string name="color_custom_alpha">This selector allows for custom colors with alpha values kjljkjljk ljl lijl ijlijl</string> </resources> |