public class LightSeekBar extends View {
//资源图片
private Bitmap bitmap;
//画笔
private Paint mPaint;
//上次手指离开屏幕时的位置
private float xLast = 0.0f;
//每个单元的宽度
private int width = 0;
//单元内的文字
private List<String> points;
//结束时监听回调
private OnLightSeekBarChangeListener onLightSeekBarChangeListener;
/**
* 构造方法
*
* @param context
*/
public LightSeekBar(Context context) {
super(context);
init(context);
}
public LightSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
bitmap = BitmapUtil.readBitMap(context, R.drawable.mycar_setting_light_seekbar_btn);
mPaint = new Paint();
points = Arrays.asList(new String[]{"10%", "20%", "30%", "40%", "50%", "60%", "70%", "80%", "90%", "100%"});
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawTxt(canvas);
drawImg(canvas, xLast);
}
/**
* 手指触摸事件
*
* @param event
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
invalidateImg(event);
break;
case MotionEvent.ACTION_UP:
onFinish();
break;
}
return true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
* 手指离开屏幕时,设置滑块的位置
*/
private int onFinish() {
int point = 10;
if (xLast < width * 0.5) {
xLast = 0;
point = 10;
} else if (xLast < width * 1.5f) {
xLast = width;
point = 20;
} else if (xLast < width * 2.5f) {
xLast = width * 2;
point = 30;
} else if (xLast < width * 3.5f) {
xLast = width * 3;
point = 40;
} else if (xLast < width * 4.5f) {
xLast = width * 4;
point = 50;
} else if (xLast < width * 5.5f) {
xLast = width * 5;
point = 60;
} else if (xLast < width * 6.5f) {
xLast = width * 6;
point = 70;
} else if (xLast < width * 7.5f) {
xLast = width * 7;
point = 80;
} else if (xLast < width * 8.5f) {
xLast = width * 8;
point = 90;
} else {
xLast = width * 9;
point = 100;
}
invalidate();
onChange(point);
return point;
}
/**
* 绘制文字
*
* @param canvas
*/
private void drawTxt(Canvas canvas) {
// //绘制底色 // canvas.drawARGB(255, 0, 0, 0);
int maxWidth = getWidth();
if (0 == maxWidth) {
throw new RuntimeException("cant get width");
} else {
if (width == 0) {
width = maxWidth / points.size();
}
mPaint.setAlpha(255);
mPaint.setAntiAlias(true);
mPaint.setColor(Color.YELLOW);
if (port) {
mPaint.setTextSize(30);
} else {
mPaint.setTextSize(45);
}
mPaint.setTextAlign(Paint.Align.CENTER);
for (int i = 0; i < points.size(); i++) {
canvas.drawText(points.get(i), (int) ((i + 0.5) * width), 80, mPaint);
}
}
}
/**
* 画遮挡层
*
* @param canvas
* @param x
*/
private void drawImg(Canvas canvas, float x) {
//计算bitmap缩放比例
int newWidth = width;
int newHeight = getHeight();
float scaleWidth = ((float) newWidth) / bitmap.getWidth();
float scaleHeight = ((float) newHeight) / bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap target = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
canvas.drawBitmap(target, x, 0, mPaint);
mPaint.setColor(Color.rgb(13, 13, 13));
//绘制左侧黑色遮挡层
canvas.drawRect(0, 0, xLast, getHeight(), mPaint);
//绘制右侧黑色遮挡层
canvas.drawRect(xLast + width, 0, getWidth(), getHeight(), mPaint);
}
/**
* 根据手指位置,重新绘制Bitmap和遮挡层
*
* @param event
*/
private void invalidateImg(MotionEvent event) {
xLast = event.getX() - (int) (0.5f * width);
if (-0.5f <= xLast && xLast <= width * 9 + 0.5f) {
invalidate();
}
}
/**
* 暴露一个方法,供外部调用
*
* @param progress
*/
private void onChange(int progress) {
if (onLightSeekBarChangeListener != null) {
onLightSeekBarChangeListener.onLightChange(progress);
}
}
public void setOnLightSeekBarChangeListener(OnLightSeekBarChangeListener onLightSeekBarChangeListener) {
this.onLightSeekBarChangeListener = onLightSeekBarChangeListener;
}
/**
* SeekBar 手指抬起时的监听
*/
public interface OnLightSeekBarChangeListener {
void onLightChange(int progress);
}
public void setCurrentProgress(int progress) {
if (width == 0) {
width = getWidth() / points.size();
}
xLast = (float)( ((progress - 10)/ 10) * width);
invalidate();
}
public int getCurrentProgress() {
return onFinish();
}
}
来源:oschina
链接:https://my.oschina.net/u/2517638/blog/692107