From 6f00505265a4dcf2baf0a8f73b4db7610b54756a Mon Sep 17 00:00:00 2001 From: JayGoo <1015121748@qq.com> Date: Wed, 18 Jul 2018 16:50:03 +0800 Subject: [PATCH] =?UTF-8?q?increase=20hit=20area=20of=20thumb=E3=80=81supp?= =?UTF-8?q?ort=20indicator=20radius?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/jaygoo/widget/RangeSeekBar.java | 59 +++++++++++++++++-- .../main/java/com/jaygoo/widget/SeekBar.java | 19 ++++++ RangeSeekBar/src/main/res/values/attrs.xml | 2 + app/src/main/res/layout/activity_main.xml | 4 +- 4 files changed, 79 insertions(+), 5 deletions(-) diff --git a/RangeSeekBar/src/main/java/com/jaygoo/widget/RangeSeekBar.java b/RangeSeekBar/src/main/java/com/jaygoo/widget/RangeSeekBar.java index e7ce38a..5f727c2 100755 --- a/RangeSeekBar/src/main/java/com/jaygoo/widget/RangeSeekBar.java +++ b/RangeSeekBar/src/main/java/com/jaygoo/widget/RangeSeekBar.java @@ -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(); @@ -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 @@ -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; @@ -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) { @@ -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); @@ -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(); @@ -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); @@ -891,4 +940,6 @@ public void setSeekBarMode(int seekBarMode) { public void setTypeface(Typeface typeFace){ paint.setTypeface(typeFace); } + + } diff --git a/RangeSeekBar/src/main/java/com/jaygoo/widget/SeekBar.java b/RangeSeekBar/src/main/java/com/jaygoo/widget/SeekBar.java index f550bd7..0698391 100644 --- a/RangeSeekBar/src/main/java/com/jaygoo/widget/SeekBar.java +++ b/RangeSeekBar/src/main/java/com/jaygoo/widget/SeekBar.java @@ -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; @@ -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 ******************// @@ -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(); } @@ -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; + + } /** @@ -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); } @@ -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; + } } diff --git a/RangeSeekBar/src/main/res/values/attrs.xml b/RangeSeekBar/src/main/res/values/attrs.xml index ba79092..376b0af 100644 --- a/RangeSeekBar/src/main/res/values/attrs.xml +++ b/RangeSeekBar/src/main/res/values/attrs.xml @@ -88,10 +88,12 @@ + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c282cd8..8b2f9eb 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -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" />