Skip to content

Commit

Permalink
完善方法文档
Browse files Browse the repository at this point in the history
  • Loading branch information
JayGoo committed Jun 18, 2019
1 parent e3df3a3 commit 87e78c2
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 41 deletions.
1 change: 0 additions & 1 deletion .gitignore
@@ -1,5 +1,4 @@
# Built application files
*.apk
*.ap_

# Files for the ART/Dalvik VM
Expand Down
86 changes: 63 additions & 23 deletions RangeSeekBar/src/main/java/com/jaygoo/widget/RangeSeekBar.java
Expand Up @@ -60,25 +60,40 @@ public class RangeSeekBar extends View {
}

//tick mark text gravity
public final static int TRICK_MARK_GRAVITY_LEFT = 0;
public final static int TRICK_MARK_GRAVITY_CENTER = 1;
public final static int TRICK_MARK_GRAVITY_RIGHT = 2;
public final static int TICK_MARK_GRAVITY_LEFT = 0;
public final static int TICK_MARK_GRAVITY_CENTER = 1;
public final static int TICK_MARK_GRAVITY_RIGHT = 2;

/**
* @hide
*/
@IntDef({TRICK_MARK_GRAVITY_LEFT, TRICK_MARK_GRAVITY_CENTER, TRICK_MARK_GRAVITY_RIGHT})
@IntDef({TICK_MARK_GRAVITY_LEFT, TICK_MARK_GRAVITY_CENTER, TICK_MARK_GRAVITY_RIGHT})
@Retention(RetentionPolicy.SOURCE)
public @interface TickMarkGravityDef {
}

/**
* @hide
*/
@IntDef({Gravity.TOP, Gravity.BOTTOM})
@Retention(RetentionPolicy.SOURCE)
public @interface TickMarkLayoutGravityDef {
}

/**
* @hide
*/
@IntDef({Gravity.TOP, Gravity.CENTER, Gravity.BOTTOM})
@Retention(RetentionPolicy.SOURCE)
public @interface GravityDef {
}

public static class Gravity {
public final static int TOP = 0;
public final static int BOTTOM = 1;
public final static int CENTER = 2;
}


private int progressTop, progressBottom, progressLeft, progressRight;
private int seekBarMode;
//刻度模式:number根据数字实际比例排列;other 均分排列
Expand Down Expand Up @@ -135,15 +150,14 @@ public static class Gravity {
//the thumb will automatic bonding close to its value
private boolean stepsAutoBonding;
private int stepsDrawableId;

//****************** the above is attr value ******************//
//用户设置的真实的最大值和最小值
//True values set by the user
float minProgress, maxProgress;
private float minProgress, maxProgress;
//****************** the above is attr value ******************//

private boolean isEnable = true;
float touchDownX,touchDownY;
//剩余最小间隔的进度
float reservePercent;
boolean isEnable = true;
boolean isScaleThumb = false;
Paint paint = new Paint();
RectF progressDefaultDstRect = new RectF();
Expand Down Expand Up @@ -218,7 +232,7 @@ private void initAttrs(AttributeSet attrs) {
progressDefaultDrawableId = t.getResourceId(R.styleable.RangeSeekBar_rsb_progress_drawable_default, 0);
progressHeight = (int) t.getDimension(R.styleable.RangeSeekBar_rsb_progress_height, Utils.dp2px(getContext(), 2));
tickMarkMode = t.getInt(R.styleable.RangeSeekBar_rsb_tick_mark_mode, TRICK_MARK_MODE_NUMBER);
tickMarkGravity = t.getInt(R.styleable.RangeSeekBar_rsb_tick_mark_gravity, TRICK_MARK_GRAVITY_CENTER);
tickMarkGravity = t.getInt(R.styleable.RangeSeekBar_rsb_tick_mark_gravity, TICK_MARK_GRAVITY_CENTER);
tickMarkLayoutGravity = t.getInt(R.styleable.RangeSeekBar_rsb_tick_mark_layout_gravity, Gravity.TOP);
tickMarkTextArray = t.getTextArray(R.styleable.RangeSeekBar_rsb_tick_mark_text_array);
tickMarkTextMargin = (int) t.getDimension(R.styleable.RangeSeekBar_rsb_tick_mark_text_margin, Utils.dp2px(getContext(), 7));
Expand Down Expand Up @@ -369,16 +383,16 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
onDrawTickMark(canvas);
onDrawProgressBar(canvas);
onDrawSteps(canvas);
onDrawTickMark(canvas, paint);
onDrawProgressBar(canvas, paint);
onDrawSteps(canvas, paint);
onDrawSeekBar(canvas);
}

//绘制刻度,并且根据当前位置是否在刻度范围内设置不同的颜色显示
// Draw the scales, and according to the current position is set within
// the scale range of different color display
protected void onDrawTickMark(Canvas canvas) {
protected void onDrawTickMark(Canvas canvas, Paint paint) {
if (tickMarkTextArray != null) {
int trickPartWidth = progressWidth / (tickMarkTextArray.length - 1);
for (int i = 0; i < tickMarkTextArray.length; i++) {
Expand All @@ -389,9 +403,9 @@ protected void onDrawTickMark(Canvas canvas) {
//平分显示
float x;
if (tickMarkMode == TRICK_MARK_MODE_OTHER) {
if (tickMarkGravity == TRICK_MARK_GRAVITY_RIGHT) {
if (tickMarkGravity == TICK_MARK_GRAVITY_RIGHT) {
x = getProgressLeft() + i * trickPartWidth - tickMarkTextRect.width();
} else if (tickMarkGravity == TRICK_MARK_GRAVITY_CENTER) {
} else if (tickMarkGravity == TICK_MARK_GRAVITY_CENTER) {
x = getProgressLeft() + i * trickPartWidth - tickMarkTextRect.width() / 2f;
} else {
x = getProgressLeft() + i * trickPartWidth;
Expand Down Expand Up @@ -419,7 +433,7 @@ protected void onDrawTickMark(Canvas canvas) {

//绘制进度条
// draw the progress bar
protected void onDrawProgressBar(Canvas canvas) {
protected void onDrawProgressBar(Canvas canvas, Paint paint) {

//draw default progress
if (Utils.verifyBitmap(progressDefaultBitmap)) {
Expand Down Expand Up @@ -462,7 +476,7 @@ protected void onDrawProgressBar(Canvas canvas) {
}

//draw steps
protected void onDrawSteps(Canvas canvas) {
protected void onDrawSteps(Canvas canvas, Paint paint) {
if (!verifyStepsMode()) return;
int stepMarks = getProgressWidth() / (steps);
float extHeight = (stepsHeight - getProgressHeight()) / 2f;
Expand Down Expand Up @@ -530,7 +544,6 @@ private void scaleCurrentSeekBarThumb() {
if (currTouchSB != null && currTouchSB.getThumbScaleRatio() > 1f && !isScaleThumb) {
isScaleThumb = true;
currTouchSB.scaleThumb();

}
}

Expand Down Expand Up @@ -956,6 +969,11 @@ public int getSeekBarMode() {
return seekBarMode;
}

/**
* {@link #SEEKBAR_MODE_SINGLE} is single SeekBar
* {@link #SEEKBAR_MODE_RANGE} is range SeekBar
* @param seekBarMode
*/
public void setSeekBarMode(@SeekBarModeDef int seekBarMode) {
this.seekBarMode = seekBarMode;
rightSB.setVisible(seekBarMode != SEEKBAR_MODE_SINGLE);
Expand All @@ -965,6 +983,11 @@ public int getTickMarkMode() {
return tickMarkMode;
}

/**
* {@link #TICK_MARK_GRAVITY_LEFT} is number tick mark, it will locate the position according to the value.
* {@link #TICK_MARK_GRAVITY_RIGHT} is text tick mark, it will be equally positioned.
* @param tickMarkMode
*/
public void setTickMarkMode(@TickMarkModeDef int tickMarkMode) {
this.tickMarkMode = tickMarkMode;
}
Expand All @@ -989,6 +1012,13 @@ public int getTickMarkGravity() {
return tickMarkGravity;
}

/**
* the tick mark text gravity
* {@link #TICK_MARK_GRAVITY_LEFT}
* {@link #TICK_MARK_GRAVITY_RIGHT}
* {@link #TICK_MARK_GRAVITY_CENTER}
* @param tickMarkGravity
*/
public void setTickMarkGravity(@TickMarkGravityDef int tickMarkGravity) {
this.tickMarkGravity = tickMarkGravity;
}
Expand Down Expand Up @@ -1130,15 +1160,25 @@ public int getTickMarkLayoutGravity() {
return tickMarkLayoutGravity;
}

public void setTickMarkLayoutGravity(int tickMarkLayoutGravity) {
/**
* the tick mark layout gravity
* Gravity.TOP and Gravity.BOTTOM
* @param tickMarkLayoutGravity
*/
public void setTickMarkLayoutGravity(@TickMarkLayoutGravityDef int tickMarkLayoutGravity) {
this.tickMarkLayoutGravity = tickMarkLayoutGravity;
}

public int getGravity() {
return gravity;
}

public void setGravity(int gravity) {
/**
* the RangeSeekBar gravity
* Gravity.TOP and Gravity.BOTTOM
* @param gravity
*/
public void setGravity(@GravityDef int gravity) {
this.gravity = gravity;
}

Expand All @@ -1154,7 +1194,7 @@ public int getStepsDrawableId() {
return stepsDrawableId;
}

public void setStepsDrawableId(int stepsDrawableId) {
public void setStepsDrawableId(@DrawableRes int stepsDrawableId) {
this.stepsBitmaps.clear();
this.stepsDrawableId = stepsDrawableId;
initStepsBitmap();
Expand Down
23 changes: 14 additions & 9 deletions RangeSeekBar/src/main/java/com/jaygoo/widget/SeekBar.java
Expand Up @@ -23,10 +23,7 @@
import android.text.TextUtils;
import android.util.AttributeSet;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.text.DecimalFormat;
import java.util.IllegalFormatConversionException;


/**
Expand Down Expand Up @@ -81,7 +78,7 @@ public class SeekBar {
int left, right, top, bottom;
float currPercent;
float material = 0;
boolean isShowIndicator;
private boolean isShowIndicator;
boolean isLeft;
Bitmap thumbBitmap;
Bitmap thumbInactivatedBitmap;
Expand Down Expand Up @@ -217,9 +214,9 @@ protected void draw(Canvas canvas) {
// translate canvas, then don't care left
canvas.translate(left, 0);
if (isShowIndicator) {
drawIndicator(canvas, paint, formatCurrentIndicatorText(userText2Draw));
onDrawIndicator(canvas, paint, formatCurrentIndicatorText(userText2Draw));
}
drawThumb(canvas);
onDrawThumb(canvas);
canvas.restore();
}

Expand All @@ -233,7 +230,7 @@ protected void draw(Canvas canvas) {
*
* @param canvas canvas
*/
protected void drawThumb(Canvas canvas) {
protected void onDrawThumb(Canvas canvas) {
if (thumbInactivatedBitmap != null && !isActivate) {
canvas.drawBitmap(thumbInactivatedBitmap, 0, rangeSeekBar.getProgressTop() + (rangeSeekBar.getProgressHeight() - scaleThumbHeight) / 2f, null);
} else if (thumbBitmap != null) {
Expand Down Expand Up @@ -278,7 +275,7 @@ protected String formatCurrentIndicatorText(String text2Draw) {
* @param canvas Canvas
* @param text2Draw Indicator text
*/
protected void drawIndicator(Canvas canvas, Paint paint, String text2Draw) {
protected void onDrawIndicator(Canvas canvas, Paint paint, String text2Draw) {
if (text2Draw == null) return;
paint.setTextSize(indicatorTextSize);
paint.setStyle(Paint.Style.FILL);
Expand Down Expand Up @@ -494,6 +491,14 @@ public int getIndicatorShowMode() {
return indicatorShowMode;
}

/**
* the indicator show mode
* {@link #INDICATOR_SHOW_WHEN_TOUCH}
* {@link #INDICATOR_ALWAYS_SHOW}
* {@link #INDICATOR_ALWAYS_SHOW_AFTER_TOUCH}
* {@link #INDICATOR_ALWAYS_SHOW}
* @param indicatorShowMode
*/
public void setIndicatorShowMode(@IndicatorModeDef int indicatorShowMode) {
this.indicatorShowMode = indicatorShowMode;
}
Expand Down Expand Up @@ -680,6 +685,6 @@ public void setVisible(boolean visible) {

public float getProgress() {
float range = rangeSeekBar.getMaxProgress() - rangeSeekBar.getMinProgress();
return rangeSeekBar.minProgress + range * currPercent;
return rangeSeekBar.getMinProgress() + range * currPercent;
}
}
1 change: 0 additions & 1 deletion RangeSeekBar/src/main/java/com/jaygoo/widget/Utils.java
Expand Up @@ -12,7 +12,6 @@
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;
import android.util.Log;

Expand Down
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.IntDef;
import android.text.TextUtils;
import android.util.AttributeSet;
Expand Down Expand Up @@ -128,7 +129,7 @@ protected void onDraw(Canvas canvas) {
}

@Override
protected void onDrawTickMark(Canvas canvas) {
protected void onDrawTickMark(Canvas canvas, Paint paint) {
if (getTickMarkTextArray() != null) {
int arrayLength = getTickMarkTextArray().length;
int trickPartWidth = getProgressWidth() / (arrayLength - 1);
Expand All @@ -140,9 +141,9 @@ protected void onDrawTickMark(Canvas canvas) {
//平分显示
float x;
if (getTickMarkMode() == TRICK_MARK_MODE_OTHER) {
if (getTickMarkGravity() == TRICK_MARK_GRAVITY_RIGHT) {
if (getTickMarkGravity() == TICK_MARK_GRAVITY_RIGHT) {
x = getProgressLeft() + i * trickPartWidth - tickMarkTextRect.width();
} else if (getTickMarkGravity() == TRICK_MARK_GRAVITY_CENTER) {
} else if (getTickMarkGravity() == TICK_MARK_GRAVITY_CENTER) {
x = getProgressLeft() + i * trickPartWidth - tickMarkTextRect.width() / 2f;
} else {
x = getProgressLeft() + i * trickPartWidth;
Expand All @@ -154,7 +155,7 @@ protected void onDrawTickMark(Canvas canvas) {
paint.setColor(getTickMarkInRangeTextColor());
}
//按实际比例显示
x = getProgressLeft() + getProgressWidth() * (num - minProgress) / (maxProgress - minProgress)
x = getProgressLeft() + getProgressWidth() * (num - getMinProgress()) / (getMaxProgress() - getMinProgress())
- tickMarkTextRect.width() / 2f;
}
float y;
Expand Down Expand Up @@ -250,6 +251,12 @@ public int getOrientation() {
return orientation;
}

/**
* set VerticalRangeSeekBar Orientation
* {@link #DIRECTION_LEFT}
* {@link #DIRECTION_RIGHT}
* @param orientation
*/
public void setOrientation(@DirectionDef int orientation) {
this.orientation = orientation;
}
Expand All @@ -258,6 +265,12 @@ public int getTickMarkDirection() {
return tickMarkDirection;
}

/**
* set tick mark text direction
* {@link #TEXT_DIRECTION_VERTICAL}
* {@link #TEXT_DIRECTION_HORIZONTAL}
* @param tickMarkDirection
*/
public void setTickMarkDirection(@TextDirectionDef int tickMarkDirection) {
this.tickMarkDirection = tickMarkDirection;
}
Expand Down
Expand Up @@ -2,7 +2,6 @@

import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
Expand Down Expand Up @@ -62,13 +61,13 @@ private void initAttrs(AttributeSet attrs) {
}

@Override
protected void drawIndicator(Canvas canvas, Paint paint, String text2Draw) {
protected void onDrawIndicator(Canvas canvas, Paint paint, String text2Draw) {
if (text2Draw == null) return;
//draw indicator
if (indicatorTextOrientation == TEXT_DIRECTION_VERTICAL) {
drawVerticalIndicator(canvas, paint, text2Draw);
} else {
super.drawIndicator(canvas, paint, text2Draw);
super.onDrawIndicator(canvas, paint, text2Draw);
}
}

Expand Down

0 comments on commit 87e78c2

Please sign in to comment.