您好,登錄后才能下訂單哦!
圖表網上有很多第三方工具,但是往往都會受到一些限制,于是用canvas畫pieChart.
- Java代碼
- // 定義一些常量
- int areaX = 1;
- int areaY = 22;
- int areaWidth;
- int areaHight;
- //pie上的顏色
- int colors[];
- //占的比例
- int percent[];
- int tempAngle = 0;
- private Bitmap bitmap;
- /**
- * 屏幕中間點的X、Y坐標
- */
- private float centerX,centerY;
- /**
- * 大園半徑
- */
- private float bigR;
- private Canvas canvas;
- Bitmap OverlayBitmap;
- /**
- * 屏幕的寬
- */
- private int width;
- /**
- * 屏幕的高
- */
- private int height;
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- this.canvas = canvas;
- centerX = width / 2;
- centerY = height / 2;
- bigR = (width - 80) / 2;
- areaWidth = width - 2;
- areaHight = height - 2;
- Paint paint = new Paint();
- paint.setColor(Color.RED);
- paint.setStyle(Style.FILL);
- paint.setAntiAlias(true);
- paint.setStrokeWidth(1);
- canvas.save();
- canvas.rotate(mDegree);
- canvas.translate(centerX, centerY);
- for (int j = 0; j < percent.length; j++) {
- paint.setColor(colors[j]);
- canvas.drawArc(new RectF(-bigR, -bigR,bigR, bigR), tempAngle,percent[j],
- true,paint);tempAngle += percent[j];
- }
- canvas.restore();
- }
把在餅圖上占的各個部分,由在初始化此view時傳參,代碼便靈活使用
- Java代碼
- int[] colors =new int[]{Color.rgb(255, 0, 68),Color.rgb(4, 227, 66),Color.rgb(120, 120, 120),Color.rgb(255, 145, 0)};
- int[] percent = new int[]{50,140,100,70};
- PieView pieView = new PieView(this,width,height,colors,percent);
percent 在餅圖上所占的比例,無論里面有幾個元素,總和都必須是360,不然畫出來是有空缺的.
- Java代碼
- int mDegree = 0; // 偏移量
- int mPreDegree = 0; // 上次偏移量
- public void calcDegree(boolean bFlag) {
- if (mDegree >= 360) {
- mDegree -= 360;
- }
- if (mDegree < 0) {
- mDegree += 360;
- }
- mPreDegree = mDegree;
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- // TODO Auto-generated method stub
- switch (keyCode) {
- //順時針轉動
- case KeyEvent.KEYCODE_DPAD_LEFT:
- mDegree -= 6;
- calcDegree(false);
- postInvalidate();
- break;
- //逆時針轉動
- case KeyEvent.KEYCODE_DPAD_RIGHT:
- mDegree += 6;
- calcDegree(false);
- postInvalidate();
- break;
- default:
- break;
- }
- return super.onKeyDown(keyCode, event);
- }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。