您的位置:首页 > 移动开发 > Android开发

Android自定义仪表盘

2017-01-01 18:53 453 查看
public class panelview
extends View {

private Paint
paint=new Paint();

Paint textpaint=new Paint();

Paint mPaint=new Paint();

TextView text;

float
dg=120;

float
num=66;

public panelview(Context context,AttributeSet attrs) {

super(context,attrs);

//
TODO Auto-generated constructor stub

//
handler.post(redrawRunnable);



public
void setdg(float dgree) {

dg=(dgree*180)/100;

num=dgree;

invalidate();

}

protected
void onDraw(Canvas canvas) {

//
TODO Auto-generated method stub

paint.setAntiAlias(true);    

    paint.setStyle(Style.STROKE);    

    canvas.translate(canvas.getWidth()/3, 250);

    paint.setStrokeWidth(110);

    paint.setColor(0xff1e7d1c);

    RectF vol1=new RectF(-120,-120,120,120);

    canvas.drawArc(vol1, 180,
dg, false,
paint);

    paint.setColor(0xffd3d3d3);

    canvas.drawArc(vol1, 180+dg, 180-dg,
false, paint);

    //canvas.drawCircle(0, 0, 200, paint); //??ԲȦ    

    mPaint.setAntiAlias(true);

    mPaint.setStyle(Style.STROKE);

    mPaint.setStrokeWidth(10);

    mPaint.setColor(0xffd3d3d3);

    canvas.drawCircle(0, 0, 15,
mPaint);

   

    canvas.save();  

   

    Paint citePaint =
new Paint(paint); 

    citePaint.setColor(Color.BLACK);

    citePaint.setTextSize(18);    

    citePaint.setStrokeWidth(2); 

    citePaint.setStyle(Style.FILL);

    canvas.drawText("State of charge", -60, 50, citePaint);

    citePaint.setTextSize(30);

    citePaint.setStrokeWidth(4);

    canvas.drawText((int)(num)+"%", -30, 90, citePaint);

     

    canvas.restore(); 

         

    Paint tmpPaint =
new Paint(paint);
//画白色小圆圈   

    tmpPaint.setStrokeWidth(1);  

    tmpPaint.setColor(Color.WHITE);

    tmpPaint.setStyle(Style.FILL_AND_STROKE);  

    float  y=150;    

    int count = 30;
//控制数量   

         

    for(int i=0 ; i <count ; i++){  

    if(i>=6&&i<=22){

        if(i%5 == 0){    

         
// canvas.drawLine(0, y, 0, y+10, tmpPaint);  

        canvas.drawCircle(0, y, 5, tmpPaint);

         

        }else{    

           
//canvas.drawLine(0f, y+3f, 0f, y +10f, tmpPaint); 

        canvas.drawCircle(0, y, 5, tmpPaint);

        } 

    }

        canvas.rotate(360/count,0f,0f);
//??ת??ֽ  

     

    }  

         

    //画指针   

    canvas.rotate(dg-90, 0, 0);

    tmpPaint.setStrokeWidth(2);

    tmpPaint.setStyle(Style.FILL);

    tmpPaint.setColor(Color.BLACK);

  // canvas.drawCircle(0, -150, 8, tmpPaint);

    Path path=new Path();

    path.moveTo(-7, 0);

    path.lineTo(7, 0);

    path.lineTo(0, -190);

    path.close();

    //canvas.drawLine(0, 0, 0, -150, tmpPaint); 

    canvas.drawPath(path, tmpPaint);

   

    canvas.restore();

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android