免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3996 | 回复: 0
打印 上一主题 下一主题

android中在progressBar上添加文字显示百分比进度 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-23 10:47 |只看该作者 |倒序浏览
android中在progressBar上添加文字显示百分比进度







工作中遇到ProgressBar上加文字显示的问题,在网上查了下,发现好多都说可以用TextProgressBar来实现,但具体的使用方法却没有。后来在一个黑客网站发现了一个方法。觉得还不错,所以拿来用了,效果也还行。在此分享一下:

    一般的ProgressBar都只是一个光光的条(这里说的都是水平进度条),虽然比不用进度条时给用户的感觉要好,但是如果在形像化的东西上面再加上点文字,将进度描述量化,就可以让用户更加明白当前进度是多少了。
  有了需求,就可以开始实现了。
  这里的原理就是继承一个ProgressBar,然后重写里面的onDraw()方法。

Java代码
  1. 1.public class MyProgressBar extends ProgressBar {   
  2. 2.    String text;   
  3. 3.    Paint mPaint;   
  4. 4.  
  5. 5.    public MyProgressBar (Context context) {   
  6. 6.        super(context);   
  7. 7.        System.out.println("1");   
  8. 8.        initText();   
  9. 9.    }   
  10. 10.  
  11. 11.    public MyProgressBar (Context context, AttributeSet attrs, int defStyle) {   
  12. 12.        super(context, attrs, defStyle);   
  13. 13.        System.out.println("2");   
  14. 14.        initText();   
  15. 15.    }   
  16. 16.  
  17. 17.    public MyProgressBar (Context context, AttributeSet attrs) {   
  18. 18.        super(context, attrs);   
  19. 19.        System.out.println("3");   
  20. 20.        initText();   
  21. 21.    }   
  22. 22.  
  23. 23.    @Override  
  24. 24.    public synchronized void setProgress(int progress) {   
  25. 25.        setText(progress);   
  26. 26.        super.setProgress(progress);   
  27. 27.    }   
  28. 28.  
  29. 29.    @Override  
  30. 30.    protected synchronized void onDraw(Canvas canvas) {   
  31. 31.        super.onDraw(canvas);   
  32. 32.//this.setText();   
  33. 33.        Rect rect = new Rect();   
  34. 34.        this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);   
  35. 35.        int x = (getWidth() / 2) - rect.centerX();   
  36. 36.  
  37. 37.        int y = (getHeight() / 2) - rect.centerY();   
  38. 38.        canvas.drawText(this.text, x, y, this.mPaint);   
  39. 39.    }   
  40. 40.  
  41. 41.//初始化,画笔   
  42. 42.    private void initText() {   
  43. 43.        this.mPaint = new Paint();   
  44. 44.        this.mPaint.setColor(Color.WHITE);   
  45. 45.    }   
  46. 46.  
  47. 47.//  private void setText() {   
  48. 48.//      setText(this.getProgress());   
  49. 49.//  }   
  50. 50.  
  51. 51.//设置文字内容   
  52. 52.    private void setText(int progress) {   
  53. 53.        int i = (progress * 1) / this.getMax();   
  54. 54.        this.text = String.valueOf(i) + "%";   
  55. 55.    }   
  56. 56.}  
  57. public class MyProgressBar extends ProgressBar {
  58.         String text;
  59.         Paint mPaint;

  60.         public MyProgressBar (Context context) {
  61.                 super(context);
  62.                 System.out.println("1");
  63.                 initText();
  64.         }

  65.         public MyProgressBar (Context context, AttributeSet attrs, int defStyle) {
  66.                 super(context, attrs, defStyle);
  67.                 System.out.println("2");
  68.                 initText();
  69.         }

  70.         public MyProgressBar (Context context, AttributeSet attrs) {
  71.                 super(context, attrs);
  72.                 System.out.println("3");
  73.                 initText();
  74.         }

  75.         @Override
  76.         public synchronized void setProgress(int progress) {
  77.                 setText(progress);
  78.                 super.setProgress(progress);
  79.         }

  80.         @Override
  81.         protected synchronized void onDraw(Canvas canvas) {
  82.                 super.onDraw(canvas);
  83. //this.setText();
  84.                 Rect rect = new Rect();
  85.                 this.mPaint.getTextBounds(this.text, 0, this.text.length(), rect);
  86.                 int x = (getWidth() / 2) - rect.centerX();

  87.                 int y = (getHeight() / 2) - rect.centerY();
  88.                 canvas.drawText(this.text, x, y, this.mPaint);
  89.         }

  90. //初始化,画笔
  91.         private void initText() {
  92.                 this.mPaint = new Paint();
  93.                 this.mPaint.setColor(Color.WHITE);
  94.         }

  95. //        private void setText() {
  96. //                setText(this.getProgress());
  97. //        }

  98. //设置文字内容
  99.         private void setText(int progress) {
  100.                 int i = (progress * 1) / this.getMax();
  101.                 this.text = String.valueOf(i) + "%";
  102.         }
  103. }
复制代码
这样写后,在xml布局文件中使用时,可能会因为命名空间改变,下面属性无法用代码提示。一个简单的做法就是,先写一个正常的ProgressBar的标记,把属性写完后,再将ProgressBar替换为我们自定义的进度条的完整类名。还有使用自定义的控件后,android的布局预览是看不到效果的,运行到模拟器上就行了。
  最后,使用方法就和普通的ProgressBar差不多。

本篇文章来源于 黑软基地-中国最大的黑客软件安全教程下载站!(手机资讯) 原文链接:http://www.hackvip.com/mobiwen/html/Mobile_250548.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP