效果图
note:onDraw方法中,不要使用getCurrent(),外面要传进来这个值。每次invalidate()都getCurrent()效率低下。
自定义SeekBar
public classMySeekBarextendsandroid.support.v7.widget.AppCompatSeekBar {
privateOnKeySeekBarChangeListenermOnKeySeekBarChangeListener;
/**
*文本的颜色
*/
private intmTitleTextColor;
/**
*文本的大小
*/
private floatmTitleTextSize;
privateStringmTitleText;//文字的内容
private intmyProgress;
private intmaxProgress;
/**
*背景图片
*/
private intimg;
privateBitmapbitmapCenter;
privateBitmapbitmapLeft;
privateBitmapbitmapRight;
//bitmap对应的宽高
private floatimg_width,img_height;
Paintpaint;
private floatnumTextWidth;
//测量seekbar的规格
privateRectrect_seek;
privatePaint.FontMetricsfm;
public static final intTEXT_ALIGN_LEFT=0x00000001;
public static final intTEXT_ALIGN_RIGHT=0x00000010;
public static final intTEXT_ALIGN_CENTER_VERTICAL=0x00000100;
public static final intTEXT_ALIGN_CENTER_HORIZONTAL=0x00001000;
public static final intTEXT_ALIGN_TOP=0x00010000;
public static final intTEXT_ALIGN_BOTTOM=0x00100000;
/**
*文本中轴线X坐标
*/
private floattextCenterX;
/**
*文本baseline线Y坐标
*/
private floattextBaselineY;
/**
*文字的方位
*/
private inttextAlign;
publicMySeekBar(Context context) {
this(context, null);
}
publicMySeekBar(Context context,AttributeSet attrs) {
this(context,attrs,0);
}
publicMySeekBar(Context context,AttributeSet attrs, intdefStyleAttr) {
super(context,attrs,defStyleAttr);
/* TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MySeekBar, defStyleAttr, 0);
int n = array.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = array.getIndex(i);
switch (attr) {
case R.styleable.MySeekBar_textsize:
mTitleTextSize = array.getDimension(attr, 15f);
break;
case R.styleable.MySeekBar_textcolor:
mTitleTextColor = array.getColor(attr, Color.WHITE);
break;
}
}
array.recycle();*/
mTitleTextSize=24;
mTitleTextColor= Color.WHITE;
getImgWH();
paint=newPaint();
paint.setAntiAlias(true);//设置抗锯齿
paint.setTextSize(mTitleTextSize);//设置文字大小
paint.setColor(mTitleTextColor);//设置文字颜色
//设置控件的padding给提示文字留出位置
// setPadding((int) Math.ceil(img_width) / 2, (int) Math.ceil(img_height) + 10, (int) Math.ceil(img_height) / 2, 0);
setPadding(0,(int) Math.ceil(img_height)+10,0,0);
textAlign=TEXT_ALIGN_CENTER_HORIZONTAL|TEXT_ALIGN_CENTER_VERTICAL;
}
/**
*获取图片的宽高
*/
private voidgetImgWH() {
bitmapLeft= BitmapFactory.decodeResource(getResources(),R.mipmap.bg_left_pop);
bitmapCenter= BitmapFactory.decodeResource(getResources(),R.mipmap.bg_pop_center);
bitmapRight= BitmapFactory.decodeResource(getResources(),R.mipmap.bg_pop_right);
img_width=bitmapCenter.getWidth();
img_height=bitmapCenter.getHeight();
}
@Override
protected synchronized voidonDraw(Canvas canvas) {
super.onDraw(canvas);
if(hasFocus()){
setTextLocation();//定位文本绘制的位置
if(null==rect_seek){
rect_seek=this.getProgressDrawable().getBounds();
}
//定位文字背景图片的位置
floatbm_x =rect_seek.width() *myProgress/maxProgress;
floatbm_y =rect_seek.height();
// //计算文字的中心位置在bitmap
floattext_x =rect_seek.width() *myProgress/maxProgress+img_width/2-numTextWidth/2;
floatdeltaX =0;
Bitmap destBitmap=null;
if(bm_x
deltaX =0;
destBitmap=bitmapLeft;
}else if(rect_seek.width() - bm_x
deltaX = -img_width;
destBitmap=bitmapRight;
}else{
deltaX = -img_width/2;
destBitmap=bitmapCenter;
}
canvas.drawBitmap(destBitmap,bm_x + deltaX,bm_y,paint);//画背景图
canvas.drawText(mTitleText,text_x + deltaX,(float) (textBaselineY+ bm_y),paint);//画文字
//canvas.drawText(mTitleText, text_x + deltaX, (float) (textBaselineY /*+ bm_y + (0.16 * img_height / 2)*/), paint);//画文字
}
}
@Override
public booleanonTouchEvent(MotionEvent event) {
invalidate();//监听手势滑动,不断重绘文字和背景图的显示位置
return super.onTouchEvent(event);
}
/**
*定位文本绘制的位置
*/
private voidsetTextLocation() {
fm=paint.getFontMetrics();
//文本的宽度
// mTitleText = getProgress() + 10 + "℃";
numTextWidth=paint.measureText(mTitleText);
floattextCenterVerticalBaselineY =img_height/2-fm.descent+ (fm.descent-fm.ascent) /2;
switch(textAlign) {
caseTEXT_ALIGN_CENTER_HORIZONTAL|TEXT_ALIGN_CENTER_VERTICAL:
textCenterX=img_width/2;
textBaselineY= textCenterVerticalBaselineY;
break;
caseTEXT_ALIGN_LEFT|TEXT_ALIGN_CENTER_VERTICAL:
textCenterX=numTextWidth/2;
textBaselineY= textCenterVerticalBaselineY;
break;
caseTEXT_ALIGN_RIGHT|TEXT_ALIGN_CENTER_VERTICAL:
textCenterX=img_width-numTextWidth/2;
textBaselineY= textCenterVerticalBaselineY;
break;
caseTEXT_ALIGN_BOTTOM|TEXT_ALIGN_CENTER_HORIZONTAL:
textCenterX=img_width/2;
textBaselineY=img_height-fm.bottom;
break;
caseTEXT_ALIGN_TOP|TEXT_ALIGN_CENTER_HORIZONTAL:
textCenterX=img_width/2;
textBaselineY= -fm.ascent;
break;
caseTEXT_ALIGN_TOP|TEXT_ALIGN_LEFT:
textCenterX=numTextWidth/2;
textBaselineY= -fm.ascent;
break;
caseTEXT_ALIGN_BOTTOM|TEXT_ALIGN_LEFT:
textCenterX=numTextWidth/2;
textBaselineY=img_height-fm.bottom;
break;
caseTEXT_ALIGN_TOP|TEXT_ALIGN_RIGHT:
textCenterX=img_width-numTextWidth/2;
textBaselineY= -fm.ascent;
break;
caseTEXT_ALIGN_BOTTOM|TEXT_ALIGN_RIGHT:
textCenterX=img_width-numTextWidth/2;
textBaselineY=img_height-fm.bottom;
break;
}
}
public voidsetmOnKeySeekBarChangeListener(OnKeySeekBarChangeListener onKeySeekBarChangeListener) {
this.mOnKeySeekBarChangeListener= onKeySeekBarChangeListener;
}
public voidinit(String showText) {
this.mTitleText=showText;
}
public interfaceOnKeySeekBarChangeListener {
voidonKeyStartTrackingTouch();
voidonKeyStopTrackingTouch();
}
voidonKeyChange() {
if(null!=mOnKeySeekBarChangeListener) {
mOnKeySeekBarChangeListener.onKeyStartTrackingTouch();
}
}
@Override
public booleanonKeyUp(intkeyCode,KeyEvent event) {
if(null!=mOnKeySeekBarChangeListener) {
if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT|| keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
mOnKeySeekBarChangeListener.onKeyStopTrackingTouch();
}
}
return super.onKeyUp(keyCode,event);
}
public voidupdate(intprogress,String mTitleText,intmaxProgress){
this.mTitleText=mTitleText;
this.myProgress=progress;
this.maxProgress=maxProgress;
invalidate();
}
}
用法
android:id="@+id/progressSeekView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-64dp"
android:focusable="true"
android:paddingEnd="0dp"
android:paddingStart="0dp"
style="@style/ProgressBar_Mini"
android:thumb="@mipmap/progress_thumb"
/>
style/ProgressBar_Mini