package com.shunzhi.parent.views; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Paint.Cap; import android.graphics.Paint.Style; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.RectF; import android.graphics.SweepGradient; import android.util.AttributeSet; import android.view.View; /*** * 自定义圆弧进度条 * * @author yxb */ public class ProgressView extends View { //分段颜色 private static final int[] SECTION_COLORS = {Color.RED, Color.YELLOW,Color.GREEN}; private static final String[] ALARM_LEVEL = {"", "", "", ""}; private float maxCount; private float currentCount; private int score; private String crrentLevel; private Paint mPaint; private Paint mTextPaint; private int mWidth, mHeight; public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } public ProgressView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ProgressView(Context context) { this(context, null); } private void init(Context context) { mPaint = new Paint(); mTextPaint = new Paint(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); initPaint(); RectF rectBlackBg = new RectF(20, 20, mWidth - 20, mHeight - 20); canvas.drawArc(rectBlackBg, 0, 270, false, mPaint); mPaint.setColor(getColor(score)); canvas.drawText(score + "%", mWidth / 2, mHeight / 2, mTextPaint); mTextPaint.setTextSize(40); SweepGradient sweepGradient = new SweepGradient(mWidth / 2, mWidth / 2, SECTION_COLORS, null); Matrix matrix = new Matrix(); matrix.setRotate(120, this.mWidth / 2, mWidth / 2); sweepGradient.setLocalMatrix(matrix); float section = currentCount / maxCount; mPaint.setShader(sweepGradient); mPaint.setShader(sweepGradient); mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP)); mPaint.setStyle(Style.STROKE); canvas.drawArc(rectBlackBg, 135, section * 270, false, mPaint); } private void initPaint() { mPaint.setAntiAlias(true); mPaint.setStrokeWidth((float) 40.0); mPaint.setStyle(Style.STROKE); mPaint.setStrokeCap(Cap.ROUND); mPaint.setColor(Color.TRANSPARENT); mTextPaint.setAntiAlias(true); mTextPaint.setStrokeWidth((float) 3.0); mTextPaint.setTextAlign(Paint.Align.CENTER); mTextPaint.setTextSize(50); mTextPaint.setColor(Color.BLACK); } private int dipToPx(int dip) { float scale = getContext().getResources().getDisplayMetrics().density; return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1)); } public int getScore() { return score; } public String getCrrentLevel() { return crrentLevel; } public void setCrrentLevel(String crrentLevel) { this.crrentLevel = crrentLevel; } public float getMaxCount() { return maxCount; } public float getCurrentCount() { return currentCount; } public void setScore(int score) { this.score = score; if (score == 100) { this.crrentLevel = ALARM_LEVEL[0]; } else if (score >= 70 && score < 100) { this.crrentLevel = ALARM_LEVEL[1]; } else if (score >= 30 && score < 70) { this.crrentLevel = ALARM_LEVEL[2]; } else { this.crrentLevel = ALARM_LEVEL[3]; } invalidate(); } public int getColor(int score) { this.score = score; if (score > 60 && score <= 100) { return SECTION_COLORS[2]; } else if (score >= 30 && score <= 60) { return SECTION_COLORS[1]; } else { return SECTION_COLORS[0]; } } /*** * 设置最大的进度值 * * @param maxCount */ public void setMaxCount(float maxCount) { this.maxCount = maxCount; } /*** * 设置当前的进度值 * * @param currentCount */ public void setCurrentCount(float currentCount) { this.currentCount = currentCount > maxCount ? maxCount : currentCount; invalidate(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); if (widthSpecMode == MeasureSpec.EXACTLY || widthSpecMode == MeasureSpec.AT_MOST) { mWidth = widthSpecSize; } else { mWidth = 0; } if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UNSPECIFIED) { mHeight = dipToPx(15); } else { mHeight = heightSpecSize; } setMeasuredDimension(mWidth, mHeight); } }