Skip to content

Commit

Permalink
increase hit area of thumb、support indicator radius
Browse files Browse the repository at this point in the history
  • Loading branch information
JinJieGu committed Jul 18, 2018
1 parent 39d943a commit 6f00505
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
59 changes: 55 additions & 4 deletions RangeSeekBar/src/main/java/com/jaygoo/widget/RangeSeekBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public class RangeSeekBar extends View {
// private float offsetValue;
// private float maxPositiveValue, minPositiveValue;
private boolean isEnable = true;
private boolean isScaleThumb = false;
private Paint paint = new Paint();
private RectF backgroundLineRect = new RectF();
private RectF foregroundLineRect = new RectF();
Expand Down Expand Up @@ -122,13 +123,17 @@ public RangeSeekBar(Context context, AttributeSet attrs) {

setRange(minProgress, maxProgress, rangeInterval, tickMarkNumber);

initProgressLine();
}

private void initProgressLine() {
//Android 7.0以后,优化了View的绘制,onMeasure和onSizeChanged调用顺序有所变化
//Android7.0以下:onMeasure--->onSizeChanged--->onMeasure
//Android7.0以上:onMeasure--->onSizeChanged
if (rightSB == null){
lineTop = leftSB.getIndicatorHeight() + leftSB.getIndicatorArrowSize() + leftSB.getThumbSize() / 2 - progressHeight / 2;
lineTop = (int) (leftSB.getIndicatorHeight() + leftSB.getIndicatorArrowSize() + leftSB.getThumbSize() * leftSB.getThumbScaleRatio() / 2 - progressHeight / 2);
}else {
lineTop = Math.max(leftSB.getIndicatorHeight() + leftSB.getIndicatorArrowSize() + leftSB.getThumbSize() / 2, rightSB.getIndicatorHeight() + rightSB.getIndicatorArrowSize() + rightSB.getThumbSize() / 2) - progressHeight / 2;
lineTop = (int) (Math.max(leftSB.getIndicatorHeight() + leftSB.getIndicatorArrowSize() + leftSB.getThumbSize() * leftSB.getThumbScaleRatio() / 2, rightSB.getIndicatorHeight() + rightSB.getIndicatorArrowSize() + rightSB.getThumbSize() / 2) - progressHeight / 2);
}
lineBottom = lineTop + progressHeight;
//default value
Expand Down Expand Up @@ -508,6 +513,29 @@ protected float getEventY(MotionEvent event){
return event.getY();
}

/**
* scale the touch seekBar thumb
*/
private void scaleCurrentSeekBarThumb(){
if (currTouchSB != null && currTouchSB.getThumbScaleRatio() > 1f && !isScaleThumb) {
isScaleThumb = true;
currTouchSB.setThumbSize((int) (currTouchSB.getThumbSize() * currTouchSB.getThumbScaleRatio()));
currTouchSB.onSizeChanged(getLineLeft(), getLineBottom(), lineWidth);
}
}

/**
* reset the touch seekBar thumb
*/
private void resetCurrentSeekBarThumb(){
if (currTouchSB != null && currTouchSB.getThumbScaleRatio() > 1f && isScaleThumb) {
isScaleThumb = false;
currTouchSB.setThumbSize((int) (currTouchSB.getThumbSize() / currTouchSB.getThumbScaleRatio()));
currTouchSB.onSizeChanged(getLineLeft(), getLineBottom(), lineWidth);

}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnable) return true;
Expand All @@ -518,12 +546,15 @@ public boolean onTouchEvent(MotionEvent event) {
if (rightSB != null && rightSB.currPercent >= 1 && leftSB.collide(getEventX(event), getEventY(event))) {
currTouchSB = leftSB;
touchResult = true;
scaleCurrentSeekBarThumb();
} else if (rightSB != null && rightSB.collide(getEventX(event), getEventY(event))) {
currTouchSB = rightSB;
touchResult = true;
scaleCurrentSeekBarThumb();
} else if (leftSB.collide(getEventX(event), getEventY(event))) {
currTouchSB = leftSB;
touchResult = true;
scaleCurrentSeekBarThumb();
}
//Intercept parent TouchEvent
if (getParent() != null) {
Expand All @@ -544,10 +575,22 @@ public boolean onTouchEvent(MotionEvent event) {
}
if (x - touchDownX > 0) {
//method to move right
currTouchSB = rightSB;
if (currTouchSB != rightSB) {
resetCurrentSeekBarThumb();
currTouchSB = rightSB;
scaleCurrentSeekBarThumb();
}else {
currTouchSB = rightSB;
}
} else {
//method to move left
currTouchSB = leftSB;
if (currTouchSB != leftSB) {
resetCurrentSeekBarThumb();
currTouchSB = leftSB;
scaleCurrentSeekBarThumb();
}else {
currTouchSB = leftSB;
}
}
if (callback != null) {
callback.onStartTrackingTouch(this, currTouchSB == leftSB);
Expand Down Expand Up @@ -641,6 +684,11 @@ public boolean onTouchEvent(MotionEvent event) {
if (rightSB != null) {
rightSB.setShowIndicatorEnable(false);
}
if (currTouchSB == leftSB){
resetCurrentSeekBarThumb();
}else if (currTouchSB == rightSB){
resetCurrentSeekBarThumb();
}
leftSB.setShowIndicatorEnable(false);
if (callback != null) {
SeekBarState[] states = getRangeSeekBarState();
Expand All @@ -658,6 +706,7 @@ public boolean onTouchEvent(MotionEvent event) {
}
leftSB.setShowIndicatorEnable(false);
currTouchSB.materialRestore();
resetCurrentSeekBarThumb();
if (callback != null) {
SeekBarState[] states = getRangeSeekBarState();
callback.onRangeChanged(this, states[0].value, states[1].value, false);
Expand Down Expand Up @@ -891,4 +940,6 @@ public void setSeekBarMode(int seekBarMode) {
public void setTypeface(Typeface typeFace){
paint.setTypeface(typeFace);
}


}
19 changes: 19 additions & 0 deletions RangeSeekBar/src/main/java/com/jaygoo/widget/SeekBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
Expand Down Expand Up @@ -51,11 +52,14 @@ public class SeekBar {
private int indicatorArrowSize;
private int indicatorTextSize;
private int indicatorTextColor;
private float indicatorRadius;
private int indicatorBackgroundColor;
private int indicatorPaddingLeft, indicatorPaddingRight, indicatorPaddingTop, indicatorPaddingBottom;
private int thumbDrawableId;
private int thumbInactivatedDrawableId;
private int thumbSize;
//when you touch or move, the thumb will scale, default not scale
private float thumbScaleRatio;

//****************** the above is attr value ******************//

Expand Down Expand Up @@ -107,6 +111,8 @@ private void initAttrs(AttributeSet attrs){
thumbDrawableId = t.getResourceId(R.styleable.RangeSeekBar_rsb_thumb_drawable, R.drawable.rsb_default_thumb);
thumbInactivatedDrawableId = t.getResourceId(R.styleable.RangeSeekBar_rsb_thumb_inactivated_drawable, 0);
thumbSize = (int) t.getDimension(R.styleable.RangeSeekBar_rsb_thumb_size, Utils.dp2px(getContext(),26));
thumbScaleRatio = t.getFloat(R.styleable.RangeSeekBar_rsb_thumb_scale_ratio, 1f);
indicatorRadius = t.getDimension(R.styleable.RangeSeekBar_rsb_indicator_radius, 0f);
t.recycle();
}

Expand Down Expand Up @@ -146,11 +152,15 @@ private void initBitmap() {
* @param parentLineWidth the RangSeerBar progress line width
*/
protected void onSizeChanged(int x, int y, int parentLineWidth) {
initVariables();
initBitmap();
left = x - thumbSize / 2;
right = x + thumbSize / 2;
top = y - thumbSize / 2;
bottom = y + thumbSize / 2;
lineWidth = parentLineWidth;


}

/**
Expand Down Expand Up @@ -211,6 +221,8 @@ private void drawIndicator(Canvas canvas, String text2Draw){
//draw indicator background
if (indicatorBitmap != null){
Utils.drawNinePath(canvas, indicatorBitmap, indicatorRect);
}else if (indicatorRadius > 0f){
canvas.drawRoundRect(new RectF(indicatorRect), indicatorRadius, indicatorRadius, paint);
}else {
canvas.drawRect(indicatorRect, paint);
}
Expand Down Expand Up @@ -519,4 +531,11 @@ public void setTypeface(Typeface typeFace){
}


/**
* when you touch or move, the thumb will scale, default not scale
* @return default 1.0f
*/
public float getThumbScaleRatio() {
return thumbScaleRatio;
}
}
2 changes: 2 additions & 0 deletions RangeSeekBar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@
<attr name="rsb_indicator_padding_right" format="dimension" />
<attr name="rsb_indicator_padding_top" format="dimension" />
<attr name="rsb_indicator_padding_bottom" format="dimension" />
<attr name="rsb_indicator_radius" format="dimension" />
<attr name="rsb_thumb_drawable" format="reference"/>
<!--the thumb inactivated is when you don't touch the thumb button-->
<attr name="rsb_thumb_inactivated_drawable" format="reference"/>
<attr name="rsb_thumb_size" format="dimension"/>
<attr name="rsb_thumb_scale_ratio" format="float"/>
</declare-styleable>

</resources>
4 changes: 3 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
app:rsb_indicator_height="20dp"
app:rsb_indicator_width="80dp"
app:rsb_indicator_show_mode="alwaysShow"
app:rsb_indicator_radius="8dp"
/>

<com.jaygoo.widget.RangeSeekBar
Expand All @@ -58,7 +59,7 @@
app:rsb_indicator_show_mode="showWhenTouch"
app:rsb_thumb_drawable="@drawable/thumb_activated"
app:rsb_thumb_inactivated_drawable="@drawable/thumb_inactivated"

app:rsb_thumb_scale_ratio="1.2"
/>
<com.jaygoo.widget.RangeSeekBar
android:id="@+id/seekbar3"
Expand All @@ -83,6 +84,7 @@
app:rsb_indicator_show_mode="showWhenTouch"
app:rsb_thumb_size="20dp"
app:rsb_indicator_margin="3dp"
app:rsb_thumb_scale_ratio="1.5"
app:rsb_thumb_drawable="@drawable/thumb_gradient"
/>
<com.jaygoo.widget.RangeSeekBar
Expand Down

0 comments on commit 6f00505

Please sign in to comment.