圆形进度条

xiaoxiao2021-02-28  45

public class CustomProgrssView extends View { private Paint paint; private boolean running=true; private int progress=0; public CustomProgrssView(Context context) { super(context); } public CustomProgrssView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public CustomProgrssView(Context context, AttributeSet attrs) { super(context, attrs); //创建一个画笔 paint=new Paint(); //抗锯齿 paint.setAntiAlias(true); //设置画笔的颜色 paint.setColor(Color.RED); //设置画笔,填充是空心的 paint.setStyle(Paint.Style.STROKE); new Thread(new Runnable() { @Override public void run() { while(running){ if(progress>=360){ running=false; return; } System.out.println("progress="+progress); progress+=10; //子线程刷新,系统调用onDraw()方法 postInvalidate(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } float sweep; @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int x=getWidth()/2; int y=getHeight()/2; int radius=200; //设置画笔的粗细 paint.setStrokeWidth(30); //定义一个区域 RectF rectF=new RectF(x-radius,y-radius,x+radius,y+radius); //画弧 canvas.drawArc(rectF,-90,progress,false,paint); int text= (int) ((float)progress/360*100); //measureText 测量字符串的宽度 float textWidth=paint.measureText(text+"%"); Rect rectText=new Rect(); //rectText.length()求子符串的高度 paint.getTextBounds(text+"%",0,(text+"%").length(),rectText ); paint.setTextSize(30); paint.setStrokeWidth(1); canvas.drawText(text+"%",x-textWidth/2,y+rectText.height()/2,paint); }

}

转载请注明原文地址: https://www.6miu.com/read-800161.html

最新回复(0)