Android进度条学习
自定义属性
<!-- roundColor 圆环的颜色 roundProgressColor 进度的颜色 roundWidth 圆环的宽度 textColor 文字颜色 textSize 文字大小 max 最大值 textIsDisplayable 是否显示进度文本 style 样式 STROKE 空心 FILL 实心 --> <declare-styleable name="RoundProgressBar"> <attr name="roundColor" format="color"/> <attr name="roundProgressColor" format="color"/> <attr name="roundWidth" format="dimension"></attr> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> <attr name="max" format="integer"></attr> <attr name="textIsDisplayable" format="boolean"></attr> <attr name="style"> <enum name="STROKE" value="0"></enum> <enum name="FILL" value="1"></enum> </attr> </declare-styleable>
public RoundProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mPaint = new Paint(); /** * 获取自定义的属性 */ TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar); //底色 mRoundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED); //进度的颜色 mRoundProgressColor = typedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.BLUE); //圆形的宽 mRoundWidth = typedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 20); //字体颜色 中间 mTextColor = typedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.BLUE); //中间进度显示的字体大小 mTextSize = typedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15); //最大值 mMax = typedArray.getInteger(R.styleable.RoundProgressBar_max, 100); //文字是否显示 mTextIsDisplayable = typedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true); //实心或者 空心 mStyle = typedArray.getInt(R.styleable.RoundProgressBar_style, 0); typedArray.recycle(); }
绘制
//圆心 int centerOfCircle = getWidth() / 2; //radius 半径 int radius = (int) (centerOfCircle - mRoundWidth / 2); //设置画笔 mPaint.setAntiAlias(true); //圆环的颜色 mPaint.setColor(mRoundColor); //设置空心 mPaint.setStyle(Paint.Style.STROKE); //画笔宽度 mPaint.setStrokeWidth(mRoundWidth); //画圆 canvas.drawCircle(centerOfCircle, centerOfCircle, radius, mPaint); /** * 画百分比 */ mPaint.setStrokeWidth(0); //字体大小 mPaint.setTextSize(mTextSize); //画笔颜色 mPaint.setColor(mTextColor); //字体 mPaint.setTypeface(Typeface.DEFAULT_BOLD); //计算百分比 int percent = (int) (((float) mProgress / (float) mMax) * 100); //测量字体的宽度 float textWidth = mPaint.measureText(percent + "%"); //判断是否显示进度文字 不是0,风格是空心的 if (mTextIsDisplayable && percent != 0 && mStyle == STROKE) { canvas.drawText(percent + "%", centerOfCircle - textWidth / 2, centerOfCircle + textWidth / 2, mPaint); } /** * 设置进度 */ mPaint.setColor(mRoundProgressColor); //画笔宽度 mPaint.setStrokeWidth(mRoundWidth); mPaint.setAntiAlias(true); RectF oval = new RectF(centerOfCircle - radius, centerOfCircle - radius, centerOfCircle + radius, centerOfCircle + radius); switch (mStyle) { case STROKE: //空心 mPaint.setStyle(Paint.Style.STROKE); //画圆弧 /** * *开始的角度 */ canvas.drawArc(oval, 180, 360 * mProgress / mMax, false, mPaint); break; case FILL: //实心 mPaint.setStyle(Paint.Style.FILL_AND_STROKE); //画圆弧 if(mProgress!=0) { canvas.drawArc(oval, 180, 360 * mProgress / mMax, true, mPaint); } break; }
源码:
https://github.com/ln0491/ProgressDemo
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决