扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章给大家介绍怎么在Android应用中添加一个圆形进度条效果,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
创新互联-云计算及IDC服务提供商,涵盖公有云、IDC机房租用、雅安服务器托管、等保安全、私有云建设等企业级互联网基础服务,联系电话:18982081108
首先我们在attrs属性文件中增加几个自定义属性
<?xml version="1.0" encoding="utf-8"?>
上我们自定义类的实现代码:
package xxx.xxx.xxx; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Typeface; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; import test.dn.com.dn_test.R; /** * Created by Administrator on 2017/5/16 0016. */ public class CircleProgressBar extends View { private int max; //最大值 private int roundColor; //圆形进度条的颜色 private int roundProgressColor;//圆形进度条进度的颜色 private int textColor; //字体的颜色 private float textSize; //字体的大小 private float roundWidth; //圆的宽度 private boolean textShow; //是否显示圆 private int progress; //当前进度 private Paint mPaint; //画笔 public static final int STROKE = 0; public static final int FILL = 1; public CircleProgressBar(Context context, @Nullable AttributeSet attrs) { super(context, attrs); //初始化一只笔 mPaint = new Paint(); //获取xml当中设置的属性,如果没有设置,则设置一个默认值 TypedArray typedArray = context.obtainStyledAttributes(attrs , R.styleable.CustomProgressBar); max = typedArray.getInteger(R.styleable.CustomProgressBar_max , 100); roundColor = typedArray.getColor(R.styleable.CustomProgressBar_roundColor, Color.RED); roundProgressColor = typedArray.getColor(R.styleable.CustomProgressBar_roundProgressColor , Color.BLUE); textColor = typedArray.getColor(R.styleable.CustomProgressBar_textColor , Color.GREEN); textSize = typedArray.getDimension(R.styleable.CustomProgressBar_textSize , 55); roundWidth = typedArray.getDimension(R.styleable.CustomProgressBar_roundWidth , 10); textShow = typedArray.getBoolean(R.styleable.CustomProgressBar_textShow , true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //画背景圆环 int center = getWidth() / 2; //设置半径 float radius = center - roundWidth / 2; //设置圆圈的颜色 mPaint.setColor(roundColor); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(roundWidth);//圆环的宽度 mPaint.setAntiAlias(true);//设置抗锯齿 //画外圈 canvas.drawCircle(center , center ,radius , mPaint); //画进度百分比 mPaint.setColor(textColor); mPaint.setStrokeWidth(0); //设置字体大小 mPaint.setTextSize(textSize); mPaint.setTypeface(Typeface.DEFAULT); //设置笔帽 mPaint.setStrokeCap(Paint.Cap.ROUND); //设置文字的摆放方式为居中 mPaint.setTextAlign(Paint.Align.CENTER); //获取当前进度的值 int percent = (int) (progress / (float)max * 100); String strPercent = percent + "%"; //获取画笔的文字属性,总共有bottom , top , leading , ascent , descent 这个以后会详细讲解 Paint.FontMetricsInt fm = mPaint.getFontMetricsInt(); if(percent != 0){ canvas.drawText(strPercent , getWidth() / 2 , getWidth() / 2 + (fm.bottom - fm.top) / 2 - fm.bottom, mPaint); } //画圆弧 RectF oval = new RectF(center - radius , center - radius ,center + radius , center + radius); mPaint.setColor(roundProgressColor); mPaint.setStrokeWidth(roundWidth); mPaint.setStyle(Paint.Style.STROKE); //设置笔帽 mPaint.setStrokeCap(Paint.Cap.ROUND); //话进度 canvas.drawArc(oval , 0 , 360 * progress / max , false , mPaint); } public void setProgress(int progress){ if(progress < 0){ throw new IllegalArgumentException("进度progress不能小于0"); } if(progress > max){ progress = max; } if(progress <= max){ this.progress = progress; postInvalidate(); } } }
在我们的xml中设置控件:
Activity功能实现代码:
mProgressBar = (CircleProgressBar) findViewById(R.id.progressbar); mProgressBar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //模拟http请求 new Thread(new Runnable() { @Override public void run() { while (progress <= 100){ progress += 2; mProgressBar.setProgress(progress); //模拟网络请求,每隔100毫秒增加一个进度 SystemClock.sleep(100); } } }).start(); } });
关于怎么在Android应用中添加一个圆形进度条效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流