歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android之加載圖片時自定義進度條

Android之加載圖片時自定義進度條

日期:2017/3/1 9:57:22   编辑:Linux編程

也許我們在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.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2013年資料/5月/14日/Android之加載圖片時自定義進度條

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved