From 76c849f99739758cbfe533e3073c1d904c4cb48f Mon Sep 17 00:00:00 2001 From: Ryan Aquilina Date: Wed, 4 Oct 2017 13:32:15 +0200 Subject: [PATCH] Changed selected color when bar pointers are moved When event ACTION_UP is triggered the picker changes the color selected, however it did not do so when the user changes the color using the bars. The code was edited to notify the onColorSelectedListener to change the selected color once the ACTION_UP event is triggered on a bar. --- .../holocolorpicker/ColorPicker.java | 28 ++++++------ .../holocolorpicker/OpacityBar.java | 43 +++++++++++-------- .../larswerkman/holocolorpicker/SVBar.java | 33 ++++++++------ .../holocolorpicker/SaturationBar.java | 39 ++++++++++------- .../larswerkman/holocolorpicker/ValueBar.java | 35 +++++++++------ 5 files changed, 106 insertions(+), 72 deletions(-) diff --git a/libary/src/main/java/com/larswerkman/holocolorpicker/ColorPicker.java b/libary/src/main/java/com/larswerkman/holocolorpicker/ColorPicker.java index ee1fd3e..121ad9d 100644 --- a/libary/src/main/java/com/larswerkman/holocolorpicker/ColorPicker.java +++ b/libary/src/main/java/com/larswerkman/holocolorpicker/ColorPicker.java @@ -31,8 +31,6 @@ import android.view.MotionEvent; import android.view.View; -import com.larswerkman.holocolorpicker.R; - /** * Displays a holo-themed color picker. * @@ -52,7 +50,7 @@ public class ColorPicker extends View { private static final String STATE_SHOW_OLD_COLOR = "showColor"; /** - * Colors to construct the color wheel using {@link android.graphics.SweepGradient}. + * Colors to construct the color wheel using {@link SweepGradient}. */ private static final int[] COLORS = new int[] { 0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00, 0xFFFFFF00, 0xFFFF0000 }; @@ -118,8 +116,8 @@ public class ColorPicker extends View { /** * {@code true} if the user clicked on the pointer to start the move mode.
* {@code false} once the user stops touching the screen. - * - * @see #onTouchEvent(android.view.MotionEvent) + * + * @see #onTouchEvent(MotionEvent) */ private boolean mUserIsMovingPointer = false; @@ -132,7 +130,7 @@ public class ColorPicker extends View { * The ARGB value of the center with the old selected color. */ private int mCenterOldColor; - + /** * Whether to show the old color in the center or not. */ @@ -145,19 +143,19 @@ public class ColorPicker extends View { /** * Number of pixels the origin of this view is moved in X- and Y-direction. - * + * *

* We use the center of this (quadratic) View as origin of our internal * coordinate system. Android uses the upper left corner as origin for the * View-specific coordinate system. So this is the value we use to translate * from one coordinate system to the other. *

- * + * *

* Note: (Re)calculated in {@link #onMeasure(int, int)}. *

- * - * @see #onDraw(android.graphics.Canvas) + * + * @see #onDraw(Canvas) */ private float mTranslationOffset; @@ -235,7 +233,7 @@ public class ColorPicker extends View { /** * {@code onColorSelectedListener} instance of the onColorSelectedListener */ - private OnColorSelectedListener onColorSelectedListener; + protected OnColorSelectedListener onColorSelectedListener; public ColorPicker(Context context) { super(context); @@ -316,7 +314,7 @@ public OnColorSelectedListener getOnColorSelectedListener() { /** * Color of the latest entry of the onColorSelectedListener. */ - private int oldSelectedListenerColor; + protected int oldSelectedListenerColor; private void init(AttributeSet attrs, int defStyle) { final TypedArray a = getContext().obtainStyledAttributes(attrs, @@ -659,9 +657,9 @@ else if (Math.sqrt(x*x + y*y) <= mColorWheelRadius + mColorPointerHaloRadius mCenterHaloPaint.setAlpha(0x00); if (onColorSelectedListener != null && mCenterNewColor != oldSelectedListenerColor) { - onColorSelectedListener.onColorSelected(mCenterNewColor); - oldSelectedListenerColor = mCenterNewColor; - } + onColorSelectedListener.onColorSelected(mCenterNewColor); + oldSelectedListenerColor = mCenterNewColor; + } invalidate(); break; diff --git a/libary/src/main/java/com/larswerkman/holocolorpicker/OpacityBar.java b/libary/src/main/java/com/larswerkman/holocolorpicker/OpacityBar.java index fccac9a..3ca72d1 100644 --- a/libary/src/main/java/com/larswerkman/holocolorpicker/OpacityBar.java +++ b/libary/src/main/java/com/larswerkman/holocolorpicker/OpacityBar.java @@ -31,8 +31,6 @@ import android.view.MotionEvent; import android.view.View; -import com.larswerkman.holocolorpicker.R; - public class OpacityBar extends View { /* @@ -109,7 +107,7 @@ public class OpacityBar extends View { * {@code true} if the user clicked on the pointer to start the move mode.
* {@code false} once the user stops touching the screen. * - * @see #onTouchEvent(android.view.MotionEvent) + * @see #onTouchEvent(MotionEvent) */ private boolean mIsMovingPointer; @@ -139,7 +137,7 @@ public class OpacityBar extends View { * to the host activity/fragment */ private OnOpacityChangedListener onOpacityChangedListener; - + /** * Opacity of the latest entry of the onOpacityChangedListener. */ @@ -262,7 +260,7 @@ else if (lengthMode == MeasureSpec.AT_MOST) { @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); - + // Fill the rectangle instance based on orientation int x1, y1; if (mOrientation == ORIENTATION_HORIZONTAL) { @@ -297,14 +295,14 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) { 0x0081ff00, 0xff81ff00 }, null, Shader.TileMode.CLAMP); Color.colorToHSV(0xff81ff00, mHSVColor); } - + mBarPaint.setShader(shader); mPosToOpacFactor = 0xFF / ((float) mBarLength); mOpacToPosFactor = ((float) mBarLength) / 0xFF; - + float[] hsvColor = new float[3]; Color.colorToHSV(mColor, hsvColor); - + if (!isInEditMode()){ mBarPointerPosition = Math.round((mOpacToPosFactor * Color.alpha(mColor)) + mBarPointerHaloRadius); @@ -328,7 +326,7 @@ protected void onDraw(Canvas canvas) { cX = mBarPointerHaloRadius; cY = mBarPointerPosition; } - + // Draw the pointer halo. canvas.drawCircle(cX, cY, mBarPointerHaloRadius, mBarPointerHaloPaint); // Draw the pointer. @@ -395,8 +393,19 @@ public boolean onTouchEvent(MotionEvent event) { oldChangedListenerOpacity = getOpacity(); } break; - case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_UP: + mIsMovingPointer = false; + if(mPicker.onColorSelectedListener!= null){ + mPicker.onColorSelectedListener.onColorSelected(mColor); + mPicker.oldSelectedListenerColor = mColor; + } + break; + case MotionEvent.ACTION_CANCEL: mIsMovingPointer = false; + if(mPicker.onColorSelectedListener!= null){ + mPicker.onColorSelectedListener.onColorSelected(mColor); + mPicker.oldSelectedListenerColor = mColor; + } break; } return true; @@ -406,7 +415,7 @@ public boolean onTouchEvent(MotionEvent event) { * Set the bar color.
*
* Its discouraged to use this method. - * + * * @param color */ public void setColor(int color) { @@ -419,7 +428,7 @@ public void setColor(int color) { x1 = mBarThickness; y1 = (mBarLength + mBarPointerHaloRadius); } - + Color.colorToHSV(color, mHSVColor); shader = new LinearGradient(mBarPointerHaloRadius, 0, x1, y1, new int[] { @@ -436,7 +445,7 @@ public void setColor(int color) { /** * Set the pointer on the bar. With the opacity value. - * + * * @param opacity float between 0 and 255 */ public void setOpacity(int opacity) { @@ -452,7 +461,7 @@ public void setOpacity(int opacity) { /** * Get the currently selected opacity. - * + * * @return The int value of the currently selected opacity. */ public int getOpacity() { @@ -469,7 +478,7 @@ public int getOpacity() { /** * Calculate the color selected by the pointer on the bar. - * + * * @param coord Coordinate of the pointer. */ private void calculateColor(int coord) { @@ -492,7 +501,7 @@ private void calculateColor(int coord) { /** * Get the currently selected color. - * + * * @return The ARGB value of the currently selected color. */ public int getColor() { @@ -504,7 +513,7 @@ public int getColor() { *
* WARNING: Don't change the color picker. it is done already when the bar * is added to the ColorPicker - * + * * @see com.larswerkman.holocolorpicker.ColorPicker#addSVBar(SVBar) * @param picker */ diff --git a/libary/src/main/java/com/larswerkman/holocolorpicker/SVBar.java b/libary/src/main/java/com/larswerkman/holocolorpicker/SVBar.java index 352159f..9e562cd 100644 --- a/libary/src/main/java/com/larswerkman/holocolorpicker/SVBar.java +++ b/libary/src/main/java/com/larswerkman/holocolorpicker/SVBar.java @@ -31,8 +31,6 @@ import android.view.MotionEvent; import android.view.View; -import com.larswerkman.holocolorpicker.R; - public class SVBar extends View { /* @@ -110,7 +108,7 @@ public class SVBar extends View { * {@code true} if the user clicked on the pointer to start the move mode.
* {@code false} once the user stops touching the screen. * - * @see #onTouchEvent(android.view.MotionEvent) + * @see #onTouchEvent(MotionEvent) */ private boolean mIsMovingPointer; @@ -275,7 +273,7 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) { Shader.TileMode.CLAMP); Color.colorToHSV(0xff81ff00, mHSVColor); } - + mBarPaint.setShader(shader); mPosToSVFactor = 1 / ((float) mBarLength / 2); mSVToPosFactor = ((float) mBarLength / 2) / 1; @@ -309,7 +307,7 @@ protected void onDraw(Canvas canvas) { cX = mBarPointerHaloRadius; cY = mBarPointerPosition; } - + // Draw the pointer halo. canvas.drawCircle(cX, cY, mBarPointerHaloRadius, mBarPointerHaloPaint); // Draw the pointer. @@ -377,6 +375,17 @@ public boolean onTouchEvent(MotionEvent event) { break; case MotionEvent.ACTION_UP: mIsMovingPointer = false; + if(mPicker.onColorSelectedListener!= null){ + mPicker.onColorSelectedListener.onColorSelected(mColor); + mPicker.oldSelectedListenerColor = mColor; + } + break; + case MotionEvent.ACTION_CANCEL: + mIsMovingPointer = false; + if(mPicker.onColorSelectedListener!= null){ + mPicker.onColorSelectedListener.onColorSelected(mColor); + mPicker.oldSelectedListenerColor = mColor; + } break; } return true; @@ -384,7 +393,7 @@ public boolean onTouchEvent(MotionEvent event) { /** * Set the pointer on the bar. With the saturation value. - * + * * @param saturation float between 0 and 1 */ public void setSaturation(float saturation) { @@ -403,7 +412,7 @@ public void setSaturation(float saturation) { /** * Set the pointer on the bar. With the Value value. - * + * * @param value float between 0 and 1 */ public void setValue(float value) { @@ -424,7 +433,7 @@ public void setValue(float value) { * Set the bar color.
*
* Its discouraged to use this method. - * + * * @param color */ public void setColor(int color) { @@ -436,7 +445,7 @@ public void setColor(int color) { x1 = mBarThickness; y1 = (mBarLength + mBarPointerHaloRadius); } - + Color.colorToHSV(color, mHSVColor); shader = new LinearGradient(mBarPointerHaloRadius, 0, x1, y1, new int[] {Color.WHITE, color, Color.BLACK}, null, @@ -454,7 +463,7 @@ public void setColor(int color) { /** * Calculate the color selected by the pointer on the bar. - * + * * @param coord Coordinate of the pointer. */ private void calculateColor(int coord) { @@ -481,7 +490,7 @@ private void calculateColor(int coord) { /** * Get the currently selected color. - * + * * @return The ARGB value of the currently selected color. */ public int getColor() { @@ -493,7 +502,7 @@ public int getColor() { *
* WARNING: Don't change the color picker. it is done already when the bar * is added to the ColorPicker - * + * * @see com.larswerkman.holocolorpicker.ColorPicker#addSVBar(SVBar) * @param picker */ diff --git a/libary/src/main/java/com/larswerkman/holocolorpicker/SaturationBar.java b/libary/src/main/java/com/larswerkman/holocolorpicker/SaturationBar.java index fd736ea..4e10888 100644 --- a/libary/src/main/java/com/larswerkman/holocolorpicker/SaturationBar.java +++ b/libary/src/main/java/com/larswerkman/holocolorpicker/SaturationBar.java @@ -31,8 +31,6 @@ import android.view.MotionEvent; import android.view.View; -import com.larswerkman.holocolorpicker.R; - public class SaturationBar extends View { /* @@ -109,7 +107,7 @@ public class SaturationBar extends View { * {@code true} if the user clicked on the pointer to start the move mode.
* {@code false} once the user stops touching the screen. * - * @see #onTouchEvent(android.view.MotionEvent) + * @see #onTouchEvent(MotionEvent) */ private boolean mIsMovingPointer; @@ -143,13 +141,13 @@ public class SaturationBar extends View { * Used to toggle orientation between vertical and horizontal. */ private boolean mOrientation; - + /** * Interface and listener so that changes in SaturationBar are sent * to the host activity/fragment */ private OnSaturationChangedListener onSaturationChangedListener; - + /** * Saturation of the latest entry of the onSaturationChangedListener. */ @@ -297,14 +295,14 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) { Color.WHITE, 0xff81ff00 }, null, Shader.TileMode.CLAMP); Color.colorToHSV(0xff81ff00, mHSVColor); } - + mBarPaint.setShader(shader); mPosToSatFactor = 1 / ((float) mBarLength); mSatToPosFactor = ((float) mBarLength) / 1; - + float[] hsvColor = new float[3]; Color.colorToHSV(mColor, hsvColor); - + if (!isInEditMode()){ mBarPointerPosition = Math.round((mSatToPosFactor * hsvColor[1]) + mBarPointerHaloRadius); @@ -328,7 +326,7 @@ protected void onDraw(Canvas canvas) { cX = mBarPointerHaloRadius; cY = mBarPointerPosition; } - + // Draw the pointer halo. canvas.drawCircle(cX, cY, mBarPointerHaloRadius, mBarPointerHaloPaint); // Draw the pointer. @@ -403,6 +401,17 @@ public boolean onTouchEvent(MotionEvent event) { break; case MotionEvent.ACTION_UP: mIsMovingPointer = false; + if(mPicker.onColorSelectedListener!= null){ + mPicker.onColorSelectedListener.onColorSelected(mColor); + mPicker.oldSelectedListenerColor = mColor; + } + break; + case MotionEvent.ACTION_CANCEL: + mIsMovingPointer = false; + if(mPicker.onColorSelectedListener!= null){ + mPicker.onColorSelectedListener.onColorSelected(mColor); + mPicker.oldSelectedListenerColor = mColor; + } break; } return true; @@ -412,7 +421,7 @@ public boolean onTouchEvent(MotionEvent event) { * Set the bar color.
*
* Its discouraged to use this method. - * + * * @param color */ public void setColor(int color) { @@ -425,7 +434,7 @@ public void setColor(int color) { x1 = mBarThickness; y1 = (mBarLength + mBarPointerHaloRadius); } - + Color.colorToHSV(color, mHSVColor); shader = new LinearGradient(mBarPointerHaloRadius, 0, x1, y1, new int[] { @@ -446,7 +455,7 @@ else if(mPicker.hasOpacityBar()) /** * Set the pointer on the bar. With the opacity value. - * + * * @param saturation float between 0 and 1 */ public void setSaturation(float saturation) { @@ -464,7 +473,7 @@ public void setSaturation(float saturation) { /** * Calculate the color selected by the pointer on the bar. - * + * * @param coord Coordinate of the pointer. */ private void calculateColor(int coord) { @@ -480,7 +489,7 @@ private void calculateColor(int coord) { /** * Get the currently selected color. - * + * * @return The ARGB value of the currently selected color. */ public int getColor() { @@ -492,7 +501,7 @@ public int getColor() { *
* WARNING: Don't change the color picker. it is done already when the bar * is added to the ColorPicker - * + * * @see com.larswerkman.holocolorpicker.ColorPicker#addSVBar(SVBar) * @param picker */ diff --git a/libary/src/main/java/com/larswerkman/holocolorpicker/ValueBar.java b/libary/src/main/java/com/larswerkman/holocolorpicker/ValueBar.java index 93e337e..922e3b6 100644 --- a/libary/src/main/java/com/larswerkman/holocolorpicker/ValueBar.java +++ b/libary/src/main/java/com/larswerkman/holocolorpicker/ValueBar.java @@ -31,8 +31,6 @@ import android.view.MotionEvent; import android.view.View; -import com.larswerkman.holocolorpicker.R; - public class ValueBar extends View { /* @@ -109,7 +107,7 @@ public class ValueBar extends View { * {@code true} if the user clicked on the pointer to start the move mode.
* {@code false} once the user stops touching the screen. * - * @see #onTouchEvent(android.view.MotionEvent) + * @see #onTouchEvent(MotionEvent) */ private boolean mIsMovingPointer; @@ -143,13 +141,13 @@ public class ValueBar extends View { * Used to toggle orientation between vertical and horizontal. */ private boolean mOrientation; - + /** * Interface and listener so that changes in ValueBar are sent * to the host activity/fragment */ private OnValueChangedListener onValueChangedListener; - + /** * Value of the latest entry of the onValueChangedListener. */ @@ -329,7 +327,7 @@ protected void onDraw(Canvas canvas) { cX = mBarPointerHaloRadius; cY = mBarPointerPosition; } - + // Draw the pointer halo. canvas.drawCircle(cX, cY, mBarPointerHaloRadius, mBarPointerHaloPaint); // Draw the pointer. @@ -401,6 +399,17 @@ public boolean onTouchEvent(MotionEvent event) { break; case MotionEvent.ACTION_UP: mIsMovingPointer = false; + if(mPicker.onColorSelectedListener!= null){ + mPicker.onColorSelectedListener.onColorSelected(mColor); + mPicker.oldSelectedListenerColor = mColor; + } + break; + case MotionEvent.ACTION_CANCEL: + mIsMovingPointer = false; + if(mPicker.onColorSelectedListener!= null){ + mPicker.onColorSelectedListener.onColorSelected(mColor); + mPicker.oldSelectedListenerColor = mColor; + } break; } return true; @@ -410,7 +419,7 @@ public boolean onTouchEvent(MotionEvent event) { * Set the bar color.
*
* Its discouraged to use this method. - * + * * @param color */ public void setColor(int color) { @@ -423,7 +432,7 @@ public void setColor(int color) { x1 = mBarThickness; y1 = (mBarLength + mBarPointerHaloRadius); } - + Color.colorToHSV(color, mHSVColor); shader = new LinearGradient(mBarPointerHaloRadius, 0, x1, y1, new int[] { @@ -441,7 +450,7 @@ public void setColor(int color) { /** * Set the pointer on the bar. With the opacity value. - * + * * @param value float between 0 and 1 */ public void setValue(float value) { @@ -456,10 +465,10 @@ public void setValue(float value) { } invalidate(); } - + /** * Calculate the color selected by the pointer on the bar. - * + * * @param coord Coordinate of the pointer. */ private void calculateColor(int coord) { @@ -476,7 +485,7 @@ private void calculateColor(int coord) { /** * Get the currently selected color. - * + * * @return The ARGB value of the currently selected color. */ public int getColor() { @@ -488,7 +497,7 @@ public int getColor() { *
* WARNING: Don't change the color picker. it is done already when the bar * is added to the ColorPicker - * + * * @see com.larswerkman.holocolorpicker.ColorPicker#addSVBar(com.larswerkman.holocolorpicker.SVBar) * @param picker */