項目里面的需求,當時搜索到MPAndroidChart庫,可以實現,但是只是一個需求就引用偌大的一個庫,感覺不太爽,打算自己自定義一個。
一、慣例先上效果圖

更新圖
二、GitHub
代碼地址,歡迎指正https://github.com/MNXP/XPPieChart
三、思路
1、空心圖(一個大圓中心繪制一個小圓)
2、根據數據算出所占的角度
3、根據動畫獲取當前繪制的角度
4、根據當前角度獲取Paint使用的顏色
5、動態繪制即將繪制的 和 繪制已經繪制的部分(最重要)
四、實現
1、空心圖(一個大圓中心繪制一個小圓)初始化數據
paint = new Paint(); paint.setAntiAlias(true); paint.setStyle(Paint.Style.FILL_AND_STROKE); screenW = DensityUtils.getScreenWidth(context); int width = DensityUtils.dip2px(context, 15);//圓環寬度 int widthXY = DensityUtils.dip2px(context, 10);//微調距離 int pieCenterX = screenW / 2;//餅狀圖中心X int pieCenterY = screenW / 3;//餅狀圖中心Y int pieRadius = screenW / 4;// 大圓半徑 //整個餅狀圖rect pieOval = new RectF(); pieOval.left = pieCenterX - pieRadius; pieOval.top = pieCenterY - pieRadius + widthXY; pieOval.right = pieCenterX + pieRadius; pieOval.bottom = pieCenterY + pieRadius + widthXY; //里面的空白rect pieOvalIn = new RectF(); pieOvalIn.left = pieOval.left + width; pieOvalIn.top = pieOval.top + width; pieOvalIn.right = pieOval.right - width; pieOvalIn.bottom = pieOval.bottom - width; //里面的空白畫筆 piePaintIn = new Paint(); piePaintIn.setAntiAlias(true); piePaintIn.setStyle(Paint.Style.FILL); piePaintIn.setColor(Color.parseColor("#f4f4f4"));2、根據數據算出所占的角度
使用遞歸保證cakeValues的值的總和必為100,然后根據值求出角度
private void settleCakeValues(int i) { float sum = getSum(cakeValues, i); CakeValue value = cakeValues.get(i); if (sum <= 100f) { value.setItemValue(100f - sum); cakeValues.set(i, value); } else { value.setItemValue(0); settleCakeValues(i - 1); } }3、根據動畫獲取當前繪制的角度
curAngle就是當前繪制的角度,drawArc()就是繪制的方法
cakeValueAnimator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float mAngle = obj2Float(animation.getAnimatedValue("angle")); curAngle = mAngle; drawArc(); } });4、根據當前角度獲取Paint使用的顏色
根據當前的角度,計算當前是第幾個item,通過paint.setColor(Color.parseColor(cakeValues.get(colorIndex).getColors()));來設置paint的顏色
private int getCurItem(float curAngle) { int res = 0; for (int i = 0; i < itemFrame.length; i++) { if (curAngle <= itemFrame[i] * ANGLE_NUM) { res = i; break; } } return res; }5、動態繪制即將繪制的 和 繪制已經繪制的部分
最重要的一步,我的需求是4類,用不同的顏色
繪制當前顏色的扇形,curStartAngle扇形的起始位置,curSweepAngle扇形的終止位置
paint.setColor(Color.parseColor(cakeValues.get(colorIndex).getColors())); float curStartAngle = 0; float curSweepAngle = curAngle; if (curItem > 0) { curStartAngle = itemFrame[curItem - 1] * ANGLE_NUM; curSweepAngle = curAngle - (itemFrame[curItem - 1] * ANGLE_NUM); } canvas.drawArc(pieOval, curStartAngle, curSweepAngle, true, paint);繪制已經繪制的扇形。根據curItem判斷繪制過得扇形
for (int i = 0; i < curItem; i++) { paint.setColor(Color.parseColor(cakeValues.get(i).getColors())); if (i == 0) { canvas.drawArc(pieOval, startAngle,(float) cakeValues.get(i).getItemValue() * ANGLE_NUM, true, paint); continue; } canvas.drawArc(pieOval,itemFrame[i - 1] * ANGLE_NUM,(float) cakeValues.get(i).getItemValue() * ANGLE_NUM, true, paint);}繪制中心的圓
canvas.drawArc(pieOvalIn, 0, 360, true, piePaintIn);
6、特別注意
isFirst判斷是夠是第一次繪制(繪制完成后,home鍵進入后臺,再次進入,不需要動態繪制)
@Override protected void onDraw(Canvas canvas) { if (isFirst && isDrawByAnim) { drawCakeByAnim(); } isFirst = false; }isDrawByAnim判斷是否需要動畫繪制
drawCake()為靜態繪制餅狀圖
public void surfaceCreated(SurfaceHolder holder) { if (!isFirst||!isDrawByAnim) drawCake();}更新
增加立體效果,提取配置參數
<declare-styleable name="CakeSurfaceView"> <attr name="isDrawByAnim" format="boolean"/>//是否動畫 <attr name="isSolid" format="boolean"/>//是否立體 <attr name="duration" format="integer|reference"/>//動畫時間 <attr name="defaultColor" format="string"/>//默認顏色 <attr name="ringWidth" format="integer|reference"/>//圓環寬度 <attr name="solidWidth" format="integer|reference"/>//立體寬度 <attr name="fineTuningWidth" format="integer|reference"/>//微調寬度 </declare-styleable>
xml中使用
<com.xp.xppiechart.view.CakeSurfaceView android:id="@+id/assets_pie_chart" android:background="#ffffff" android:layout_width="wrap_content" android:layout_height="wrap_content" app:defaultColor="#ff8712" app:ringWidth="20" app:solidWidth="5" app:duration="3000" app:isSolid="true" app:isDrawByAnim="true"/>
以上就是簡單的實現動態繪制餅狀圖,待完善,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答