From 11090367b5783d33aae97637cb08360be9659ff9 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sun, 11 Jun 2017 13:51:40 -0700 Subject: Fix selection and add fade effect --- .../kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt | 4 +-- .../ca/allanwang/kau/kpref/items/KPrefItemBase.kt | 4 +-- .../kotlin/ca/allanwang/kau/views/RippleCanvas.kt | 31 +++++++++++++++++++--- library/src/main/res/layout/kau_preference.xml | 4 ++- 4 files changed, 34 insertions(+), 9 deletions(-) (limited to 'library') diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt index 13407db..36c0feb 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/KPrefActivity.kt @@ -34,14 +34,14 @@ abstract class KPrefActivity : AppCompatActivity() { fun reload(vararg index: Int) { if (index.isEmpty()) adapter.notifyAdapterDataSetChanged() - else index.forEach { adapter.notifyItemChanged(it, null) } + else index.forEach { adapter.notifyItemChanged(it) } } fun reloadByTitle(@StringRes vararg title: Int) { if (title.isEmpty()) return adapter.adapterItems.forEachIndexed { index, item -> if (title.any { item.title == it }) - adapter.notifyItemChanged(index, null) + adapter.notifyItemChanged(index) } } diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt index cae2979..a15dcc3 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefItemBase.kt @@ -30,7 +30,7 @@ abstract class KPrefItemBase(builder: KPrefAdapterBuilder, override fun onPostBindView(viewHolder: ViewHolder, textColor: Int?, accentColor: Int?) { val enabled = enabler.invoke() with(viewHolder) { - container?.isEnabled = enabled + itemView.isEnabled = enabled container?.alpha = if (enabled) 1.0f else 0.3f } } @@ -38,7 +38,7 @@ abstract class KPrefItemBase(builder: KPrefAdapterBuilder, override fun unbindView(holder: ViewHolder) { super.unbindView(holder) with(holder) { - container?.isEnabled = true + itemView.isEnabled = true container?.alpha = 1.0f } } diff --git a/library/src/main/kotlin/ca/allanwang/kau/views/RippleCanvas.kt b/library/src/main/kotlin/ca/allanwang/kau/views/RippleCanvas.kt index eaa9121..805fb21 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/views/RippleCanvas.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/views/RippleCanvas.kt @@ -1,5 +1,6 @@ package ca.allanwang.kau.views +import android.animation.ArgbEvaluator import android.animation.ValueAnimator import android.content.Context import android.graphics.Canvas @@ -71,16 +72,21 @@ class RippleCanvas @JvmOverloads constructor( return color.adjustAlpha(factor) } - fun ripple(color: Int, startX: Float = 0f, startY: Float = 0f, duration: Int = 1000, fade: Boolean = Color.alpha(color) != 255) { + /** + * Creates a ripple effect from the given starting values + * [fade] will gradually transition previous ripples to a transparent color so the resulting background is what we want + * this is typically only necessary if the ripple color has transparency + */ + fun ripple(color: Int, startX: Float = 0f, startY: Float = 0f, duration: Long = 600L, fade: Boolean = Color.alpha(color) != 255) { val w = width.toFloat() val h = height.toFloat() val x = when (startX) { - MIDDLE -> w/2 + MIDDLE -> w / 2 END -> w else -> startX } val y = when (startY) { - MIDDLE -> h/2 + MIDDLE -> h / 2 END -> h else -> startY } @@ -88,7 +94,7 @@ class RippleCanvas @JvmOverloads constructor( val ripple = Ripple(color, x, y, 0f, maxRadius, fade) ripples.add(ripple) val animator = ValueAnimator.ofFloat(0f, maxRadius) - animator.duration = duration.toLong() + animator.duration = duration animator.addUpdateListener { animation -> ripple.radius = animation.animatedValue as Float invalidate() @@ -96,12 +102,29 @@ class RippleCanvas @JvmOverloads constructor( animator.start() } + /** + * Sets a color directly; clears ripple queue if it exists + */ fun set(color: Int) { baseColor = color ripples.clear() invalidate() } + /** + * Sets a color directly but with a transition + */ + fun fade(color: Int, duration: Long = 300L) { + ripples.clear() + val animator = ValueAnimator.ofObject(ArgbEvaluator(), baseColor, color) + animator.duration = duration + animator.addUpdateListener { animation -> + baseColor = animation.animatedValue as Int + invalidate() + } + animator.start() + } + internal class Ripple(val color: Int, val x: Float, val y: Float, diff --git a/library/src/main/res/layout/kau_preference.xml b/library/src/main/res/layout/kau_preference.xml index 5123b27..4401145 100644 --- a/library/src/main/res/layout/kau_preference.xml +++ b/library/src/main/res/layout/kau_preference.xml @@ -1,4 +1,7 @@ + + +