Android自定义仪表盘

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();

}



}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值