本文实例为大家分享了android实现简单垂直进度条的具体代码,供大家参考,具体内容如下
代码注释特别清晰,就不多解释了
支持属性:
- progress_radius 进度条圆角大小
- progress_border_enable 进度条是否有边框
- progress_gradient_enable 进度条颜色是否渐变
- progress_start_color 从上到下进度条开始的渐变色
- progress_end_color 从上到下进度条结束的渐变色
- progress_solid_color 带边框进度条的背景填充色
- progress_border_color 进度条边框的颜色
- progress_border_width 进度条边框的宽度
上代码
verticalprogress
public class verticalprogress extends view { //进度条圆角 private int mradius; //进度条是否有边框 private boolean mborderenable; //是否有渐变色 private boolean mgradientenable; //渐变色 private int mstartresid; private int mendresid; //边框的颜色 private int mbordercolorresid; //进度条背景填充色 private int mprogressbgcolorid; //边框宽度 private int mborderwidth; private int mprogress = 10; private int max = 100; private int mwidth; private int mheight; private rectf mrectf; private paint mpaint; public verticalprogress(context context) { super(context); init(context, null); } public verticalprogress(context context, @nullable attributeset attrs) { super(context, attrs); init(context, attrs); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); mwidth = getmeasuredwidth() - 1;// 宽度值 mheight = getmeasuredheight() - 1;// 高度值 } private void init(context context, attributeset attrs) { typedarray typedarray = null; if (attrs != null) { typedarray = context.obtainstyledattributes(attrs, r.styleable.verticalprogress); mradius = typedarray.getint(r.styleable.verticalprogress_progress_radius, 0); mborderenable = typedarray.getboolean(r.styleable.verticalprogress_progress_border_enable, false); mgradientenable = typedarray.getboolean(r.styleable.verticalprogress_progress_gradient_enable, true); mstartresid = typedarray.getresourceid(r.styleable.verticalprogress_progress_start_color, r.color.colorprimary); mprogressbgcolorid = typedarray.getresourceid(r.styleable.verticalprogress_progress_solid_color, r.color.white); mendresid = typedarray.getresourceid(r.styleable.verticalprogress_progress_end_color, r.color.color_4ea6fd); mbordercolorresid = typedarray.getresourceid(r.styleable.verticalprogress_progress_border_color, r.color.color_4ea6fd); mborderwidth = typedarray.getresourceid(r.styleable.verticalprogress_progress_border_width, 10); } if (typedarray != null) { typedarray.recycle(); } mrectf = new rectf(); mpaint = new paint(); mpaint.setantialias(true); } @suppresslint("drawallocation") @override protected void ondraw(canvas canvas) { super.ondraw(canvas); if (mradius == 0) { //弧度为高度的一半 mradius = mwidth / 2; } if (mborderenable) { //第一层矩形(描边层) mrectf.set(0, 0, mwidth, mheight); //第一层矩形颜色(进度条描边的颜色) mpaint.setcolor(getresources().getcolor(mbordercolorresid)); //画第一层圆角矩形 canvas.drawroundrect(mrectf, mradius, mradius, mpaint); //第二层矩形颜色(背景层颜色) mpaint.setcolor(getresources().getcolor(mprogressbgcolorid)); //第二层矩形(背景层) mrectf.set(mborderwidth, mborderwidth, mwidth - mborderwidth, mheight - mborderwidth); //画背景层圆角矩形(盖在描边层之上) canvas.drawroundrect(mrectf, mradius, mradius, mpaint); } if (mprogress == 0)//进度为 0不画进度 return; float section = mprogress / max; //进度层底层 mrectf.set(+8, mheight - mprogress / 100f * mheight + 10, mwidth - 8, mheight - 8); if (mgradientenable) { //渐变器 lineargradient shader = new lineargradient(0, 0, mwidth * section, mheight, getresources().getcolor(mstartresid), getresources().getcolor(mendresid), shader.tilemode.clamp); //第三层矩形颜色(进度渐变色) mpaint.setshader(shader); } //画第三层(进度层)圆角矩形(盖在背景层之上) canvas.drawroundrect(mrectf, mradius, mradius, mpaint); //清除之前传递的shader mpaint.setshader(null); } public void setprogress(int currentcount) { this.mprogress = currentcount > max ? max : currentcount; postinvalidate(); }}attr.xml样式
<declare-styleable name="verticalprogress"> <attr name="progress_radius" format="dimension" /> <attr name="progress_border_width" format="dimension" /> <attr name="progress_gradient_enable" format="boolean" /> <attr name="progress_border_enable" format="boolean" /> <attr name="progress_start_color" format="color" /> <attr name="progress_solid_color" format="color" /> <attr name="progress_end_color" format="color" /> <attr name="progress_border_color" format="boolean" /></declare-styleable>最后调用示例
<com.widget.verticalprogress android:id="@+id/vp_progress" android:layout_width="20dp" android:layout_height="match_parent" app:progress_border_enable="true" app:progress_solid_color="@color/white" android:layout_centerinparent="true" />【Android实现简单实用的垂直进度条】以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com 。
-- 展开阅读全文 --
推荐阅读
- WPF+SkiaSharp实现自绘拖曳小球
- Android画图实现MPAndroidchart折线图示例详解
- Android开发Retrofit源码分析
- OpenCViOS 图像处理编程入门详细教程
- Android实现断点续传功能
- 蜂蛹怎么吃最有营养,蜂子幼崽怎么吃营养最佳
- Android实现多线程断点续传
- Android实现多张图片合成加载动画
- 古诗春风的意思