From 6548a6ddc54539540397bfbfc89e3d4bacdddee5 Mon Sep 17 00:00:00 2001 From: skydoves Date: Mon, 9 Dec 2019 15:31:21 +0900 Subject: [PATCH 1/3] Implement devounce functionality --- app/src/main/res/layout/activity_main.xml | 3 +- .../colorpickerview/ColorPickerView.java | 50 +++++++++++++------ .../src/main/res/values/attrs_colorpicker.xml | 1 + dependencies.gradle | 2 +- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index aa07ca9..83fbc03 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -50,7 +50,8 @@ android:layout_weight="6" app:palette="@drawable/palette" app:preferenceName="Test2" - app:selector="@drawable/wheel" /> + app:selector="@drawable/wheel" + app:debounceDuration="150" /> = this.lastDebouncedTime + this.debounceDuration) { + this.lastDebouncedTime = now; + if (actionMode == ActionMode.LAST) { + if (event.getAction() == MotionEvent.ACTION_UP) { + fireColorListener(getColor(), true); + notifyToSlideBars(); + } + } else { fireColorListener(getColor(), true); notifyToSlideBars(); } - } else { - fireColorListener(getColor(), true); - notifyToSlideBars(); } return true; } @@ -299,17 +310,18 @@ public void setColorListener(ColorPickerViewListener colorListener) { * @param color color. * @param fromUser triggered by user or not. */ - public void fireColorListener(int color, boolean fromUser) { - if (colorListener != null) { - selectedColor = color; + public void fireColorListener(int color, final boolean fromUser) { + if (this.colorListener != null) { + this.selectedColor = color; if (getAlphaSlideBar() != null) { getAlphaSlideBar().notifyColor(); - selectedColor = getAlphaSlideBar().assembleColor(); + this.selectedColor = getAlphaSlideBar().assembleColor(); } if (getBrightnessSlider() != null) { getBrightnessSlider().notifyColor(); - selectedColor = getBrightnessSlider().assembleColor(); + this.selectedColor = getBrightnessSlider().assembleColor(); } + if (colorListener instanceof ColorListener) { ((ColorListener) colorListener).onColorSelected(selectedColor, fromUser); } else if (colorListener instanceof ColorEnvelopeListener) { @@ -317,15 +329,15 @@ public void fireColorListener(int color, boolean fromUser) { ((ColorEnvelopeListener) colorListener).onColorSelected(envelope, fromUser); } - if (flagView != null) flagView.onRefresh(getColorEnvelope()); + if (this.flagView != null) this.flagView.onRefresh(getColorEnvelope()); if (VISIBLE_FLAG) { VISIBLE_FLAG = false; - if (selector != null) { - selector.setAlpha(alpha_selector); + if (this.selector != null) { + this.selector.setAlpha(alpha_selector); } - if (flagView != null) { - flagView.setAlpha(alpha_flag); + if (this.flagView != null) { + this.flagView.setAlpha(alpha_flag); } } } @@ -502,7 +514,7 @@ public void moveSelectorPoint(int x, int y, int color) { selectedColor = getBrightnessSlider().assembleColor(); } - selectedPoint = new Point(x, y); + this.selectedPoint = new Point(x, y); setCoordinate(x, y); fireColorListener(getColor(), false); notifyToFlagView(selectedPoint); @@ -720,6 +732,7 @@ public void onDestroy() { public static class Builder { private Context context; private ColorPickerViewListener colorPickerViewListener; + private int debounceDuration = 100; private FlagView flagView; private Drawable paletteDrawable; private Drawable selectorDrawable; @@ -742,6 +755,11 @@ public Builder setColorListener(ColorPickerViewListener colorPickerViewListener) return this; } + public Builder setDebounceDuration(int debounceDuration) { + this.debounceDuration = debounceDuration; + return this; + } + public Builder setPaletteDrawable(@NonNull Drawable palette) { this.paletteDrawable = palette; return this; diff --git a/colorpickerview/src/main/res/values/attrs_colorpicker.xml b/colorpickerview/src/main/res/values/attrs_colorpicker.xml index b0d4a55..a6c7e54 100644 --- a/colorpickerview/src/main/res/values/attrs_colorpicker.xml +++ b/colorpickerview/src/main/res/values/attrs_colorpicker.xml @@ -6,6 +6,7 @@ + diff --git a/dependencies.gradle b/dependencies.gradle index bc6327c..382ec81 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -5,7 +5,7 @@ ext.versions = [ versionName : '2.1.5', gradleBuildTool : '3.5.0', - spotlessGradle : '3.24.2', + spotlessGradle : '3.26.0', bintrayRelease : '0.9.1', androidxAppcompat : '1.1.0', From ab6c4b4690c49c931b8775535b2af965defc56f8 Mon Sep 17 00:00:00 2001 From: skydoves Date: Sat, 14 Dec 2019 19:13:36 +0900 Subject: [PATCH 2/3] implement debounce & released version 2.1.6 --- README.md | 27 ++++-- app/src/main/res/layout/activity_main.xml | 3 +- .../colorpickerview/ColorPickerView.java | 97 +++++++++++++------ dependencies.gradle | 8 +- 4 files changed, 92 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index b20174a..6837bf9 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ allprojects { And add a dependency code to your **module**'s `build.gradle` file. ```gradle dependencies { - implementation "com.github.skydoves:colorpickerview:2.1.5" + implementation "com.github.skydoves:colorpickerview:2.1.6" } ``` @@ -36,8 +36,9 @@ dependencies { ## Table of Contents #### [1. ColorPickerView](https://github.com/skydoves/ColorPickerView#usage) - [ColorPickerView in layout](https://github.com/skydoves/ColorPickerView#colorpickerview-in-layout) -- [ColorListener](https://github.com/skydoves/ColorPickerView#colorlistener-listener) +- [ColorListener](https://github.com/skydoves/ColorPickerView#colorlistener) - [ActionMode](https://github.com/skydoves/ColorPickerView#actionmode) +- [Debounce](https://github.com/skydoves/ColorPickerView#debounce) - [Create using builder](https://github.com/skydoves/ColorPickerView#create-using-builder) - [Restore and save](https://github.com/skydoves/ColorPickerView#restore-and-save) - [Pallette from Gallery](https://github.com/skydoves/ColorPickerView#pallette-from-gallery)
@@ -60,11 +61,11 @@ xmlns:app="http://schemas.android.com/apk/res-auto" ### ColorPickerView in layout ```gradle + android:id="@+id/colorPickerView" + android:layout_width="300dp" + android:layout_height="300dp" + app:palette="@drawable/palette" + app:selector="@drawable/wheel" /> ``` ### Attribute descriptions @@ -115,6 +116,18 @@ ActionMode controls an action about `ColorListener` invoking. colorPickerView.setActionMode(ActionMode.LAST); // the listener will be invoked only when finger released. ``` +### Debounce +Only emit a color to the listener if a particular timespan has passed without it emitting using `debounceDuration` attribute. + +We can set the `debounceDuration` on our xml layout file. +```xml +app:debounceDuration="150" +``` +Or we can set set programmatically. +```java +colorPickerView.setDebounceDuration(150); +``` + ### Create using builder This is how to create `ColorPickerView`'s instance using `ColorPickerView.Builder` class. ```java diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 83fbc03..aa07ca9 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -50,8 +50,7 @@ android:layout_weight="6" app:palette="@drawable/palette" app:preferenceName="Test2" - app:selector="@drawable/wheel" - app:debounceDuration="150" /> + app:selector="@drawable/wheel" /> = this.lastDebouncedTime + this.debounceDuration) { - this.lastDebouncedTime = now; - if (actionMode == ActionMode.LAST) { - if (event.getAction() == MotionEvent.ACTION_UP) { - fireColorListener(getColor(), true); - notifyToSlideBars(); - } - } else { - fireColorListener(getColor(), true); - notifyToSlideBars(); - } - } + notifyToFlagView(this.selectedPoint); + + this.debounceHandler.removeCallbacksAndMessages(null); + Runnable debounceRunnable = + new Runnable() { + @Override + public void run() { + if (actionMode == ActionMode.LAST) { + if (event.getAction() == MotionEvent.ACTION_UP) { + fireColorListener(getColor(), true); + notifyToSlideBars(); + } + } else { + fireColorListener(getColor(), true); + notifyToSlideBars(); + } + } + }; + this.debounceHandler.postDelayed(debounceRunnable, this.debounceDuration); return true; } @@ -349,9 +361,9 @@ private void notifyToSlideBars() { if (brightnessSlider != null) { brightnessSlider.notifyColor(); - if (brightnessSlider.assembleColor() != Color.WHITE) + if (brightnessSlider.assembleColor() != Color.WHITE) { selectedColor = brightnessSlider.assembleColor(); - else if (alphaSlideBar != null) selectedColor = alphaSlideBar.assembleColor(); + } else if (alphaSlideBar != null) selectedColor = alphaSlideBar.assembleColor(); } } @@ -377,8 +389,9 @@ private void notifyToFlagView(Point point) { flagView.onRefresh(getColorEnvelope()); } if (posX < 0) flagView.setX(0); - if (posX + flagView.getMeasuredWidth() > getMeasuredWidth()) + if (posX + flagView.getMeasuredWidth() > getMeasuredWidth()) { flagView.setX(getMeasuredWidth() - flagView.getMeasuredWidth()); + } } } @@ -439,6 +452,30 @@ public void setFlagView(@NonNull FlagView flagView) { flagView.setAlpha(alpha_flag); } + /** + * gets a debounce duration. + * + *

only emit a color to the listener if a particular timespan has passed without it emitting + * another value. + * + * @return debounceDuration. + */ + public long getDebounceDuration() { + return this.debounceDuration; + } + + /** + * sets a debounce duration. + * + *

only emit a color to the listener if a particular timespan has passed without it emitting + * another value. + * + * @param debounceDuration intervals. + */ + public void setDebounceDuration(long debounceDuration) { + this.debounceDuration = debounceDuration; + } + /** * gets center coordinate of the selector. * diff --git a/dependencies.gradle b/dependencies.gradle index 382ec81..7d0cfcc 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -1,17 +1,17 @@ ext.versions = [ minSdk : 15, compileSdk : 29, - versionoCode : 19, - versionName : '2.1.5', + versionoCode : 20, + versionName : '2.1.6', - gradleBuildTool : '3.5.0', + gradleBuildTool : '3.5.1', spotlessGradle : '3.26.0', bintrayRelease : '0.9.1', androidxAppcompat : '1.1.0', // for demo - googleMaterial : '1.1.0-beta01', + googleMaterial : '1.2.0-alpha02', powermenu : '2.1.2', timber : '4.7.1' ] From 03eac7686c8344ade5ef2f4af025386edd32d82d Mon Sep 17 00:00:00 2001 From: skydoves Date: Sat, 14 Dec 2019 19:15:21 +0900 Subject: [PATCH 3/3] Fix README typo --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 6837bf9..94cbd26 100644 --- a/README.md +++ b/README.md @@ -118,12 +118,11 @@ colorPickerView.setActionMode(ActionMode.LAST); // the listener will be invoked ### Debounce Only emit a color to the listener if a particular timespan has passed without it emitting using `debounceDuration` attribute. - We can set the `debounceDuration` on our xml layout file. ```xml app:debounceDuration="150" ``` -Or we can set set programmatically. +Or we can set it programmatically. ```java colorPickerView.setDebounceDuration(150); ```