Android控件之圆形进度条

    xiaoxiao2021-03-31  34

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

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

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

    参数

    nameformatdescriptionborderWidthinteger圆弧边框的宽度progressStyletick/arc进度条类型,tick为带刻度的radiusinteger半径arcbgColorcolor圆弧的边框背景degreeinteger弧度,设置为0即为圆形进度条,180为半圆tickWidthinteger刻度的宽度tickDensityinteger刻度的密度  2~8 越小越密bgShowboolean是否显示圆弧边框背景arcCapRoundboolean圆弧的笔触是否为圆形,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' }

    源码下载地址

    转载请注明原文地址: https://ju.6miu.com/read-665434.html

    最新回复(0)