转载请标明出处:http://blog.csdn.net/u012840567/article/details/54288937
主要是里面的四个方法
drawArc(canvas,radius);
drawFont(canvas,radius);
drawPoint(canvas,radius);
drawScale(canvas,radius);
/**
* 画圆弧
* @param canvas
* @param radius
*/
private void drawArc(Canvas canvas, int radius) {
RectF arcRectF = new RectF(-radius,-radius,radius,radius);
canvas.drawArc(arcRectF,134,272,false,arcPaint);//画圆弧 这个地方是为了与刻度重合
// canvas.drawArc(arcRectF,135,270,false,arcPaint);//画圆弧
canvas.save();
canvas.restore();
arcPaint.setColor(Color.BLACK);
canvas.drawArc(arcRectF,134,progress,false,arcPaint);//画圆弧 这个地方是为了与刻度重合
}
/**
* 画字
* @param canvas
* @param radius 半径
*/
private void drawFont(Canvas canvas, int radius) {
//测量字体宽度,我们需要根据字体的宽度设置在圆环中间
float outSpeedTextWidth = outSpeedPaint.measureText(outSpeed[1]);
canvas.drawText(outSpeed[0],-(int)(radius*Math.sin(degree))+outSpeedTextWidth/2,(int)(radius*Math.sin(degree)),outSpeedPaint);
canvas.drawText(outSpeed[1],-radius-50,0+line_width,outSpeedPaint);
canvas.drawText(outSpeed[2],-(int)(radius*Math.sin(degree))-line_width,-(int)(radius*Math.sin(degree))+outSpeedTextWidth/2,outSpeedPaint);
canvas.drawText(outSpeed[3],0-outSpeedTextWidth/2,-radius-line_width,outSpeedPaint);
canvas.drawText(outSpeed[4],(int)(radius*Math.sin(degree))-outSpeedTextWidth/2,-(int)(radius*Math.sin(degree))+outSpeedTextWidth/2,outSpeedPaint);
canvas.drawText(outSpeed[5],radius+line_width,0+line_width,outSpeedPaint);
canvas.drawText(outSpeed[6],(int)(radius*Math.sin(degree))-outSpeedTextWidth,(int)(radius*Math.sin(degree)),outSpeedPaint);
float textWidth = centerPaint.measureText(centerSpeed); //中间字体的宽度
canvas.drawText(centerSpeed,-textWidth/2,centerSize/2,centerPaint);
float speedTextWidth = speedPaint.measureText(speedFont); //测量字体宽度,我们需要根据字体的宽度设置在圆环中间
canvas.drawText(speedFont,-speedTextWidth/2,(int)(radius * Math.sin(degree) - speedTextWidth/2),speedPaint);
}
/**
* 画指针
* @param canvas
* @param radius
*/
private void drawPoint(Canvas canvas, int radius) {
Path path = new Path();
path.moveTo(-radius+50, 0);// 此点为多边形的起点
path.lineTo(-radius+150,10);
path.lineTo(-radius+150,-10);
path.close();
canvas.save(); // 保存canvas状态
canvas.rotate(progress-45);
canvas.drawPath(path,arrowsPaint);
canvas.restore();
}
/**
* 画刻度
* @param canvas
* @param radius
*/
private void drawScale(Canvas canvas, int radius) {
canvas.save(); // 保存canvas状态
canvas.rotate(-90);
radius = radius-50;//分辨率影响半径
int startx = (int)(radius*Math.sin(degree));
int starty = (int)(radius*Math.sin(degree));
int stopx = (int)((radius-20)*Math.sin(degree));
int stopy = (int)((radius-20)*Math.sin(degree));
for (int i = 0; i < 7; i++) {
canvas.drawLine(-startx-line_width/2,-starty-line_width/2,-stopx-line_width/2,-stopy-line_width/2,linePaint);
canvas.rotate(degree);
}
canvas.save(); // 保存canvas状态
canvas.restore();
canvas.rotate(45);
linePaint.setColor(Color.BLACK);
for (int i = 0; i < progressnum; i++) {
canvas.drawLine(-startx-line_width/2,-starty-line_width/2,-stopx-line_width/2,-stopy-line_width/2,linePaint);
canvas.rotate(degree);
}
canvas.save(); // 保存canvas状态
canvas.restore();
}
使用方式:
speed_view = (ViewSpeed) findViewById(R.id.speed_view);
speed_view.setInternetSpeed(downspeed*1024);//单位是字节的形式 b/s