Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	indicatorseekbar/src/main/java/com/warkiz/widget/IndicatorSeekBar.java
  • Loading branch information
warkiz committed Dec 9, 2018
2 parents 87099a4 + 7f864f6 commit 88b6f96
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 26 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ implementation 'com.github.warkiz.widget:indicatorseekbar:2.1.0'
app:isb_track_background_color="@color/color_gray"
app:isb_track_background_size="2dp"
app:isb_track_progress_color="@color/color_blue"
app:isb_track_progress_size="4dp" />
app:isb_track_progress_size="4dp"
app:isb_only_thumb_draggable="false"/>
```

#### Java
Expand Down Expand Up @@ -86,6 +87,7 @@ implementation 'com.github.warkiz.widget:indicatorseekbar:2.1.0'
.trackProgressSize(4)
.trackBackgroundColor(getResources().getColor(R.color.color_gray))
.trackBackgroundSize(2)
.onlyThumbDraggable(false)
.build();

```
Expand Down Expand Up @@ -265,7 +267,7 @@ seekBar.setOnSeekChangeListener(new OnSeekChangeListener() {

## Attributes

[ attr.xml ](https://github.com/warkiz/IndicatorSeekBar/blob/master/indicatorseekBar/src/main/res/values/attr.xml)
[ attr.xml ](https://github.com/warkiz/IndicatorSeekBar/blob/master/indicatorseekbar/src/main/res/values/attr.xml)

## Contact me

Expand All @@ -281,13 +283,13 @@ Buy me a coffee by Alipay or WeChatPay, thanks
## License

Copyright (C) 2017 zhuangguangquan(庄广权)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
93 changes: 93 additions & 0 deletions indicatorseekbar/src/main/java/com/warkiz/widget/FormatUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.warkiz.widget;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;

/**
* by zhyhang
* from https://blog.csdn.net/zhyhang/article/details/17973049
*/
class FormatUtils {

private final static char[][] LEADING_DECIMALS = new char[][]{
"0.".toCharArray(), "0.0".toCharArray(),
"0.00".toCharArray(), "0.000".toCharArray(), "0.0000".toCharArray(),
"0.00000".toCharArray(),
"0.000000".toCharArray(), "0.0000000".toCharArray(), "0.00000000".toCharArray(),
"0.000000000".toCharArray(), "0.0000000000".toCharArray(), "0.00000000000".toCharArray(),
"0.000000000000".toCharArray(), "0.0000000000000".toCharArray(),
"0.00000000000000".toCharArray(),
"0.000000000000000".toCharArray()
};

/**
* 快速格式化一个double,尾零去除(非对齐)<br>
* 等同于:<br>
* NumberFormat f = NumberFormat.getNumberInstance();<br>
* f.setGroupingUsed(false);<br>
* f.setMaximumFractionDigits(precision);<br>
* f.format(d);<br>
* 但一般情况效率高于NumberFormat一倍,且精度无丢失。<br>
*
* @param d the double value
* @param precision [0,16]
*/
static String fastFormat(double d, int precision) {
int posPrecision = Math.abs(precision);
double roundUpVal = Math.abs(d) * Math.pow(10d, posPrecision) + 0.5d;
if (roundUpVal > 999999999999999d || posPrecision > 16) {// double has max 16 precisions
return bigDecFormat(d, posPrecision);
}
long longPart = (long) Math.nextUp(roundUpVal);
if (longPart < 1) {
return "0";
}
char[] longPartChars = Long.toString(longPart).toCharArray();
char[] formatChars;
if (longPartChars.length > posPrecision) {
int end = longPartChars.length - 1;
int decIndex = longPartChars.length - posPrecision;
while (end >= decIndex && longPartChars[end] == '0') {
end--;
}
if (end >= decIndex) {
formatChars = new char[end + 2];
System.arraycopy(longPartChars, 0, formatChars, 0, decIndex);
formatChars[decIndex] = '.';
System.arraycopy(longPartChars, decIndex, formatChars,
decIndex + 1, end - decIndex + 1);
} else {
formatChars = new char[decIndex];
System.arraycopy(longPartChars, 0, formatChars, 0, decIndex);
}
} else {
int end = longPartChars.length - 1;
while (end >= 0 && longPartChars[end] == '0') {
end--;
}
char[] leadings = LEADING_DECIMALS[posPrecision - longPartChars.length];
formatChars = Arrays.copyOf(leadings, leadings.length + end + 1);
System.arraycopy(longPartChars, 0, formatChars, leadings.length, end + 1);
}
return Math.signum(d) > 0 ? new String(formatChars) : "-" + new String(formatChars);
}

private static String bigDecFormat(double d, int precision) {
String formatStr = new BigDecimal(Double.toString(d)).setScale(Math.abs(precision), RoundingMode.HALF_UP)
.toString();
if (precision == 0) {
return formatStr;
}
int end = formatStr.length() - 1;
while (end >= 0 && formatStr.charAt(end) == '0') {
end--;
}
formatStr = formatStr.substring(0, end + 1);
if (formatStr.charAt(formatStr.length() - 1) == '.') {
formatStr = formatStr.substring(0, formatStr.length() - 1);
}
return formatStr;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,8 @@ private void refreshSeekBarLocation() {
}
initTextsArray();
//adjust thumb auto,so find out the closest progress in the mProgressArr array and replace it.
//it is not necessary to adjust thumb while count is less than 3.
if (mTicksCount > 2) {
//it is not necessary to adjust thumb while count is less than 2.
if (mTicksCount > 1) {
mProgress = mProgressArr[getClosestIndex()];
lastProgress = mProgress;
}
Expand Down Expand Up @@ -526,7 +526,7 @@ private void drawTickMarks(Canvas canvas) {
continue;
}
}
if (i == getThumbPosOnTick() && mTicksCount > 2 && !mSeekSmoothly) {
if (i == getThumbPosOnTick() && mTicksCount > 1 && !mSeekSmoothly) {
continue;
}
if (i <= thumbPosFloat) {
Expand Down Expand Up @@ -628,7 +628,7 @@ private void drawThumb(Canvas canvas) {
}

private void drawThumbText(Canvas canvas) {
if (!mShowThumbText || (mShowTickText && mTicksCount > 2)) {
if (!mShowThumbText || (mShowTickText && mTicksCount > 1)) {
return;
}
mTextPaint.setColor(mThumbTextColor);
Expand Down Expand Up @@ -1266,8 +1266,8 @@ private float calculateProgress(float touchX) {
private float calculateTouchX(float touchX) {
float touchXTemp = touchX;
//make sure the seek bar to seek smoothly always
// while the tick's count is less than 3(tick's count is 1 or 2.).
if (mTicksCount > 2 && !mSeekSmoothly) {
// while the tick's count is less than 2(tick's count is 1).
if (mTicksCount > 1 && !mSeekSmoothly) {
int touchBlockSize = Math.round((touchX - mPaddingLeft) / mSeekBlockLength);
touchXTemp = mSeekBlockLength * touchBlockSize + mPaddingLeft;
}
Expand Down Expand Up @@ -1366,7 +1366,7 @@ private void updateStayIndicator() {
}

private boolean autoAdjustThumb() {
if (mTicksCount < 3 || !mSeekSmoothly) {//it is not necessary to adjust while count less than 3 .
if (mTicksCount < 2 || !mSeekSmoothly) {//it is not necessary to adjust while count less than 2.
return false;
}
if (!mAdjustAuto) {
Expand Down Expand Up @@ -1402,14 +1402,11 @@ public void onAnimationUpdate(ValueAnimator animation) {
* transfer the progress value to string type
*/
private String getProgressString(float progress) {
String progressString;
if (mIsFloatProgress) {
progressString = String.valueOf(BigDecimal.valueOf(progress).
setScale(mScale, BigDecimal.ROUND_HALF_UP).floatValue());
return FormatUtils.fastFormat(progress, mScale);
} else {
progressString = String.valueOf(Math.round(progress));
return String.valueOf(Math.round(progress));
}
return progressString;
}

private int getClosestIndex() {
Expand Down Expand Up @@ -1446,7 +1443,7 @@ private SeekParams collectParams(boolean formUser) {
mSeekParams.progressFloat = getProgressFloat();
mSeekParams.fromUser = formUser;
//for discrete series seek bar
if (mTicksCount > 2) {
if (mTicksCount > 1) {
int rawThumbPos = getThumbPosOnTick();
if (mShowTickText && mTickTextsArr != null) {
mSeekParams.tickText = mTickTextsArr[rawThumbPos];
Expand Down Expand Up @@ -1536,7 +1533,7 @@ View getIndicatorContentView() {

String getIndicatorTextString() {
if (mIndicatorTextFormat != null && mIndicatorTextFormat.contains(FORMAT_TICK_TEXT)) {
if (mTicksCount > 2 && mTickTextsArr != null) {
if (mTicksCount > 1 && mTickTextsArr != null) {
return mIndicatorTextFormat.replace(FORMAT_TICK_TEXT, mTickTextsArr[getThumbPosOnTick()]);
}
} else if (mIndicatorTextFormat != null && mIndicatorTextFormat.contains(FORMAT_PROGRESS)) {
Expand Down Expand Up @@ -1631,7 +1628,7 @@ public synchronized void setProgress(float progress) {
lastProgress = mProgress;
mProgress = progress < mMin ? mMin : (progress > mMax ? mMax : progress);
//adjust to the closest tick's progress
if ((!mSeekSmoothly) && mTicksCount > 2) {
if ((!mSeekSmoothly) && mTicksCount > 1) {
mProgress = mProgressArr[getClosestIndex()];
}
setSeekListener(false);
Expand Down Expand Up @@ -1855,12 +1852,6 @@ public void setDecimalScale(int scale) {
* seekBar.setIndicatorTextFormat("${PROGRESS} miles");
* seekBar.setIndicatorTextFormat("I am ${TICK_TEXT}%");
* <p>
* Kotlin:
* seekBar.setIndicatorTextFormat("\${PROGRESS} %");
* seekBar.setIndicatorTextFormat("\${PROGRESS} miles");
* seekBar.setIndicatorTextFormat("I am \${TICK_TEXT}%");
* <p>
* <p>
* make sure you have custom and show the tick text before you
* use ${TICK_TEXT}% , otherwise will be shown a "" value.
* <p>
Expand Down

0 comments on commit 88b6f96

Please sign in to comment.