PieChart 饼图

 2023-09-05 阅读 343 评论 0

摘要:图表网上有很多第三方工具,但是往往都会受到一些限制,于是用canvas画pieChart. Java代码 //定义一些常量 intareaX=1; intareaY=22; intareaWidth; intareaHight; //pie上的颜色 intcolors[]; //占的比例 intpercent[]; inttempAngle=0; privateBi

图表网上有很多第三方工具,但是往往都会受到一些限制,于是用canvas画pieChart.

 

 

 
  1. Java代码  
  2.       
  3.          // 定义一些常量         
  4.     int areaX = 1;         
  5.     int areaY = 22;         
  6.     int areaWidth;         
  7.     int areaHight;         
  8.      //pie上的颜色         
  9.     int colors[];          
  10.     //占的比例         
  11.     int percent[];         
  12.     int tempAngle = 0;         
  13.     private Bitmap bitmap;         
  14.     /**        
  15.      * 屏幕中间点的X、Y坐标        
  16.      */        
  17.     private float centerX,centerY;         
  18.         
  19.     /**        
  20.      * 大园半径        
  21.      */        
  22.     private float bigR;         
  23.     private Canvas canvas;         
  24.     Bitmap OverlayBitmap;         
  25.     /**        
  26.      * 屏幕的宽        
  27.      */        
  28.     private int width;         
  29.         
  30.     /**        
  31.      * 屏幕的高        
  32.      */        
  33.     private int height;         
  34.         
  35.     @Override        
  36.     protected void onDraw(Canvas canvas) {         
  37.         super.onDraw(canvas);         
  38.         this.canvas = canvas;         
  39.         centerX = width / 2;         
  40.         centerY = height / 2;         
  41.         bigR = (width - 80) / 2;         
  42.         areaWidth = width - 2;         
  43.         areaHight = height - 2;         
  44.         Paint paint = new Paint();         
  45.         paint.setColor(Color.RED);         
  46.         paint.setStyle(Style.FILL);         
  47.         paint.setAntiAlias(true);         
  48.         paint.setStrokeWidth(1);         
  49.         
  50.         canvas.save();         
  51.         canvas.rotate(mDegree);      
  52.         canvas.translate(centerX, centerY);               
  53.         for (int j = 0; j < percent.length; j++) {         
  54.              paint.setColor(colors[j]);         
  55.              canvas.drawArc(new RectF(-bigR, -bigR,bigR, bigR), tempAngle,percent[j],         
  56.                       true,paint);tempAngle += percent[j];         
  57.         }         
  58.         canvas.restore();        
  59. }     

 把在饼图上占的各个部分,由在初始化此view时传参,代码便灵活使用

 

 
  1. Java代码  
  2.       
  3. int[] colors =new int[]{Color.rgb(255068),Color.rgb(422766),Color.rgb(120120120),Color.rgb(2551450)};         
  4.        int[] percent = new int[]{50,140,100,70};         
  5.        PieView pieView = new PieView(this,width,height,colors,percent);    

percent 在饼图上所占的比例,无论里面有几个元素,总和都必须是360,不然画出来是有空缺的.

 

 
  1. Java代码  
  2.       
  3.     int mDegree = 0// 偏移量         
  4.     int mPreDegree = 0// 上次偏移量         
  5.     public void calcDegree(boolean bFlag) {         
  6.         if (mDegree >= 360) {         
  7.             mDegree -= 360;         
  8.         }         
  9.         
  10.         if (mDegree < 0) {         
  11.             mDegree += 360;         
  12.         }         
  13.         
  14.         mPreDegree = mDegree;         
  15.     }        
  16.           
  17.        
  18. @Override        
  19. public boolean onKeyDown(int keyCode, KeyEvent event) {         
  20.     // TODO Auto-generated method stub         
  21.         
  22.     switch (keyCode) {         
  23. //顺时针转动      
  24.     case KeyEvent.KEYCODE_DPAD_LEFT:         
  25.         mDegree -= 6;         
  26.         calcDegree(false);         
  27.         
  28.         postInvalidate();         
  29.         break;         
  30. //逆时针转动      
  31.     case KeyEvent.KEYCODE_DPAD_RIGHT:         
  32.         mDegree += 6;         
  33.         calcDegree(false);         
  34.         
  35.         postInvalidate();         
  36.         break;         
  37.     default:         
  38.         break;         
  39.     }         
  40.         
  41.     return super.onKeyDown(keyCode, event);         
  42. }    

 


     本文转自06peng 51CTO博客,原文链接:http://blog.51cto.com/06peng/963352,如需转载请自行联系原作者



版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/266.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息