diff --git a/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/AlphaSeekBar.java b/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/AlphaSeekBar.java
index 2601bca..ddcc8df 100644
--- a/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/AlphaSeekBar.java
+++ b/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/AlphaSeekBar.java
@@ -165,48 +165,11 @@ protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
-
@Override
- public boolean onTouchEvent(MotionEvent event) {
- if (!isEnabled()) {
- return true;
- }
- float x = vertical ? event.getY() : event.getX();
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- if (barRect.contains(event.getX(), event.getY()) || thumbRect.contains(event.getX(), event.getY())) {
- movingBar = true;
- float value = calculateTouchPercent(x);
- setProgress((int) value);
- if (listener != null) {
- listener.onAlphaChangeListener(progress, getAlphaValue());
- }
- }
- break;
- case MotionEvent.ACTION_MOVE:
- getParent().requestDisallowInterceptTouchEvent(true);
- if (movingBar) {
- float value = calculateTouchPercent(x);
- setProgress((int) value);
- if (listener != null) {
- listener.onAlphaChangeListener(progress, getAlphaValue());
- }
- }
- invalidate();
- break;
- case MotionEvent.ACTION_UP:
- movingBar = false;
- break;
- default:
- }
- return true;
- }
-
- private float calculateTouchPercent(float x) {
- if (isVertical()) {
- return (x - thumbDragRect.top) / thumbDragRect.height() * maxProgress;
- } else {
- return (x - thumbDragRect.left) / thumbDragRect.width() * maxProgress;
+ protected void onBarTouch(int progress) {
+ setProgress(progress);
+ if (listener != null) {
+ listener.onAlphaChangeListener(progress, getAlphaValue());
}
}
diff --git a/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/BaseSeekBar.java b/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/BaseSeekBar.java
index 84c83ec..37b6c8c 100644
--- a/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/BaseSeekBar.java
+++ b/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/BaseSeekBar.java
@@ -5,6 +5,7 @@
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
+import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;
@@ -12,7 +13,7 @@
import com.rtugeek.android.colorseekbar.thumb.ThumbDrawer;
-public class BaseSeekBar extends View {
+public abstract class BaseSeekBar extends View {
/**
* Rectangle that specified thumb's bounds
*/
@@ -21,6 +22,12 @@ public class BaseSeekBar extends View {
* Rectangle that specified seekbar's bounds
*/
protected final RectF barRect = new RectF();
+ /**
+ * Rectangle that detect seekbar's touch event
+ * in horizontal, the rect's height calculate by Math.max(barHeight,thumbDrawer.getHeight)
+ * in vertical, the rect's width calculate by Math.max(barHeight,thumbDrawer.getWidth)
+ */
+ protected final RectF touchDetectRect = new RectF();
/**
* Rectangle that specified thumb's allowed drag bounds
*/
@@ -119,8 +126,11 @@ protected void init() {
float barBottom = getHeight() - mPaddingSize;
barRect.set(barLeft, barTop, barRight, barBottom);
-
thumbDragRect.set(barRect.centerX(), barTop + borderRadius, barRect.centerX() + 1, barBottom - borderRadius);
+
+ int dragWidth = Math.max(barHeight, thumbDrawer.getWidth());
+ float dragLeft = thumbDragRect.left - dragWidth / 2f;
+ touchDetectRect.set(dragLeft, barTop, dragLeft + dragWidth, barBottom);
} else {
//init size
int viewBottom = getHeight() - mPaddingSize;
@@ -134,6 +144,10 @@ protected void init() {
barRect.set(barLeft, barTop, barRight, barBottom);
thumbDragRect.set(barLeft + borderRadius, barRect.centerY(), barRect.right - borderRadius, barRect.centerY() + 1);
+
+ int dragHeight = Math.max(barHeight, thumbDrawer.getHeight());
+ float dragTop = barRect.centerY() - dragHeight / 2f;
+ touchDetectRect.set(barLeft, dragTop, barRight, dragTop + dragHeight);
}
}
@@ -275,6 +289,45 @@ protected int dp2px(float dpValue) {
return (int) (dpValue * scale + 0.5f);
}
+ private boolean isMovingColorBar = false;
+
+ protected abstract void onBarTouch(int progress);
+ private float calculateTouchProgress(float x) {
+ if (isVertical()) {
+ return (x - touchDetectRect.top) / touchDetectRect.height() * maxProgress;
+ } else {
+ return (x - touchDetectRect.left) / touchDetectRect.width() * maxProgress;
+ }
+ }
+
+ @Override
+ public boolean onTouchEvent(MotionEvent event) {
+ if (!isEnabled()) {
+ return true;
+ }
+ float x = vertical ? event.getY() : event.getX();
+ switch (event.getAction()) {
+ case MotionEvent.ACTION_DOWN:
+ if (touchDetectRect.contains(event.getX(), event.getY())) {
+ isMovingColorBar = true;
+ onBarTouch((int) calculateTouchProgress(x));
+ }
+ break;
+ case MotionEvent.ACTION_MOVE:
+ getParent().requestDisallowInterceptTouchEvent(true);
+ if (isMovingColorBar) {
+ onBarTouch((int) calculateTouchProgress(x));
+ }
+ invalidate();
+ break;
+ case MotionEvent.ACTION_UP:
+ performClick();
+ isMovingColorBar = false;
+ break;
+ default:
+ }
+ return true;
+ }
}
diff --git a/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/ColorSeekBar.java b/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/ColorSeekBar.java
index da50b4c..17a6f73 100644
--- a/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/ColorSeekBar.java
+++ b/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/ColorSeekBar.java
@@ -96,7 +96,7 @@ private void applyStyle(Context context, AttributeSet attrs, int defStyleAttr, i
}
if (thumbDrawer == null) {
- setThumbDrawer(new DefaultThumbDrawer(dp2px(16), Color.WHITE, Color.BLACK));
+ setThumbDrawer(new DefaultThumbDrawer(barHeight + dp2px(6), Color.WHITE, Color.BLACK));
}
// setBackgroundColor(backgroundColor);
@@ -179,10 +179,15 @@ protected void onDraw(Canvas canvas) {
canvas.drawRoundRect(barRect, borderRadius, borderRadius, barRectPaint);
//draw mCachedBitmapColor bitmap
mCachedBitmapCanvas.drawRect(mCachedBitmapRect, mBitmapRectPaint);
+
+ //draw touchDetectRect, debug only
+ //canvas.drawRect(touchDetectRect, borderPaint);
+
// drawBorder
if (borderSize > 0) {
canvas.drawRoundRect(barRect, borderRadius, borderRadius, borderPaint);
-// canvas.drawRect(thumbDragRect, borderPaint);
+ //draw thumbDragRect, debug only
+ //canvas.drawRect(thumbDragRect, borderPaint);
}
if (showThumb && thumbDrawer != null) {
@@ -210,42 +215,6 @@ protected void onDraw(Canvas canvas) {
}
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- if (!isEnabled()) {
- return true;
- }
- float x = vertical ? event.getY() : event.getX();
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- if (barRect.contains(event.getX(), event.getY()) || thumbRect.contains(event.getX(), event.getY())) {
- mMovingColorBar = true;
- float value = calculateTouchPercent(x);
- setProgress((int) value);
- if (mOnColorChangeLister != null) {
- mOnColorChangeLister.onColorChangeListener(progress, getColor());
- }
- }
- break;
- case MotionEvent.ACTION_MOVE:
- getParent().requestDisallowInterceptTouchEvent(true);
- if (mMovingColorBar) {
- float value = calculateTouchPercent(x);
- setProgress((int) value);
- if (mOnColorChangeLister != null) {
- mOnColorChangeLister.onColorChangeListener(progress, getColor());
- }
- }
- invalidate();
- break;
- case MotionEvent.ACTION_UP:
- mMovingColorBar = false;
- break;
- default:
- }
- return true;
- }
-
private float calculateTouchPercent(float x) {
if (isVertical()) {
return (x - thumbDragRect.top) / thumbDragRect.height() * maxProgress;
@@ -310,6 +279,14 @@ public int dp2px(float dpValue) {
return (int) (dpValue * scale + 0.5f);
}
+ @Override
+ protected void onBarTouch(int progress) {
+ setProgress(progress);
+ if (mOnColorChangeLister != null) {
+ mOnColorChangeLister.onColorChangeListener(progress, getColor());
+ }
+ }
+
/**
* Set colors by resource id. The resource's type must be ArrayRes
*
diff --git a/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/Logger.java b/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/Logger.java
index f896bd8..f0b2a50 100644
--- a/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/Logger.java
+++ b/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/Logger.java
@@ -9,7 +9,7 @@
*/
public class Logger {
- private static boolean debug = true;
+ private static boolean debug = false;
private static final String TAG = "ColorSeekBarLib";
public static void i(String s) {
diff --git a/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/OnAlphaChangeListener.java b/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/OnAlphaChangeListener.java
index 8f267ef..c9236ba 100644
--- a/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/OnAlphaChangeListener.java
+++ b/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/OnAlphaChangeListener.java
@@ -2,8 +2,8 @@
public interface OnAlphaChangeListener {
/**
- * @param alphaBarPosition between 0-maxValue
+ * @param progress between 0-maxValue
* @param alpha between 0-255
*/
- void onAlphaChangeListener( int alphaBarPosition, int alpha);
+ void onAlphaChangeListener( int progress, int alpha);
}
diff --git a/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/thumb/DefaultThumbDrawer.java b/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/thumb/DefaultThumbDrawer.java
index a903799..c6dcb82 100644
--- a/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/thumb/DefaultThumbDrawer.java
+++ b/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/thumb/DefaultThumbDrawer.java
@@ -10,25 +10,53 @@
import com.rtugeek.android.colorseekbar.ColorSeekBar;
public class DefaultThumbDrawer implements ThumbDrawer {
+
private final Paint thumbStrokePaint = new Paint();
private final Paint thumbSolidPaint = new Paint();
private int size;
private int borderColor = Color.BLACK;
- private int borderSize = 3;
- private int color;
+ private int solidColor = Color.BLACK;
+ private int borderSize;
private final Path outerCircle = new Path();
private final Path innerCircle = new Path();
public DefaultThumbDrawer(int size, int solidColor, int borderColor) {
this.size = size;
this.borderColor = borderColor;
- this.color = solidColor;
thumbStrokePaint.setAntiAlias(true);
- thumbStrokePaint.setStyle(Paint.Style.STROKE);
- thumbStrokePaint.setColor(Color.GRAY);
- thumbStrokePaint.setStrokeWidth(borderSize);
thumbSolidPaint.setAntiAlias(true);
+
+ thumbStrokePaint.setStyle(Paint.Style.STROKE);
+
+ setBorderColor(borderColor);
+ setSolidColor(solidColor);
+ setBorderSize(3);
+ }
+
+ public int getBorderColor() {
+ return borderColor;
+ }
+
+ public void setBorderColor(int borderColor) {
+ this.borderColor = borderColor;
+ thumbStrokePaint.setColor(borderColor);
+ }
+
+ public int getBorderSize() {
+ return borderSize;
+ }
+
+ public int getSolidColor() {
+ return solidColor;
+ }
+
+ public void setSolidColor(int solidColor) {
+ this.solidColor = solidColor;
thumbSolidPaint.setColor(solidColor);
+ }
+
+ public void setBorderSize(int borderSize) {
+ this.borderSize = borderSize;
thumbStrokePaint.setStrokeWidth(borderSize);
}
@@ -42,7 +70,6 @@ public void onDrawThumb(RectF thumbBounds, BaseSeekBar seekBar, Canvas canvas) {
// thumbSolidPaint.setColor(((ColorSeekBar) seekBar).getColor());
// }
float outerRadius = thumbBounds.height() / 2f;
- thumbStrokePaint.setColor(seekBar.getBorderColor());
outerCircle.addCircle(centerX, centerY, outerRadius, Path.Direction.CW);
innerCircle.addCircle(centerX, centerY, outerRadius - 10, Path.Direction.CW);
outerCircle.op(innerCircle, Path.Op.DIFFERENCE);
diff --git a/colorseekbar/src/main/res/values/color.xml b/colorseekbar/src/main/res/values/color.xml
index e123b88..19dd810 100644
--- a/colorseekbar/src/main/res/values/color.xml
+++ b/colorseekbar/src/main/res/values/color.xml
@@ -12,5 +12,4 @@
- #795548
- #607D8B
-
\ No newline at end of file