Skip to content

Commit

Permalink
Increased seek bar touch event sensitivity
Browse files Browse the repository at this point in the history
  • Loading branch information
rtugeek committed Oct 9, 2021
1 parent af97c60 commit 758426d
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 92 deletions.
Expand Up @@ -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());
}
}

Expand Down
Expand Up @@ -5,14 +5,15 @@
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.rtugeek.android.colorseekbar.thumb.ThumbDrawer;

public class BaseSeekBar extends View {
public abstract class BaseSeekBar extends View {
/**
* Rectangle that specified thumb's bounds
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
}

Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down
Expand Up @@ -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) {
Expand Down
Expand Up @@ -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);
}
Expand Up @@ -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);
}

Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion colorseekbar/src/main/res/values/color.xml
Expand Up @@ -12,5 +12,4 @@
<item>#795548</item>
<item>#607D8B</item>
</array>

</resources>

0 comments on commit 758426d

Please sign in to comment.