public class MySeekBar extends androidx.appcompat.widget.AppCompatSeekBar {
private Drawable thumb;
private Resources res;
private Paint paint;
private Bitmap bmp;
private Drawable mThumb;
public MySeekBar(Context context) {
super(context);
}
@SuppressWarnings("deprecation")
public MySeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setTextAlign(Paint.Align.CENTER);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setColor(getResources().getColor(R.color.uytewryuew));
res = context.getResources();
if (Pixels.getpixels_x(100) == 720) {
bmp = BitmapFactory.decodeResource(res, R.drawable.jiojjoi);
} else {
bmp = BitmapFactory.decodeResource(res, R.drawable.jiojjoi);
}
thumb = new BitmapDrawable(bmp);
paint.setTextSize(sp2px(13));
// 设置拖动的图片
setThumb(thumb);
// 图片的位置
setThumbOffset(thumb.getIntrinsicWidth());
}
public int sp2px(float spValue) {
final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
@Override
public void setThumb(Drawable thumb) {
super.setThumb(thumb);
this.mThumb = thumb;
}
public Drawable getSeekBarThumb() {
return mThumb;
}
//设置thumb的偏移数值
@Override
public void setThumbOffset(int thumbOffset) {
super.setThumbOffset(thumbOffset / 3);
}
String temp_str = "0";
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
Rect rect = getSeekBarThumb().getBounds();
canvas.drawText(temp_str, rect.left + (rect.width()) / 3.0F, rect.top - paint.ascent() + (rect.height() - (paint.descent() - paint.ascent())) / 2.0F, paint);
canvas.restore();
}
private Bitmap drawableToBitmap(Drawable drawable) {
// 取 drawable 的长宽
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
// 取 drawable 的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 建立对应 bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
public void SetValue(String value) {
StringBuffer sb = new StringBuffer();
sb.append(value);
temp_str = sb.toString();
invalidate();
}
@SuppressLint("NewApi")
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public void setOnSeekBarChangeListener(final OnSeekBarChangeListener l) {
super.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (l != null) {
l.onProgressChanged(seekBar, progress, fromUser);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
if (l != null) {
l.onStartTrackingTouch(seekBar);
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (l != null) {
l.onStopTrackingTouch(seekBar);
}
}
});
}
}
这个会让thumb左右显示不全,不要设置最小进度0和最大进度100