自定义圆环进度条-百分比进度

1.style文件

<attr name="originColor" format="color"/>
<attr name="setColor" format="color"/>
<attr name="textViewColor" format="color"/>
<attr name="TextViewSize" format="dimension"/>
<attr name="circleWidth" format="dimension"/>
<declare-styleable name="CircleProgress">
    <attr name="originColor"/>
    <attr name="setColor"/>
    <attr name="textViewColor"/>
    <attr name="TextViewSize"/>
    <attr name="circleWidth"/>
</declare-styleable>

2.自定义circleProgress

public class CircleProgress extends View {

    private Paint mPaint;
    private int mRadius; // 圆环半径
    private TextPaint mTextPaint;
    private int strokeWidth = 20; //圆环圈的宽度
    private int mProgress;
    private int mPercent;
    private int mOriginColor;
    private int mSetColor;
    private int mTextViewColor;
    private int mTextViewSize;
    private int mCount = 0;

    public CircleProgress(Context context) {
        this(context, null);
    }

    public CircleProgress(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleProgress(Context context, final AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CircleProgress, defStyleAttr, 0);
        int indexCount = a.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.CircleProgress_circleWidth:
                    strokeWidth = (int) a.getDimension(attr, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics()));
                    break;
                case R.styleable.CircleProgress_originColor:
                    mOriginColor = a.getColor(attr, Color.GRAY);
                    break;
                case R.styleable.CircleProgress_setColor:
                    mSetColor = a.getColor(attr, Color.RED);
                    break;
                case R.styleable.CircleProgress_textViewColor:
                    mTextViewColor = a.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.CircleProgress_TextViewSize:
                    mTextViewSize = (int) a.getDimension(attr, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics()));
                    break;
            }
        }
        a.recycle();
        init();
        start();
    }

    private void start() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (mCount != 361) {
                    startDraw(mCount);
                    mCount++;
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    private void init() {
        //画数字
        mTextPaint = new TextPaint();
        mTextPaint.setColor(mTextViewColor);
        mTextPaint.setTextSize(mTextViewSize);
        mTextPaint.setAntiAlias(true);
        //画图
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(strokeWidth);
    }

    public void startDraw(int progress) {
        mProgress = progress;
        mPercent = (int) (mProgress / 3.6);
        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth() / 2;
        mRadius = width - strokeWidth / 2;
        RectF rectF = new RectF(width - mRadius, width - mRadius, width + mRadius, width + mRadius);
        mPaint.setColor(mOriginColor);
        canvas.drawCircle(width, width, mRadius, mPaint);
        mPaint.setColor(mSetColor);
        canvas.drawText(mPercent + "%", width - strokeWidth * 2, width + strokeWidth, mTextPaint);
        canvas.drawArc(rectF, -90, mProgress, false, mPaint);

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
圆环进度条是一种常见的 UI 设计元素,用于表示任务或进程的完成度。它通常由一个圆环和一个指示器组成,指示器会沿着圆环逐渐移动,表示进度的完成情况。 实现圆环进度条的方法有很多种,其中比较常见的是通过 CSS3 的 transform 属性来实现。具体来说,可以通过设置圆环的 border 属性和 border-radius 属性来定义圆环的形状,并通过设置 border-color 属性和 transform 属性来控制指示器的位置和角度。 以下是一个简单的示例代码,实现了一个 50% 完成度的圆环进度条: ```html <div class="progress-bar"> <div class="progress-bar__inner"></div> </div> ``` ```css .progress-bar { width: 100px; height: 100px; border: 10px solid #ccc; border-radius: 50%; position: relative; } .progress-bar__inner { width: 100%; height: 100%; border: 10px solid #38b6ff; border-radius: 50%; position: absolute; top: 0; left: 0; transform: rotate(45deg); border-right-color: transparent; border-top-color: transparent; } ``` 在上面的代码中,`.progress-bar` 是圆环进度条的外层容器,`.progress-bar__inner` 是进度指示器。通过设置 `.progress-bar__inner` 的 `transform: rotate(45deg)` 属性,可以让指示器从圆环的正上方开始。 当需要实现动态的圆环进度条时,可以通过 JavaScript 来动态修改 `.progress-bar__inner` 的 `transform` 属性,从而让指示器沿着圆环逐渐移动,表示进度的完成情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值