仪表盘 网速测试 自定义view

模拟器上面的效果图

转载请标明出处: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

类下载地址:http://download.csdn.net/detail/u012840567/9732655

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值