/** * Created by Cwm on 2018/3/6. * 圆形进度条 */ public class Progress extends View { private int mExcircleColor = Color.RED; private int mFilletColor = Color.BLUE; private int mBreadth = 20;//20px private int mFontColor = Color.RED; private int mFontSize = 40;//20px //分别是外圆弧、内圆弧、字体画笔 private Paint mExcirclePaint, mFilletPaint, mTextPaint; private String text = "0%"; private float mProgress=0.0f; public Progress(Context context) { this(context, null); } public Progress(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public Progress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Progress); mExcircleColor = typedArray.getColor(R.styleable.Progress_Progress_excircleColor, mExcircleColor); mFilletColor = typedArray.getColor(R.styleable.Progress_Progress_filletColor, mFilletColor); mFontColor = typedArray.getColor(R.styleable.Progress_Progress_fontColor, mFontColor); mBreadth = typedArray.getDimensionPixelSize(R.styleable.Progress_Progress_breadth, mBreadth); mFontSize = typedArray.getDimensionPixelSize(R.styleable.Progress_Progress_fontSize, mFontSize); typedArray.recycle(); mExcirclePaint = initPaint(mExcircleColor, mBreadth, false); mFilletPaint = initPaint(mFilletColor, mBreadth, false); mTextPaint = initPaint(mFontColor, mFontSize, true); } //初始化画笔 private Paint initPaint(int color, int paintSize, boolean flag) { Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(color); if (flag) { paint.setTextSize(paintSize); } else { paint.setStrokeWidth(paintSize); } paint.setStyle(Paint.Style.STROKE); return paint; } //重新测量设置View宽高 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //获取宽高 int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); //获取模式 int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); //在测量宽高时判断模式是否为AT_MOST if (widthMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.AT_MOST) { throw new UnsupportedOperationException("Width and height must specify specific values"); } //在测量宽高时判断是否大于0 if (widthSize <= 0 || heightSize <= 0) { throw new UnsupportedOperationException("Width and height must be greater than 0"); } //当宽和高有具体值且大于0,但是宽和高的值不一样时,按照高度走,必须保持一个正方形 //即: setMeasuredDimension(widthSize > heightSize ? heightSize : widthSize , widthSize > heightSize ? heightSize : widthSize); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); RectF rectF = new RectF(mBreadth, mBreadth, getWidth() - mBreadth, getHeight() - mBreadth); canvas.drawArc(rectF, 0, 360, false, mExcirclePaint); canvas.drawArc(rectF, 0, 360*(mProgress/100), false, mFilletPaint); Rect rect = new Rect(); mTextPaint.getTextBounds(text, 0, text.length(), rect); int dx = getWidth() / 2 - rect.width() / 2; Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt(); int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom; int baseLine = getHeight() / 2 + dy; canvas.drawText(text, dx, baseLine, mTextPaint); } public void setProgress(float progress) { this.mProgress = progress; invalidate(); } public void setText(String text) { this.text = text; }
圆形进度条
最新推荐文章于 2024-02-10 10:35:41 发布