老规矩 先看效果图
public class ProgressBarView extends View { private int barBackgroundColor= Color.BLACK; private int progressColor= Color.GREEN; private int bartextColor =Color.BLACK; private int barWidth =20; // 进度条宽度 private String bartextStr ; // 显示文本 private int bartextSize = 20 ; public ProgressBarView(Context context) { this(context,null); } public ProgressBarView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public ProgressBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ProgressBarView); barBackgroundColor = array.getColor( R.styleable.ProgressBarView_barBackgroundColor,barBackgroundColor); progressColor = array.getColor( R.styleable.ProgressBarView_progressColor,progressColor); bartextColor = array.getColor( R.styleable.ProgressBarView_bartextColor,bartextColor); bartextSize = array.getDimensionPixelSize( R.styleable.ProgressBarView_bartextSize,bartextSize); barWidth = (int) array.getDimension(R.styleable.ProgressBarView_barWidth,barWidth); bartextStr = array.getString(R.styleable.ProgressBarView_bartextStr); array.recycle(); initPaint(); initTextPaint(); initProgressPaint(); } private Paint mPaint,mTextPaint,mProgressPaint ; private void initPaint(){ mPaint = new Paint(); mPaint.setColor(barBackgroundColor); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(barWidth); mPaint.setStyle(Paint.Style.STROKE); } private void initProgressPaint(){ mProgressPaint = new Paint(); mProgressPaint.setColor(progressColor); mProgressPaint.setAntiAlias(true); mProgressPaint.setStrokeWidth(barWidth); mProgressPaint.setStyle(Paint.Style.STROKE); } private void initTextPaint(){ mTextPaint = new Paint(); mTextPaint.setColor(bartextColor); mTextPaint.setAntiAlias(true); mTextPaint.setTextSize(bartextSize); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); /*8 去除最小值 */ setMeasuredDimension(Math.min(width,height),Math.min(width,height)); } private int totalNumber =100; private float number=0 ; public void setNumber(float number) { this.number = number; invalidate(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // float left, float top, float right, float bottom RectF rectF = new RectF(barWidth,barWidth,getWidth()-barWidth,getHeight()-barWidth) ; // drawCircle 都可以 canvas.drawArc(rectF,180,360,false,mPaint); if(totalNumber!=0){ float sy = number/totalNumber; canvas.drawArc(rectF,180,sy*360,false,mProgressPaint); } /** * 画中心的文本 就是进度 */ bartextStr = (int)((number/totalNumber)*100)+"%"; Rect boundsRect = new Rect(); mTextPaint.getTextBounds(bartextStr,0,bartextStr.length(),boundsRect); // 获取基线位置 Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt(); int baseLine = getHeight()/2+ (fontMetricsInt.bottom-fontMetricsInt.top)/2-fontMetricsInt.descent; canvas.drawText(bartextStr,getWidth()/2-boundsRect.width()/2,baseLine,mTextPaint); } }
需要自定义属性文件 attrs
<declare-styleable name="ProgressBarView"> <attr name="barBackgroundColor" format="color"/> <attr name="progressColor" format="color"/> <attr name="bartextColor" format="color"/> <attr name="barWidth" format="dimension"/> <attr name="bartextSize" format="dimension"/> <attr name="bartextStr" format="string"/> </declare-styleable>