https://github.com/jakob-grabner/Circle-Progress-View

来源:互联网 发布:中建七局网络教育 编辑:程序博客网 时间:2024/06/04 20:09

这个库里 Sweep 渐变和 Cap.ROUND 的冲突也没解决掉.

drawArc 的 bug 也没解决掉。

1800 行的代码,慢慢看。


public void setBarColor(@ColorInt int... barColors) {
用可变参数作为参数,单一颜色或渐变颜色,用一个接口就搞定了。


 public void setBlockCount(int blockCount) {        if (blockCount > 1) {            mShowBlock = true;            mBlockCount = blockCount;            mBlockDegree = 360.0f / blockCount;            mBlockScaleDegree = mBlockDegree * mBlockScale;        } else {            mShowBlock = false;        }    }    private void drawBlocks(Canvas _canvas, RectF circleBounds, float startAngle, float _degrees, boolean userCenter, Paint paint) {        float tmpDegree = 0.0f;        while (tmpDegree < _degrees) {            _canvas.drawArc(circleBounds, startAngle + tmpDegree, Math.min(mBlockScaleDegree, _degrees - tmpDegree), userCenter, paint);            tmpDegree += mBlockDegree;        }    }
类似 DashEffect 的东西。


  private void drawBar(Canvas _canvas, float _degrees) {        float startAngle = mDirection == Direction.CW ? mStartAngle : mStartAngle - _degrees;            _canvas.drawArc(mCircleBounds, startAngle, _degrees, false, mBarPaint);    }
让进度条逆时针增加,反正我是没看懂,好神奇。


public class CircleProgress extends View {    // ====================== CircleProgress ======================    public CircleProgress(Context context, AttributeSet attrs) {        super(context, attrs);        initPaint();    }    // ====================== paints ======================    private final Paint mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);    private final Paint mProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);    private int bgColor = Color.LTGRAY;    private int progressColor = Color.BLUE;    // ====================== stroke ======================    private final RectF mStrokeBounds = new RectF();    private int mStrokeWidth = 30;    // ====================== angles ======================    private float mProgress = 80f;    private float mStartAngle = 0f;    private float mArcAngle = 360f;    // ====================== direction ======================    public static final int CW = 0;    public static final int CCW = 1;    private int mDirection = CW;    // ====================== initPaint ======================    private void initPaint() {        mBgPaint.setColor(bgColor);        mBgPaint.setStyle(Paint.Style.STROKE);        mBgPaint.setStrokeWidth(mStrokeWidth);        mBgPaint.setStrokeCap(Paint.Cap.ROUND);        mProgressPaint.setColor(progressColor);        mProgressPaint.setStyle(Paint.Style.STROKE);        mProgressPaint.setStrokeWidth(mStrokeWidth);        mProgressPaint.setStrokeCap(Paint.Cap.ROUND);    }    // ====================== onSizeChanged ======================    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        updateStrokeBounds();    }    private void updateStrokeBounds() {        final int delta = mStrokeWidth / 2;        mStrokeBounds.set(delta, delta, getWidth() - delta, getHeight() - delta);    }    // ====================== onDraw ======================    @Override    protected void onDraw(Canvas canvas) {        drawBg(canvas);        drawProgress(canvas);    }    private void drawBg(Canvas canvas) {        final float sweepAngle = mArcAngle;        float startAngle = getStartAngle(sweepAngle);        drawArcPath(canvas, startAngle, mArcAngle, mBgPaint);    }    private void drawProgress(Canvas canvas) {        float sweepAngle = mArcAngle * (mProgress / 100);        float startAngle = getStartAngle(sweepAngle);        drawArcPath(canvas, startAngle, sweepAngle, mProgressPaint);    }    private void drawArcPath(Canvas canvas, final float startAngle, final float sweepAngle, Paint paint) {        Path path = new Path();        path.addArc(mStrokeBounds, startAngle, sweepAngle);        canvas.drawPath(path, paint);    }    // 反向画圆    private float getStartAngle(final float sweepAngle) {        return mDirection == CW ? mStartAngle : mStartAngle - sweepAngle;    }    // ====================== setter ======================    public void setStartAngle(final float startAngle) {        mStartAngle = startAngle;        invalidate();    }    public void setArcAngle(final float arcAngle) {        mArcAngle = arcAngle;        invalidate();    }    public void setProgress(final float progress) {        mProgress = progress;        invalidate();    }    public void setDirection(final int direction) {        mDirection = direction;        invalidate();    }    public void setStrokeWidth(final int strokeWidth) {        mStrokeWidth = strokeWidth;        onStrokeWidthChange();    }    private void onStrokeWidthChange() {        updateStrokeBounds();        mBgPaint.setStrokeWidth(mStrokeWidth);        mProgressPaint.setStrokeWidth(mStrokeWidth);        invalidate();    }    }
把代码又重构了下,看 issue 去。




0 0
原创粉丝点击