Android中如何使用progressbar实现带底部指示器和文字进度条

本篇内容主要讲解“Android中如何使用progressbar实现带底部指示器和文字进度条”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android中如何使用progressbar实现带底部指示器和文字进度条”吧!

列出源码:

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中如何使用progressbar实现带底部指示器和文字进度条”有了更深的了解,不妨来实际操作一番吧!这里是蜗牛博客网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接