Skip to content

Commit

Permalink
new feature warkiz#26 :only touch thumb to seek. fixed warkiz#27
Browse files Browse the repository at this point in the history
  • Loading branch information
warkiz committed Feb 4, 2018
1 parent 0154471 commit dbe6435
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class BuilderParams {
float mProgress = 0;
boolean mClearPadding = false;
boolean mIsFloatProgress = false;
boolean mForbidUserSeek = false;
boolean mTouchToSeek = true;
//indicator
int mIndicatorType = 0;
boolean mShowIndicator = true;
Expand Down Expand Up @@ -54,13 +56,12 @@ class BuilderParams {
String mLeftEndText = null;
String mRightEndText = null;
CharSequence[] mTextArray = null;
Typeface mTextTypeface = Typeface.DEFAULT;
//thumb
int mThumbColor = Color.parseColor("#FF4081");
int mThumbSize;
Drawable mThumbDrawable = null;
boolean mThumbProgressStay = false;
boolean mForbidUserSeek = false;
Typeface mTextTypeface = Typeface.DEFAULT;

BuilderParams(Context context) {
this.mContext = context;
Expand All @@ -81,6 +82,8 @@ BuilderParams copy(BuilderParams p) {
this.mProgress = p.mProgress;
this.mClearPadding = p.mClearPadding;
this.mIsFloatProgress = p.mIsFloatProgress;
this.mForbidUserSeek = p.mForbidUserSeek;
this.mTouchToSeek = p.mTouchToSeek;
//indicator
this.mIndicatorType = p.mIndicatorType;
this.mShowIndicator = p.mShowIndicator;
Expand Down Expand Up @@ -116,7 +119,6 @@ BuilderParams copy(BuilderParams p) {
this.mThumbSize = p.mThumbSize;
this.mThumbDrawable = p.mThumbDrawable;
this.mThumbProgressStay = p.mThumbProgressStay;
this.mForbidUserSeek = p.mForbidUserSeek;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private void setMargin(int left, int top, int right, int bottom) {
* call this to update indicator's location. if SeekBar is covered ,the indicator will dismiss auto and would show after the SeekBar showing completed.
*/
public void update() {
if (!mSeekBar.isEnabled()){
if (!mSeekBar.isEnabled() || !(mSeekBar.getVisibility() == View.VISIBLE)) {
return;
}
if (mSeekBar.isCover()) {
Expand All @@ -208,7 +208,7 @@ public void update() {
* @param touchX the x location you touch without padding left.
*/
void update(float touchX) {
if (!mSeekBar.isEnabled()){
if (!mSeekBar.isEnabled() || !(mSeekBar.getVisibility() == View.VISIBLE)) {
return;
}
if (mIndicatorView instanceof CircleBubbleView) {
Expand All @@ -225,7 +225,7 @@ void update(float touchX) {
* call this to show indicator
*/
public void show() {
if (!mSeekBar.isEnabled()){
if (!mSeekBar.isEnabled() || !(mSeekBar.getVisibility() == View.VISIBLE)) {
return;
}
if (!this.isShowing() && !mSeekBar.isCover()) {
Expand All @@ -240,7 +240,7 @@ public void show() {
*/

void show(float touchX) {
if (mIndicator.isShowing()||!mSeekBar.isEnabled()) {
if (mIndicator.isShowing() || !mSeekBar.isEnabled() || !(mSeekBar.getVisibility() == View.VISIBLE)) {
return;
}
if (mIndicatorView instanceof CircleBubbleView) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

import java.math.BigDecimal;
Expand Down Expand Up @@ -119,6 +120,7 @@ private void initAttrs(Context context, AttributeSet attrs) {
p.mClearPadding = ta.getBoolean(R.styleable.IndicatorSeekBar_isb_clear_default_padding, p.mClearPadding);
p.mForbidUserSeek = ta.getBoolean(R.styleable.IndicatorSeekBar_isb_forbid_user_seek, p.mForbidUserSeek);
p.mIsFloatProgress = ta.getBoolean(R.styleable.IndicatorSeekBar_isb_progress_value_float, p.mIsFloatProgress);
p.mTouchToSeek = ta.getBoolean(R.styleable.IndicatorSeekBar_isb_touch_to_seek, p.mTouchToSeek);
//track
p.mBackgroundTrackSize = ta.getDimensionPixelSize(R.styleable.IndicatorSeekBar_isb_track_background_bar_size, p.mBackgroundTrackSize);
p.mProgressTrackSize = ta.getDimensionPixelSize(R.styleable.IndicatorSeekBar_isb_track_progress_bar_size, p.mProgressTrackSize);
Expand Down Expand Up @@ -374,7 +376,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
}

@Override
protected void onDraw(Canvas canvas) {
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
//draw 2th track
mStockPaint.setColor(p.mProgressTrackColor);
Expand Down Expand Up @@ -702,12 +704,16 @@ public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
performClick();
if (isTouchSeekBar(event) && !p.mForbidUserSeek && isEnabled()) {
if (mListener != null) {
mListener.onStartTrackingTouch(this, getThumbPosOnTick());
float mX = event.getX();
float mY = event.getY();
if (isTouchSeekBar(mX, mY) && !p.mForbidUserSeek && isEnabled()) {
if (p.mTouchToSeek || isTouchThumb(mX)) {
if (mListener != null) {
mListener.onStartTrackingTouch(this, getThumbPosOnTick());
}
refreshSeekBar(event, true);
return true;
}
refreshSeekBar(event, true);
return true;
}
break;
case MotionEvent.ACTION_MOVE:
Expand Down Expand Up @@ -907,9 +913,7 @@ private void calculateTouchX(float touchX) {
mTouchX = mSeekBlockLength * touchBlockSize + mPaddingLeft;
}

private boolean isTouchSeekBar(MotionEvent event) {
float mX = event.getX();
float mY = event.getY();
private boolean isTouchSeekBar(float mX, float mY) {
if (mFaultTolerance == -1) {
mFaultTolerance = IndicatorUtils.dp2px(mContext, 5);
}
Expand Down Expand Up @@ -969,7 +973,7 @@ public int getProgress() {
*
* @param max the max value , if is less than min, will set to min.
*/
public void setMax(float max) {
public synchronized void setMax(float max) {
if (max < mRawParams.mMin) {
max = mRawParams.mMin;
}
Expand All @@ -988,7 +992,7 @@ public void setMax(float max) {
*
* @param min the min value , if is larger than max, will set to max.
*/
public void setMin(float min) {
public synchronized void setMin(float min) {
if (min > mRawParams.mMax) {
min = mRawParams.mMax;
}
Expand All @@ -1007,7 +1011,7 @@ public void setMin(float min) {
*
* @param progress a new progress value , if the new progress is less than min , it will set to min ,if over max ,will be max.
*/
public void setProgress(float progress) {
public synchronized void setProgress(float progress) {
if (progress < p.mMin) {
p.mProgress = p.mMin;
} else if (progress > p.mMax) {
Expand Down Expand Up @@ -1039,7 +1043,7 @@ public void forbidUserToSeek(boolean forbidding) {
*
* @return
*/
public Builder getBuilder() {
public synchronized Builder getBuilder() {
if (mBuilder == null) {
mBuilder = new Builder(mContext);
}
Expand All @@ -1051,7 +1055,7 @@ public Builder getBuilder() {
*
* @return current progress in float type.
*/
public float getProgressFloat() {
public synchronized float getProgressFloat() {
return getProgressFloat(1);
}

Expand Down Expand Up @@ -1085,7 +1089,7 @@ public Indicator getIndicator() {
*
* @param customIndicatorView the view is the indicator you touch to show;
*/
public void setCustomIndicator(@NonNull View customIndicatorView) {
public synchronized void setCustomIndicator(@NonNull View customIndicatorView) {
mIndicator.setCustomIndicator(customIndicatorView);
}

Expand All @@ -1094,7 +1098,7 @@ public void setCustomIndicator(@NonNull View customIndicatorView) {
*
* @param customIndicatorViewId the layout ID for indicator you touch to show;
*/
public void setCustomIndicator(@LayoutRes int customIndicatorViewId) {
public synchronized void setCustomIndicator(@LayoutRes int customIndicatorViewId) {
mIndicator.setCustomIndicator(View.inflate(mContext, customIndicatorViewId, null));
}

Expand All @@ -1104,7 +1108,7 @@ public void setCustomIndicator(@LayoutRes int customIndicatorViewId) {
* @param customIndicatorView the view is the indicator you touch to show;
* @param progressTextViewId the progress id in the indicator root view , this id view must be a textView to show the progress
*/
public void setCustomIndicator(@NonNull View customIndicatorView, @IdRes int progressTextViewId) {
public synchronized void setCustomIndicator(@NonNull View customIndicatorView, @IdRes int progressTextViewId) {
View tv = customIndicatorView.findViewById(progressTextViewId);
if (tv == null) {
throw new IllegalArgumentException(" can not find the textView in topContentView by progressTextViewId. ");
Expand Down Expand Up @@ -1184,6 +1188,11 @@ public void setOnSeekChangeListener(@NonNull OnSeekBarChangeListener listener) {
this.mListener = listener;
}

public boolean isTouchThumb(float mX) {
float rawTouchX = getTouchX();
return rawTouchX - p.mThumbSize / 2f <= mX && mX <= rawTouchX + p.mThumbSize / 2f;
}

public interface OnSeekBarChangeListener {

/**
Expand Down Expand Up @@ -1738,6 +1747,18 @@ public Builder forbidUserToSeek(boolean forbidding) {
return this;
}


/**
* user change the thumb's location by touching thumb/touching track
*
* @param touchToSeek true for seeking by touch track, false for seeking by thumb.default true;
* @return
*/
public Builder touchToSeek(boolean touchToSeek) {
p.mTouchToSeek = touchToSeek;
return this;
}

Builder setParams(BuilderParams p) {
this.p = p;
return this;
Expand Down
3 changes: 2 additions & 1 deletion indicatorseekbar/src/main/res/values/attr.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<attr name="isb_progress" format="float" /><!-- the current progress value of seekBar, default 0-->
<attr name="isb_clear_default_padding" format="boolean" /><!-- set seekBar's leftPadding&rightPadding to zero, default false, default padding is 16dp-->
<attr name="isb_forbid_user_seek" format="boolean" /><!--prevent user from touching to seek, default false-->
<attr name="isb_touch_to_seek" format="boolean" /><!--user change the thumb's location by touching thumb/touching track,true for touching track to seek. false for touching thumb; default true-->
<attr name="isb_progress_value_float" format="boolean" /><!--set the value of seekBar to float type, default false-->
<attr name="isb_seek_bar_type"><!-- the type for seekBar, default 0.-->
<enum name="continuous" value="0" />
Expand Down Expand Up @@ -58,7 +59,7 @@
<attr name="isb_text_right_end" format="string|reference" /><!--set the text below seekBar right end, default max value string, work on seekBar type :CONTINUOUS_TEXTS_ENDS/DISCRETE_TICKS_TEXTS/DISCRETE_TICKS_TEXTS_ENDS-->
<attr name="isb_text_size" format="dimension|reference" /><!--set the text size of tick below text, default 13sp-->
<attr name="isb_text_array" format="reference" /><!--set the texts below tick to replace default progress text, default string of progress, work on seekBar type :DISCRETE_TICKS_TEXTS-->
<attr name="isb_text_typeface"><!--select the tick shape type, default rectangle/1-->
<attr name="isb_text_typeface"><!--select the text typeface, default normal-->
<enum name="normal" value="0" />
<enum name="monospace" value="1" />
<enum name="sans" value="2" />
Expand Down

0 comments on commit dbe6435

Please sign in to comment.