当前位置:   article > 正文

超简单的Android圆形进度条_android 圆形进度条

android 圆形进度条

效果图:

代码优化/简化、教科书级别注释、复制粘贴即可用

代码:

  1. package com.zistone.factorytest0718.view;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.graphics.Rect;
  7. import android.graphics.RectF;
  8. import android.util.AttributeSet;
  9. import android.util.Log;
  10. import android.view.View;
  11. import com.zistone.factorytest0718.BaseActivity;
  12. /**
  13. * 圆形进度条控件
  14. *
  15. * @author LiWei
  16. * @date 2021/2/19 16:17
  17. * @email 652276536@qq.com
  18. */
  19. public class MyCircleProgress extends View {
  20. private static final String TAG = "MyCircleProgress";
  21. private Paint _paint;
  22. private RectF _rectF;
  23. private Rect _rect;
  24. private int _current = 1, _max = 100;
  25. //圆弧(也可以说是圆环)的宽度
  26. private float _arcWidth = 30;
  27. //控件的宽度
  28. private float _width;
  29. public MyCircleProgress(Context context) {
  30. this(context, null);
  31. }
  32. public MyCircleProgress(Context context, AttributeSet attrs) {
  33. this(context, attrs, 0);
  34. }
  35. public MyCircleProgress(Context context, AttributeSet attrs, int defStyleAttr) {
  36. super(context, attrs, defStyleAttr);
  37. _paint = new Paint();
  38. _paint.setAntiAlias(true);
  39. _rectF = new RectF();
  40. _rect = new Rect();
  41. }
  42. public void SetCurrent(int _current) {
  43. Log.i(TAG, "当前值:" + _current + ",最大值:" + _max);
  44. this._current = _current;
  45. invalidate();
  46. }
  47. public void SetMax(int _max) {
  48. this._max = _max;
  49. }
  50. @Override
  51. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  52. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  53. //getMeasuredWidth获取的是view的原始大小,也就是xml中配置或者代码中设置的大小
  54. //getWidth获取的是view最终显示的大小,这个大小不一定等于原始大小
  55. _width = getMeasuredWidth();
  56. }
  57. @Override
  58. protected void onDraw(Canvas canvas) {
  59. super.onDraw(canvas);
  60. //绘制圆形
  61. //设置为空心圆,如果不理解绘制弧线是什么意思就把这里的属性改为“填充”,跑一下瞬间就明白了
  62. _paint.setStyle(Paint.Style.STROKE);
  63. //设置圆弧的宽度(圆环的宽度)
  64. _paint.setStrokeWidth(_arcWidth);
  65. _paint.setColor(Color.GRAY);
  66. //大圆的半径
  67. float bigCircleRadius = _width / 2;
  68. //小圆的半径
  69. float smallCircleRadius = bigCircleRadius - _arcWidth;
  70. //绘制小圆
  71. canvas.drawCircle(bigCircleRadius, bigCircleRadius, smallCircleRadius, _paint);
  72. _paint.setColor(BaseActivity.SPRING_GREEN);
  73. _rectF.set(_arcWidth, _arcWidth, _width - _arcWidth, _width - _arcWidth);
  74. //绘制圆弧
  75. canvas.drawArc(_rectF, 90, _current * 360 / _max, false, _paint);
  76. //计算百分比
  77. String txt = _current * 100 / _max + "%";
  78. _paint.setStrokeWidth(0);
  79. _paint.setTextSize(40);
  80. _paint.getTextBounds(txt, 0, txt.length(), _rect);
  81. _paint.setColor(BaseActivity.SPRING_GREEN);
  82. //绘制百分比
  83. canvas.drawText(txt, bigCircleRadius - _rect.width() / 2, bigCircleRadius + _rect.height() / 2, _paint);
  84. }
  85. }

调用:

  1. package com.zistone.factorytest0718;
  2. import android.os.Bundle;
  3. import com.zistone.factorytest0718.view.MyCircleProgress;
  4. /**
  5. * 用来测试一些东西的,没有任何实际功能...
  6. *
  7. * @author LiWei
  8. * @date 2020/7/18 9:33
  9. * @email 652276536@qq.com
  10. */
  11. public class Test1Activity extends BaseActivity {
  12. private static final String TAG = "Test1Activity";
  13. private boolean _threadFlag = false;
  14. @Override
  15. protected void onDestroy() {
  16. _threadFlag = true;
  17. super.onDestroy();
  18. }
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_test1);
  23. MyCircleProgress myCircleProgress = findViewById(R.id.控件名);
  24. Thread thread = new Thread(() -> {
  25. int j = 0;
  26. while (!_threadFlag && j < 100) {
  27. try {
  28. Thread.sleep(100);
  29. } catch (InterruptedException e) {
  30. e.printStackTrace();
  31. }
  32. j++;
  33. int finalJ = j;
  34. runOnUiThread(() -> myCircleProgress.SetCurrent(finalJ));
  35. }
  36. });
  37. thread.start();
  38. }
  39. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/474150
推荐阅读
相关标签
  

闽ICP备14008679号