自定义圆形进度条

自定义圆形进度条

一 自定义view
 
public class CircleProgressView extends View {

    private Paint mPaintBackground; // 背景画笔
    private Paint mPaintProgress; // 进度○
    //    private Paint mPaintText;  // 绘制进度百分比
    private int bgColor = Color.WHITE; // 默认园背景
    //    private int textColor = Color.CYAN; // 文字的画笔颜色 进度百分比
    private int progressColor = Color.CYAN; // 进度背景
    private float mStrokeWidth = 10;// 圆环宽度
    private float mRadius = 60; // 默认半径
    private RectF rectPro;//
    private int mProgress = 0; // 初始进度
    private int mMaxProgress = 100; // 最大进度
    private int mWidth, mHeight;
    private onProgressListener mOnProgressListener;

    public void setOnProgressListener(onProgressListener mOnProgressListener) {
        this.mOnProgressListener = mOnProgressListener;
    }

    /**
     * 进度监听
     */
    public interface onProgressListener {
        public void onEnd();
    }

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

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

    public CircleProgressView(Context context, AttributeSet attrs,
                              int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 自定义xml属性
        // TODO Auto-generated constructor stub
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs,
                    R.styleable.CircleProgress);
            int count = ta.getIndexCount();
            for (int i = 0; i < count; i++) {
                int attr = ta.getIndex(i);
                switch (attr) {
                    case R.styleable.CircleProgress_radius:
                        mRadius = ta.getDimension(R.styleable.CircleProgress_radius, mRadius);
                        break;
                    case R.styleable.CircleProgress_strokeWidth:
                        mStrokeWidth = ta.getDimension(R.styleable.CircleProgress_strokeWidth, mStrokeWidth);
                        break;
                    case R.styleable.CircleProgress_bgColor:
                        bgColor = ta.getColor(R.styleable.CircleProgress_bgColor, bgColor);
                        break;
                    case R.styleable.CircleProgress_progressColor:
                        progressColor = ta.getColor(R.styleable.CircleProgress_progressColor, progressColor);
                        break;
//                    case R.styleable.CircleProgress_android_textColor:
//                        textColor = ta.getColor(R.styleable.CircleProgress_android_textColor, textColor);
//                        break;
                }
            }
            ta.recycle();
        }

        initPaint();
    }

    private void initPaint() {
        mPaintBackground = new Paint();
        mPaintBackground.setColor(bgColor);
        // 抗锯齿
        mPaintBackground.setAntiAlias(true);
        // 防抖动
        mPaintBackground.setDither(true);
        // 画笔样式
        mPaintBackground.setStyle(Style.STROKE);
        // 设置线宽
        mPaintBackground.setStrokeWidth(mStrokeWidth);


        mPaintProgress = new Paint();
        mPaintProgress.setColor(progressColor);
        mPaintProgress.setAntiAlias(true);
        mPaintProgress.setDither(true);
        // 设置画笔帽  画笔圆形
        mPaintProgress.setStrokeCap(Paint.Cap.ROUND);
        mPaintProgress.setStyle(Style.STROKE);
        mPaintProgress.setStrokeWidth(mStrokeWidth);


        // 绘制进度百分比
//        mPaintText = new Paint();
//        mPaintText.setColor(textColor);
//        mPaintText.setAntiAlias(true);
//        mPaintText.setTextAlign(Align.CENTER);
//        mPaintText.setTextSize(40);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        mWidth = getRealSize(widthMeasureSpec);
        mHeight = getRealSize(heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);
    }

    private void initRect() {
        if (rectPro == null) {
            rectPro = new RectF();
            int viewSize = (int) (mRadius * 2);
            int left = (mWidth - viewSize) / 2;
            int top = (mHeight - viewSize) / 2;
            int right = left + viewSize;
            int bottom = top + viewSize;
            rectPro.set(left, top, right, bottom);
        }
    }

    private int getRealSize(int measureSpec) {
        int result = -1;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.UNSPECIFIED) {
            result = (int) (mRadius * 2 + mStrokeWidth * 2);
        } else {
            result = size;
        }
        return result;
    }

    /**
     * 设置更新进度条
     *
     * @param progress
     */
    public void setProgress(int progress) {
        this.mProgress = progress;
        invalidate();
    }

    /**
     * 获取当前进度
     *
     * @return
     */
    public int getProgress() {
        return mProgress;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        float angle = mProgress / (mMaxProgress * 1.0f) * 360;
        initRect();
        //背景圆
        canvas.drawCircle(mWidth / 2, mHeight / 2, mRadius,
                mPaintBackground);
        //画进度
        canvas.drawArc(rectPro, -90, angle, false, mPaintProgress);
        //画进度百分比
//        canvas.drawText(mProgress + "%", mWidth / 2, mHeight / 2, mPaintText);

        // 进度监听
        if (mOnProgressListener != null) {
            if (mProgress == mMaxProgress) {
                mOnProgressListener.onEnd();
            }
        }
    }
}

二 xml属性  values/ attrs
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <attr name="bgColor" format="color|reference" />
    <attr name="progressColor" format="color|reference" />
    <attr name="radius" format="dimension" />
    <attr name="strokeWidth" format="dimension" />

    <declare-styleable name="CircleProgress">
        <attr name="android:textColor" />
        <attr name="bgColor" />
        <attr name="progressColor" />
        <attr name="radius" />
        <attr name="strokeWidth" />
    </declare-styleable>
</resources>

三 在使用时  父亲布局加入
xmlns:app="http://schemas.android.com/apk/res-auto"
引用自定义view的属性
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.xwb.mycircleprogressview.MainActivity">

    <com.example.xwb.mycircleprogressview.CircleProgressView
        android:id="@+id/pro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:bgColor="#FFFFFF"
        app:progressColor="#29D293"
        app:radius = "58dip"
        app:strokeWidth = "10dip"

        />
</android.support.constraint.ConstraintLayout>

四 使用
public class MainActivity extends AppCompatActivity {
    private CircleProgressView progressView;
    private int currentPro = 0;
    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            switch (message.what) {
                case 1:
                    currentPro += 1;
                    progressView.setProgress(currentPro);
                    break;
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressView = (CircleProgressView) findViewById(R.id.pro);
        progressView.setOnProgressListener(new CircleProgressView.onProgressListener() {
            @Override
            public void onEnd() {
                // TODO Auto-generated method stub
                progressView.setProgress(0);
            }
        });

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Message m = new Message();
                    m.what = 1;
                    mHandler.sendMessage(m);
                }
            }
        }).start();
    }
}


源码下载  源码




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值