最近开发中设计给出了一个类似于仪表盘的控件,虽然可以用多张图代替,但是还是写了一个类似的自定义控件,成果如下图:
1、首先把原点设置在圆心
//把方框平移到正中间
canvas.translate(dx, dy);
canvas.save();
canvasR = Math.min(getWidth(), getHeight()) / 2;
markWidth = canvasR / 20;
tableWidth = canvasR / 10;
mPaint.setStyle(Paint.Style.STROKE);
2、从外围开始描绘控件
//填写文字刻度
mPaint.setTextSize(outTextSize);
int count = 0;
while (count < mMaxCount) {
//填写外围文字
Rect bounds = new Rect();
String text = 100 / (mMaxCount - 1) * count + "";
mPaint.getTextBounds(text, 0, text.length(), bounds);
float offSet = (bounds.bottom - bounds.top);