介绍
使用Android自定义观点实现圆形进度条?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
效果如下:
<强>主要代码强>
CircularProgressView。java
公共类CircularProgressView扩展视图{ 私人油漆mBackPaint mProgPaint;//绘制画笔 私人RectF mRectF;//绘制区域 私人int [] mColorArray;//圆环渐变色 私人int mProgress;//圆环进度(0 - 100)/* * *绘制弧线的画笔 */私人油漆progressPaint;/* * *圆弧圆心位置 */私人int centerX centerY;/* * *圆弧的半径 */私人int circleRadius; 公共CircularProgressView(上下文语境){ 这(上下文,null); } 公共CircularProgressView(上下文语境,@Nullable AttributeSet attrs) { 这(上下文、attrs 0); } 公共CircularProgressView(上下文语境,@Nullable AttributeSet attrs, int defStyleAttr) { 超级(上下文、attrs defStyleAttr); @SuppressLint (“Recycle") TypedArray TypedArray=上下文。obtainStyledAttributes (attrs R.styleable.CircularProgressView);//初始化背景圆环画笔 mBackPaint=new油漆(); mBackPaint.setStyle (Paint.Style.STROKE);//只描边,不填充 mBackPaint.setStrokeCap (Paint.Cap.ROUND);//设置圆角 mBackPaint.setAntiAlias(真正的);//设置抗锯齿 mBackPaint.setDither(真正的);//设置抖动 mBackPaint.setStrokeWidth (typedArray.getDimension (R.styleable。CircularProgressView_backWidth, 5)); mBackPaint.setColor (typedArray.getColor (R.styleable。CircularProgressView_progbgColor Color.LTGRAY));//初始化进度圆环画笔 mProgPaint=new油漆(); mProgPaint.setStyle (Paint.Style.STROKE);//只描边,不填充 mProgPaint.setStrokeCap (Paint.Cap.ROUND);//设置圆角 mProgPaint.setAntiAlias(真正的);//设置抗锯齿 mProgPaint.setDither(真正的);//设置抖动 mProgPaint.setStrokeWidth (typedArray.getDimension (R.styleable。CircularProgressView_progWidth, 10)); mProgPaint.setColor (typedArray.getColor (R.styleable。CircularProgressView_progColor Color.BLUE));//初始化结束位置小圆点 progressPaint=new油漆(); progressPaint.setStyle (Paint.Style.FILL);//填充 progressPaint.setStrokeCap (Paint.Cap.ROUND);//设置圆角 progressPaint.setAntiAlias(真正的);//设置抗锯齿 progressPaint.setDither(真正的);//设置抖动 progressPaint.setStrokeWidth (typedArray.getDimension (R.styleable。CircularProgressView_progWidth, 10)); progressPaint.setColor (Color.WHITE);//初始化进度圆环渐变色 int startColor=typedArray.getColor (R.styleable。CircularProgressView_progStartColor, 1); int firstColor=typedArray.getColor (R.styleable。CircularProgressView_progFirstColor, 1); 如果(startColor !=1,,firstColor !=1) mColorArray=new int [] {startColor, firstColor}; 其他mColorArray=零;//初始化进度 mProgress=typedArray.getInteger (R.styleable。CircularProgressView_progress, 0); typedArray.recycle (); } @Override 保护空白> & lt; declare-styleable name=癈ircularProgressView"比; & lt; attr name=癰ackWidth"格式=癲imension"/比;& lt; !——背景圆环宽度——比; & lt; attr name=皃rogWidth"格式=癲imension"/比;& lt; !——进度圆环宽度——比; & lt; attr name=皃rogbgColor"格式=癱olor"/比;& lt; !——背景圆环颜色——比; & lt; attr name=皃rogColor"格式=癱olor"/比;& lt; !——进度圆环颜色——比; & lt; attr name=皃rogStartColor"格式=癱olor"/比;& lt; !——进度圆环开始颜色——比; & lt; attr name=皃rogFirstColor"格式=癱olor"/比;& lt; !——进度圆环结束颜色——比; & lt; attr name=皃rogress"格式=癷nteger"/比;& lt; !——圆环进度——比; & lt;/declare-styleable>
<强>使用方法
强>
& lt; com.view.CircularProgressView android: layout_width=?70 dp" android: layout_height=?70 dp" android: layout_centerInParent=皌rue" android: layout_centerHorizontal=皌rue" 应用:backWidth=? dp" 应用:progWidth=? dp" 应用:progbgColor=? 4 c5098" 应用:进步=?0”; android: layout_marginTop=?00 dp"/在
关于使用Android自定义观点实现圆形进度条问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。