Android自定义View——彩色圆环统计图

本文介绍了如何在Android中自定义View来创建一个彩色圆环统计图。首先展示了效果,然后详细解释了实现步骤,包括初始化变量、分配比例大小以确保能够正确计算圆环扫过的角度,并探讨了如何在处理比例为100%时避免精度问题的方法。
摘要由CSDN通过智能技术生成

效果展示




实现步骤


1、初始化变量


    
    
  1. //-------------必须给的数据相关-------------
  2. private String[] str = new String[]{ "一年级", "二年级", "三年级", "四年级", "五年级", "六年级"};
  3. //分配比例大小,总比例大小为100
  4. private int[] strPercent = new int[]{ 10, 25, 18, 41, 2, 5};
  5. //圆的直径
  6. private float mRadius = 300;
  7. //圆的粗细
  8. private float mStrokeWidth = 40;
  9. //文字大小
  10. private int textSize = 20;
  11. //-------------画笔相关-------------
  12. //圆环的画笔
  13. private Paint cyclePaint;
  14. //文字的画笔
  15. private Paint textPaint;
  16. //标注的画笔
  17. private Paint labelPaint;
  18. //-------------颜色相关-------------
  19. //边框颜色和标注颜色
  20. private int[] mColor = new int[]{ 0xFFF06292, 0xFF9575CD, 0xFFE57373, 0xFF4FC3F7, 0xFFFFF176, 0xFF81C784};
  21. //文字颜色
  22. private int textColor = 0xFF000000;
  23. //-------------View相关-------------
  24. //View自身的宽和高
  25. private int mHeight;
  26. private int mWidth;
圆的粗细:圆环的大小。

标注:文字前面的圆点。

分配比例大小:由于需要计算圆环扫过的角度,计算方法使用:(比例/100)*360度,用百分比算出360度占用了多少,由于比例/100的结果一直是0,所以换一种方法:(比例*360度)/100,先乘后除,但是这样会导致没办法获得100/100的值,所以我们分配比例大小的总和为101.


2、获取宽和高


    
    
  1. public MyChatView(Context context) {
  2. super(context);
  3. }
  4. public MyChatView(Context context, AttributeSet attrs) {
  5. super(context, attrs);
  6. }
  7. public MyChatView(Context context, AttributeSet attrs, int defStyleAttr) {
  8. super(context, attrs, defStyleAttr);
  9. }
  10. @Override
  11. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  12. super.onSizeChanged(w, h, oldw, oldh);
  13. mWidth = w;
  14. mHeight = h;
  15. }
3、绘制图形

    
    
  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3. super.onDraw(canvas);
  4. //移动画布到圆环的左上角
  5. canvas.translate(mWidth / 2 - mRadius / 2, mHeight / 2 - mRadius / 2);
  6. //初始化画笔
  7. initPaint();
  8. //画圆环
  9. drawCycle(canvas);
  10. //画文字和标注
  11. drawTextAndLabel(canvas);
  12. }
  13. /**
  14. * 初始化画笔
  15. */
  16. private void initPaint() {
  17. //边框画笔
  18. cyclePaint = new Paint();
  19. cyclePaint.setAntiAlias( true);
  20. cyclePaint.setStyle(Paint.Style.STROKE);
  21. cyclePaint.setStrokeWidth(mStrokeWidth);
  22. //文字画笔
  23. textPaint = new Paint();
  24. textPaint.setAntiAlias( true);
  25. textPaint.setColor(textColor);
  26. textPaint.setStyle(Paint.Style.STROKE);
  27. textPaint.setStrokeWidth( 1);
  28. textPaint.setTextSize(textSize);
  29. //标注画笔
  30. labelPaint = new Paint();
  31. labelPaint.setAntiAlias( true);
  32. labelPaint.setStyle(Paint.Style.FILL);
  33. labelPaint.setStrokeWidth( 2);
  34. }
  35. /**
  36. * 画圆环
  37. *
  38. * @param canvas
  39. */
  40. private void drawCycle(Canvas canvas) {
  41. float startPercent = 0;
  42. float sweepPercent = 0;
  43. for ( int i = 0; i < strPercent.length; i++) {
  44. cyclePaint.setColor(mColor[i]);
  45. startPercent = sweepPercent + startPercent;
  46. //这里采用比例占100的百分比乘于360的来计算出占用的角度,使用先乘再除可以算出值
  47. sweepPercent = strPercent[i] * 360 / 100;
  48. canvas.drawArc( new RectF( 0, 0, mRadius, mRadius), startPercent, sweepPercent, false, cyclePaint);
  49. }
  50. }
  51. /**
  52. * 画文字和标注
  53. *
  54. * @param canvas
  55. */
  56. private void drawTextAndLabel(Canvas canvas) {
  57. for ( int i = 0; i < strPercent.length; i++) {
  58. //文字离右边环边距为60,文字与文字之间的距离为40
  59. canvas.drawText(str[i], mRadius + 60, i * 40, textPaint);
  60. //画标注,标注离右边环边距为40,y轴则要减去半径(10)的一半才能对齐文字
  61. labelPaint.setColor(mColor[i]);
  62. canvas.drawCircle(mRadius + 40, i * 40 - 5, 10, labelPaint);
  63. }
  64. }

原理分析

1、画布移到圆环的左上角,为(0,0)。
2、画圆环:使用drawArc方法画出一个直径为mRadius的圆环,从初始角度开始,扫过多少角度。这里使用初始角度的递增方法使圆环一段接上一段的画出来。如果想让圆环旋转起来,就修改startPercent的值即可。
3、画文字和标注:将文字和标注画于圆环的右上角,也即圆的直径加上一段间距即可,同理,标注也是。


源码下载
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值