史上难求的自定义progress环形进度条效果——自定义View来袭!!!

还是老规矩线上效果图:

史上难求的自定义progress环形进度条效果——自定义View来袭!!!


解析:这个效果其实就是两个画笔,画了两个弧而已,一个画笔为背景色,一个画笔为进度色。

画弧的方法是:canvas.drawArc(bounds, 135,270, false, paint);  下面我给大家介绍一下这4个参数!

参数2:画圆的起始角度,默认的起始角度是,水平位置的右侧为0度,所以本次的其实角度0+135,本图的坐下的位置

参数3,画圆的终止调度,当然你可以写成360,那就一个圆形了! 由于需求,我的末角度是270.

参数4:这个参数改为true后,会有一条线只想圆心,具体效果可以自己试下看,一般很少那样做

参数5:毋庸置疑那肯定就是画笔了,可以设置以写颜色等这样的属性。

参数1:这个参数是最关键的,所以放在最后来说, 

             API:对它的解释 用来定义形状和尺寸范围,直接上图看吧,

史上难求的自定义progress环形进度条效果——自定义View来袭!!!



以下是核心代码源码:

    /**
     * 得到画布的宽高
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = this.getMeasuredWidth();//得到画布的宽
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置Paint的属性
        paint.setColor(roundColor);
        paint.setStrokeWidth(roundWidth);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);
        //1.绘制圆弧背景
        RectF bounds = new RectF(roundWidth / 2, roundWidth / 2, width - roundWidth / 2, width - roundWidth / 2);
        canvas.drawArc(bounds, 135,270, false, paint);
        //2.绘制圆弧进度
        paint.setColor(roundProgressColor);
        canvas.drawArc(bounds, 135, progress * 270 / max, false, paint); //这是是根据百分比来设置进度的

}


然后再XML中如用,只需要引入我们自定义的View就oK:

 <com.example.iuzuan.myapplication.RoundProgress
        android:layout_centerInParent="true"
        android:layout_width="120dp"
        android:layout_height="120dp" />


ok,可以使用了!