Android progressbar实现带底部指示器和文字的进度条
程序员文章站
2022-04-12 11:45:41
本文实例为大家分享了android实现带指示器和文字的进度条,供大家参考,具体内容如下
根据项目要求需要实现以下效果:
列出源码:
public clas...
本文实例为大家分享了android实现带指示器和文字的进度条,供大家参考,具体内容如下
根据项目要求需要实现以下效果:
列出源码:
public class textprogressbar extends linearlayout { string text; paint mpaint; private rect textrect; private bitmap bitmap; private progressbar progressbar; int progress; int prowidth, proheight; public textprogressbar(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); inittext(context); } public textprogressbar(context context) { super(context); inittext(context); } public textprogressbar(context context, attributeset attrs) { super(context, attrs); inittext(context); } @override protected synchronized void ondraw(canvas canvas) { super.ondraw(canvas); progress = progressbar.getprogress(); if (progress == 0 || progress == progressbar.getmax()) return; this.mpaint.gettextbounds(this.text, 0, this.text.length(), textrect); prowidth = progressbar.getwidth(); proheight = progressbar.getheight(); //画指示器 int bitmapx = (int) (progressbar.getleft() + prowidth * ((progress * 1f) / progressbar.getmax())) - bitmap.getwidth() / 2; int bitmapy = proheight + bitmap.getheight() / 2; if (bitmapx < 0) bitmapx = progressbar.getleft(); if(bitmapx > progressbar.getright()-bitmap.getwidth()) bitmapx = progressbar.getright()-bitmap.getwidth(); canvas.drawbitmap(bitmap, bitmapx, bitmapy, mpaint); //写字 int tvx = (int) (progressbar.getleft() + prowidth * ((progress * 1f) / progressbar.getmax())) - textrect.centerx(); int tvy = proheight + bitmap.getheight() * 2 + 4; if (tvx < 0) tvx = progressbar.getleft(); if(tvx >= progressbar.getright()-textrect.width()) tvx = progressbar.getright()-textrect.width(); canvas.drawtext(this.text, tvx, tvy + bitmap.getheight(), this.mpaint); } //初始化,画笔 private void inittext(context context) { view inflate = view.inflate(context, r.layout.textprogressbar, this); progressbar = (progressbar) inflate.findviewbyid(r.id.progressbar1); progressbar.setprogress(50); setwillnotdraw(false); this.mpaint = new paint(); this.mpaint.setcolor(getresources().getcolor(r.color.red)); this.mpaint.settextsize(20); textrect = new rect(); text = "0"; bitmap = bitmapfactory.decoderesource(context.getresources(), r.mipmap.progress_top); } public void setprogress(int progress) { progressbar.setprogress(progress); } public void settext(string str) { text = str; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Android实现百分比下载进度条效果