自定义圆形进度条 public class jindutiao extends View { //定义一个画笔 private Paint paint; private boolean runing = true ; private int progress = 0 ; public jindutiao(Context context) { super(context); } public jindutiao(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 (runing){ if (progress >=360){ runing = false; return; } progress +=10; postInvalidate(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } public jindutiao(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // getWidth() 获取当前View 的宽度 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); //画弧 // useCentor true 从中心点开始画 false 中心点不现实 canvas.drawArc(rectF,-90,progress,false,paint); int text = (int) ((float)progress / 360 * 100 ); // measureText 测量字符串的宽度 float textWidth = paint.measureText(text+"%"); Rect rextText = new Rect(); // rextText.height() 获取字符串的高度 paint.getTextBounds(text+"%",0,(text+"%").length(),rextText); paint.setTextSize(30); paint.setStrokeWidth(1); //画文字 canvas.drawText(text+"%",x-textWidth/2,y+rextText.height()/2,paint); } } 导入自定义view<com.example.my.demo_zidingyiview.view.jindutiao android:layout_width="match_parent" android:layout_height="match_parent" />
自定义圆形进度条
最新推荐文章于 2022-02-18 10:59:14 发布