自定义圆形进度条

布局xml

                                                                                                

<!-- 自定义圆形进度条 -->


        <com.mypackage.CircleProgressBarView
            android:id="@+id/circle_progressbar"
            android:layout_width="wrap_content"
            android:layout_height="130dp" />


自定义进度条控件:

public class CircleProgressBarView extends View {
	
	private int progress;
	private int max;
	private Paint paint;
	private RectF oval;
	
	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;
		invalidate();
	}

	public CircleProgressBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();
		oval = new RectF();
	}
	


	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		paint.setAntiAlias(true);   //设置是否抗锯齿、
		paint.setFlags(Paint.ANTI_ALIAS_FLAG);  //消除锯齿
		paint.setColor(Color.GRAY);
		paint.setStrokeWidth(10);  //设置画笔宽度
		paint.setStyle(Paint.Style.STROKE);   //设置中空的样式
		//在中心为(100,100)的地方画个半径为50的圆,宽度为setStrokeWidth:10,也就是灰色的底边
		canvas.drawCircle(100, 100, 50, paint);    
		paint.setColor(getResources().getColor(R.color.green)); //不用系统的颜色 
		// set(45, 45, 155, 155)设置类似于左上角坐标(45,45),右下角坐标(155,155),这样也就保证了半径为55
		oval.set(50, 50, 150, 150);  
	    //画圆弧,第二个参数为:起始角度,第三个为跨的角度,第四个为true的时候是实心,false的时候为空心
		canvas.drawArc(oval, -90, ((float)progress / max) * 360, false, paint);
		paint.reset();    //将画笔重置
		paint.setStrokeWidth(3);        //再次设置画笔的宽度
		paint.setTextSize(30);          //设置文字的大小
		paint.setColor(Color.CYAN);
		if(progress == max){
			canvas.drawText("完成", 70, 110, paint);
		}else{
			canvas.drawText(progress + "%", 70, 110, paint);
		}
	}

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值