package cn.flightfeather.thirdapp.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; /** * Created by 王xin on 2017/4/5. */ public class CircleProgressView extends View { private static final String TAG = "CircleProgressView"; private int mMaxProgress = 100; private final int mCircleLineStrokeWidth = 5; private final int mTxtStrokeWidth = 2; private int mCurrentPercent = 0; private float mCurrentAngle = 0; // 画圆所在的距形区域 private final RectF mRectF; private final Paint mPaint; private final Paint mPaint2; private int stepCount = 0; private int stepGoal = 0; private int mProgress = 90; private int r, g, b; private int totalTask; private int finishTask; public CircleProgressView(Context context, AttributeSet attrs) { super(context, attrs); mRectF = new RectF(); mPaint = new Paint(); mPaint2 = new Paint(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = this.getWidth(); int height = this.getHeight(); if (width != height) { int min = Math.min(width, height); width = min; height = min; } // 设置画笔相关属性 mPaint.setAntiAlias(true); //79,202,245 mPaint.setColor(Color.rgb(239, 239, 239)); canvas.drawColor(Color.TRANSPARENT); mPaint.setStrokeWidth(mCircleLineStrokeWidth); mPaint.setStyle(Paint.Style.STROKE); // 位置 mRectF.left = mCircleLineStrokeWidth / 2; // 左上角x mRectF.top = mCircleLineStrokeWidth / 2; // 左上角y mRectF.right = width - mCircleLineStrokeWidth / 2; // 左下角x mRectF.bottom = height - mCircleLineStrokeWidth / 2; // 右下角y // 绘制圆圈,进度条背景 canvas.drawArc(mRectF, -90, 360, false, mPaint); mPaint.setColor(Color.rgb(r, g, b)); canvas.drawArc(mRectF, -90, mCurrentAngle, false, mPaint); if (mCurrentPercent < mProgress) { //当前百分比+1 mCurrentPercent += 1; //当前角度+3.6 mCurrentAngle += 3.6; //每10ms重画一次 postInvalidateDelayed(5); } else if (mCurrentPercent == mProgress) { } // 绘制进度文案显示 mPaint.setStrokeWidth(mTxtStrokeWidth); int stepNumberHeight = height / 4; mPaint.setTextSize(stepNumberHeight); mPaint.setStyle(Paint.Style.FILL); //字符位置 mPaint2.setStrokeWidth(mTxtStrokeWidth); int stepTextHeight = height / 12; mPaint2.setTextSize(stepTextHeight); int stepTextWidth = (int) mPaint2.measureText("000000000000000", 0, "000000000000000".length()); int stepNumberY = height / 2 + stepNumberHeight / 3; int stepNumberX = (int) (width / 2 - (2 + stepTextWidth) / 2.4); mPaint.setColor(Color.GRAY); canvas.drawText(finishTask + "/" + totalTask, stepNumberX, stepNumberY, mPaint); } public int getProgress() { return mProgress; } public void setProgress(int progress, int totalTask) { this.finishTask = progress; if (totalTask ==0){ this.mProgress =0; }else { int newProgress = (int) (((double) progress / (double) totalTask) * 100); this.mProgress = newProgress; } this.totalTask = totalTask; this.invalidate(); } public void setProgressNotInUiThread(int progress) { this.mProgress = progress; this.postInvalidate(); } public int getStepCount() { return stepCount; } public void setStepCount(int stepCount) { this.stepCount = stepCount; this.postInvalidate(); } public int getStepGoal() { return stepGoal; } public void setRGB(int r, int g, int b) { this.r = r; this.g = g; this.b = b; } public void setStepGoal(int stepGoal) { this.stepGoal = stepGoal; this.postInvalidate(); } }