version:beta
fix: update:seekbar增加刻度
This commit is contained in:
@@ -8,6 +8,7 @@ import android.graphics.Paint;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Build;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
|
||||
public class RulerSeekBar extends androidx.appcompat.widget.AppCompatSeekBar {
|
||||
|
||||
@@ -29,13 +30,15 @@ public class RulerSeekBar extends androidx.appcompat.widget.AppCompatSeekBar {
|
||||
/**
|
||||
* 刻度线的颜色
|
||||
*/
|
||||
private int mRulerColor = Color.WHITE;
|
||||
private int mRulerColor = Color.GRAY;
|
||||
|
||||
/**
|
||||
* 滑块上面是否要显示刻度线
|
||||
*/
|
||||
private boolean isShowTopOfThumb = true;
|
||||
|
||||
private int radius = 8;
|
||||
|
||||
public RulerSeekBar(Context context) {
|
||||
super(context);
|
||||
init();
|
||||
@@ -80,23 +83,46 @@ public class RulerSeekBar extends androidx.appcompat.widget.AppCompatSeekBar {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
int thumbRadius = 0;
|
||||
if (getThumb() != null) {
|
||||
Rect rect = getThumb().getBounds();
|
||||
thumbRadius = (rect.right - rect.left) / 2;
|
||||
}
|
||||
Log.e("RulerSeekBar", "onDraw: thumbRadius = " + thumbRadius);
|
||||
|
||||
|
||||
//获取每一份的长度
|
||||
int length = (getWidth() - getPaddingLeft() - getPaddingRight() - mRulerCount * mRulerWidth) / (mRulerCount + 1);
|
||||
int length = (getWidth() - getPaddingLeft() - getPaddingRight() - (mRulerCount + 1) * (radius) - thumbRadius * 2) / (mRulerCount + 1);
|
||||
Log.e("RulerSeekBar", "onDraw: mRulerCount = " + mRulerCount);
|
||||
|
||||
Log.e("RulerSeekBar", "onDraw: getWidth = " + getWidth());
|
||||
Log.e("RulerSeekBar", "onDraw: getPaddingLeft = " + getPaddingLeft());
|
||||
Log.e("RulerSeekBar", "onDraw: getPaddingRight = " + getPaddingRight());
|
||||
Log.e("RulerSeekBar", "onDraw: length = " + length);
|
||||
|
||||
//计算刻度线的顶部坐标和底部坐标
|
||||
Log.e("RulerSeekBar", "onDraw: getHeight = " + getHeight());
|
||||
Log.e("RulerSeekBar", "onDraw: getMinimumHeight = " + getMinimumHeight());
|
||||
|
||||
int rulerTop = getHeight() / 2 - getMinimumHeight() / 2;
|
||||
int rulerBottom = rulerTop + getMinimumHeight();
|
||||
Log.e("RulerSeekBar", "onDraw: rulerTop = " + rulerTop);
|
||||
Log.e("RulerSeekBar", "onDraw: rulerBottom = " + rulerBottom);
|
||||
int center = getHeight() / 2;
|
||||
|
||||
//获取滑块的位置信息
|
||||
Rect thumbRect = null;
|
||||
if (getThumb() != null) {
|
||||
thumbRect = getThumb().getBounds();
|
||||
}
|
||||
Log.e("RulerSeekBar", "onDraw: thumbRect = " + thumbRect);
|
||||
|
||||
//绘制刻度线
|
||||
for (int i = 1; i <= mRulerCount; i++) {
|
||||
for (int i = 0; i <= mRulerCount + 1; i++) {
|
||||
//计算刻度线的左边坐标和右边坐标
|
||||
int rulerLeft = i * length + getPaddingLeft();
|
||||
Log.e("RulerSeekBar", "onDraw: rulerLeft = " + rulerLeft);
|
||||
int rulerRight = rulerLeft + mRulerWidth;
|
||||
|
||||
//判断是否需要绘制刻度线
|
||||
@@ -104,9 +130,13 @@ public class RulerSeekBar extends androidx.appcompat.widget.AppCompatSeekBar {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (rulerLeft > thumbRect.left && rulerLeft < thumbRect.right) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//进行绘制
|
||||
// canvas.drawRect(rulerLeft, rulerTop, rulerRight, rulerBottom, mRulerPaint);
|
||||
canvas.drawCircle(rulerLeft, rulerTop, 12, mRulerPaint);
|
||||
canvas.drawCircle(rulerLeft + radius + thumbRadius, center, radius, mRulerPaint);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user