自定义圆形进度条

自定义圆形进度条




*/

public class RoundProgress  extends View {
//    //    属性
//    private int roundColor = Color.GRAY;
//    private int roundProgressColor = Color.RED;
//    private int textColor = Color.BLUE;
//
//    private int roundWidth = UIUtils.dp2px(10);// 线的宽度
//    private int textSize = UIUtils.dp2px(20);// 字体的大小
//
//    private int width;
//    private Paint paint;
//
//    //    提供当前进度和最大值
//    private int progress = 60;
//    private int max = 100;



    //    自定义属性的声明
    private int  roundColor;
    private int  roundProgressColor;
    private int  textColor;

    private int  roundWidth; // 线的宽度
    private int  textSize; // 字体的大小

    private int  width;
    private Paint  paint;

    //    提供当前进度和最大值
    private int  progress;
    private int  max;

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

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

    public RoundProgress(Context context, AttributeSet attrs,  int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //1. 获取 typedArray 对象
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable. RoundProgress);
        //2. 获取布局文件中声明的自定义属性的值
        roundColor = typedArray.getColor(R.styleable. RoundProgress_roundColor, Color. GRAY);
        roundProgressColor = typedArray.getColor(R.styleable. RoundProgress_roundProgressColor, Color. RED);
        textColor = typedArray.getColor(R.styleable. RoundProgress_textColor, Color. GREEN);
        roundWidth = ( int) typedArray.getDimension(R.styleable. RoundProgress_roundWidth, UIUtils. dp2px( 10));
        textSize = ( int) typedArray.getDimension(R.styleable. RoundProgress_textSize, UIUtils. dp2px( 20));
        progress = typedArray.getInteger(R.styleable. RoundProgress_progress, 40);
        max = typedArray.getInteger(R.styleable. RoundProgress_max, 100);

        //3. 回收
        typedArray.recycle();
        paint new Paint(); // 初始化画笔
        paint.setAntiAlias( true); // 去除毛边

    }

    public int getRoundColor() {
        return  roundColor;
    }

    public void setRoundColor( int roundColor) {
        this. roundColor = roundColor;
    }

    public int getMax() {
        return  max;
    }

    public void setMax( int max) {
        this. max = max;
    }

    public int getProgress() {
        return  progress;
    }

    public void setProgress( int progress) {
        this. progress = progress;
    }

    @Override
    protected void onMeasure( int widthMeasureSpec,  int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width this.getMeasuredWidth(); // 获取当前视图的宽度
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        绘制圆环
//        设置圆环的中心点
        int dx =  width 2;
        int dy =  width 2;
        //        设置半径
        int radius =  width roundWidth 2;
        paint.setColor( roundColor);
        paint.setStyle(Paint.Style. STROKE); // 设置画笔风格,空心 ( 画一个圆里面是空的 )
        paint.setStrokeWidth( roundWidth); // 设置画笔的宽度,也就是线宽
        canvas.drawCircle(dx, dy, radius,  paint); // 画的圆的中心点,半径,取内圆与外圆的平均值


//        绘制圆弧
//        理解为包裹圆环中心线的圆的矩形
        RectF rectF =  new RectF( roundWidth 2roundWidth 2width roundWidth 2width roundWidth 2);
        paint.setColor( roundProgressColor);
        canvas.drawArc(rectF,  0progress 360 maxfalsepaint);  //false 为不关联中心点

//        绘制文本

        // 设置画笔
        paint.setColor( textColor);
        paint.setTextSize( textSize);
        paint.setStrokeWidth( 0);

        String text =  progress* 100 max "%";
        Rect bound= new Rect(); // 此时包裹文本的矩形框没有宽度和高度
        paint.getTextBounds(text, 0,text.length(),bound); // 使其宽度和高度正好包裹文本内容
//        提供文本区域的左,下
        int left= width/ 2-bound.width()/ 2;
        int bottom= width/ 2+ bound.height()/ 2;


        canvas.drawText(text,left,bottom, paint);
    }
}
布局
< com.example.administrator.p2p.ui.RoundProgress
    android :id= "@+id/roundp_home"
    android :layout_width= "120dp"
    android :layout_height= "120dp"
    android :layout_marginTop= "20dp"
    whq :roundColor= "@color/product_detail_common"
    whq :roundProgressColor= "@color/round_red_common"
    whq :textColor= "@color/title_text"
    whq :roundWidth= "10dp"
    whq :textSize= "20sp"
    whq :max= "100"
   whq:progress="86"/>

values下新建attrs文件
< declare-styleable  name= "RoundProgress">
    < attr  name= "roundColor"  format= "color" />
    < attr  name= "roundProgressColor"  format= "color" />
    < attr  name= "textColor"  format= "color" />
    < attr  name= "roundWidth"  format= "dimension" />
    < attr  name= "textSize"  format= "dimension" />
    < attr  name= "progress"  format= "integer" />
    < attr  name= "max"  format= "integer" />
</ declare-styleable>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值