Android控件之圆形进度条

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

在之前的项目中用到过这个,感觉还是非常实用的,我实现的是额度的增长.

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

205088-6555c804b693507c.gif

参数

name format description
borderWidth integer 圆弧边框的宽度
progressStyle tick/arc 进度条类型,tick为带刻度的
radius integer 半径
arcbgColor color 圆弧的边框背景
degree integer 弧度,设置为0即为圆形进度条,180为半圆
tickWidth integer 刻度的宽度
tickDensity integer 刻度的密度  2~8 越小越密
bgShow boolean 是否显示圆弧边框背景
arcCapRound boolean 圆弧的笔触是否为圆形,tick无效

interface

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

1
2
3
4
5
6
7
8
9
10
11
12
/**
   
    * @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

    
    
    
1
2
3
4
5
6
7
8
9
  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'
}


源码下载地址


猜你喜欢

转载自blog.csdn.net/qq_21153627/article/details/70156669