Android之加载图片时自定义进度条


也许我们在Android开发时有这样一个需求,在请求网络图片时,如果在图片还未完全显示完全时,显示一个比较漂亮简洁的进度条,是不是会显得很人性化呢?比如像下图所示:

下面我们就来实现一下这样一个进度条:主要代码先贴上,LoadingCircleView

  1. /**
  2. * 圆形加载进度条
  3. *
  4. * @author way
  5. *
  6. */
  7. publicclass LoadingCircleView extends View {
  8. privatefinal Paint paint;
  9. privatefinal Context context;
  10. private Resources res;
  11. privateint max = 100;
  12. privateint progress = 0;
  13. privateint ringWidth;
  14. // 圆环的颜色
  15. privateint ringColor;
  16. // 进度条颜色
  17. privateint progressColor;
  18. // 字体颜色
  19. privateint textColor;
  20. // 字的大小
  21. privateint textSize;
  22. private String textProgress;
  23. public LoadingCircleView(Context context, AttributeSet attrs, int defStyle) {
  24. super(context, attrs, defStyle);
  25. this.context = context;
  26. this.paint = new Paint();
  27. this.res = context.getResources();
  28. this.paint.setAntiAlias(true); // 消除锯齿
  29. this.ringWidth = dip2px(context, 3); // 设置圆环宽度
  30. this.ringColor = Color.BLACK;// 黑色进度条背景
  31. this.progressColor = Color.WHITE;// 白色进度条
  32. this.textColor = Color.BLACK;// 黑色数字显示进度;
  33. this.textSize = 15;// 默认字体大小
  34. }
  35. public LoadingCircleView(Context context, AttributeSet attrs) {
  36. this(context, attrs, 0);
  37. }
  38. public LoadingCircleView(Context context) {
  39. this(context, null);
  40. }
  41. /**
  42. * 设置进度条最大值
  43. *
  44. * @param max
  45. */
  46. publicsynchronizedvoid setMax(int max) {
  47. if (max < 0)
  48. max = 0;
  49. if (progress > max)
  50. progress = max;
  51. this.max = max;
  52. }
  53. /**
  54. * 获取进度条最大值
  55. *
  56. * @return
  57. */
  58. publicsynchronizedint getMax() {
  59. return max;
  60. }
  61. /**
  62. * 设置加载进度,取值范围在0~之间
  63. *
  64. * @param progress
  65. */
  66. publicsynchronizedvoid setProgress(int progress) {
  67. if (progress >= 0 && progress <= max) {
  68. this.progress = progress;
  69. invalidate();
  70. }
  71. }
  72. /**
  73. * 获取当前进度值
  74. *
  75. * @return
  76. */
  77. publicsynchronizedint getProgress() {
  78. return progress;
  79. }
  80. /**
  81. * 设置圆环背景色
  82. *
  83. * @param ringColor
  84. */
  85. publicvoid setRingColor(int ringColor) {
  86. this.ringColor = res.getColor(ringColor);
  87. }
  88. /**
  89. * 设置进度条颜色
  90. *
  91. * @param progressColor
  92. */
  93. publicvoid setProgressColor(int progressColor) {
  94. this.progressColor = res.getColor(progressColor);
  95. }
  96. /**
  97. * 设置字体颜色
  98. *
  99. * @param textColor
  100. */
  101. publicvoid setTextColor(int textColor) {
  102. this.textColor = res.getColor(textColor);
  103. }
  104. /**
  105. * 设置字体大小
  106. *
  107. * @param textSize
  108. */
  109. publicvoid setTextSize(int textSize) {
  110. this.textSize = textSize;
  111. }
  112. /**
  113. * 设置圆环半径
  114. *
  115. * @param ringWidth
  116. */
  117. publicvoid setRingWidthDip(int ringWidth) {
  118. this.ringWidth = dip2px(context, ringWidth);
  119. }
  120. /**
  121. * 通过不断画弧的方式更新界面,实现进度增加
  122. */
  123. @Override
  124. protectedvoid onDraw(Canvas canvas) {
  125. int center = getWidth() / 2;
  126. int radios = center - ringWidth / 2;
  127. // 绘制圆环
  128. this.paint.setStyle(Paint.Style.STROKE); // 绘制空心圆
  129. this.paint.setColor(ringColor);
  130. this.paint.setStrokeWidth(ringWidth);
  131. canvas.drawCircle(center, center, radios, this.paint);
  132. RectF oval = new RectF(center - radios, center - radios, center
  133. + radios, center + radios);
  134. this.paint.setColor(progressColor);
  135. canvas.drawArc(oval, 90, 360 * progress / max, false, paint);
  136. this.paint.setStyle(Paint.Style.FILL);
  137. this.paint.setColor(textColor);
  138. this.paint.setStrokeWidth(0);
  139. this.paint.setTextSize(textSize);
  140. this.paint.setTypeface(Typeface.DEFAULT_BOLD);
  141. textProgress = (int) (1000 * (progress / (10.0 * max))) + "%";
  142. float textWidth = paint.measureText(textProgress);
  143. canvas.drawText(textProgress, center - textWidth / 2, center + textSize
  144. / 2, paint);
  145. super.onDraw(canvas);
  146. }
  147. /**
  148. * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
  149. */
  150. publicstaticint dip2px(Context context, float dpValue) {
  151. finalfloat scale = context.getResources().getDisplayMetrics().density;
  152. return (int) (dpValue * scale + 0.5f);
  153. }
  154. }

其他的,我就不多说了,跟正常ProgressBar用法类似了,有需要的可以下载源码试试效果:

免费下载地址在 http://linux.bkjia.com/

用户名与密码都是www.bkjia.com

具体下载目录在 /2013年资料/5月/14日/Android之加载图片时自定义进度条

 

 

相关内容