Android-自定义ProgressBar实现圆弧进度条

继承于ProgressBar实现,保留了Progressbar的特性,源码在文尾。。

《Android-自定义ProgressBar实现圆弧进度条》 demo.gif

参数

nameformatdescription
borderWidthinteger圆弧边框的宽度
progressStyletick/arc进度条类型,tick为带刻度的
radiusinteger半径
arcbgColorcolor圆弧的边框背景
degreeinteger弧度,设置为0即为圆形进度条,180为半圆
tickWidthinteger刻度的宽度
tickDensityinteger刻度的密度 2~8 越小越密
bgShowboolean是否显示圆弧边框背景
arcCapRoundboolean圆弧的笔触是否为圆形,tick无效

interface

提供了绘制圆弧中间区域的一个接口 ,可根据自己的需求自由绘制

/**
   * 
   * @param canvas  
   * @param rectF  圆弧的Rect
   * @param x      圆弧的中心x
   * @param y      圆弧的中心y
   * @param storkeWidth   圆弧的边框宽度
   * @param progress      当前进度
   */
public interface  OnCenterDraw {
  public  void draw(Canvas canvas, RectF rectF, float x, float y,float storkeWidth,int progress);
}

默认提供了两个实现
onImageCenter and OnTextCenter

Use

    <com.czp.library.ArcProgress
        android:id="@+id/myProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:degree="0"
        app:progressStyle="arc" />

    <com.czp.library.ArcProgress
        android:id="@+id/myProgress01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/myProgress"
        app:radius="80dp"
        app:progressColor="@color/progressColor"
        app:tickDensity="3" />
    <com.czp.library.ArcProgress
        android:id="@+id/myProgress02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/myProgress"
        app:radius="90dp"
        app:arcCapRound="true"
        app:degree="180"
        app:progressStyle="arc"
        app:progressColor="@color/progressColorBlue"
        app:tickDensity="3" />
 mProgress.setOnCenterDraw(new ArcProgress.OnCenterDraw() {
            @Override
            public void draw(Canvas canvas, RectF rectF, float x, float y, float storkeWidth,int progress) {
                Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
                textPaint.setStrokeWidth(35);
                textPaint.setColor(getResources().getColor(R.color.textColor));
                String progressStr = String.valueOf(progress+"%");
                float textX = x-(textPaint.measureText(progressStr)/2);
                float textY = y-((textPaint.descent()+textPaint.ascent())/2);
                canvas.drawText(progressStr,textX,textY,textPaint);
            }
        });

依赖

dependencies {
  compile 'com.czp.arcProgressBar:ArcProgressBar:1.0.1'
}

写完搜了一下。。。貌似重复造了个轮子。。。
后续补上几个知识点~哇哈啊哈啊

源码下载地址
喜欢的不妨来个Star呀~

    原文作者:车简
    原文地址: https://www.jianshu.com/p/c9f4dcc2e0f3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞