Android動態(tài)繪制餅狀圖的示例代碼
項目里面的需求,當時搜索到MPAndroidChart庫,可以實現(xiàn),但是只是一個需求就引用偌大的一個庫,感覺不太爽,打算自己自定義一個。
一、慣例先上效果圖
更新圖
二、GitHub
代碼地址,歡迎指正https://github.com/MNXP/XPPieChart
三、思路
1、空心圖(一個大圓中心繪制一個小圓)
2、根據(jù)數(shù)據(jù)算出所占的角度
3、根據(jù)動畫獲取當前繪制的角度
4、根據(jù)當前角度獲取Paint使用的顏色
5、動態(tài)繪制即將繪制的 和 繪制已經(jīng)繪制的部分(最重要)
四、實現(xiàn)
1、空心圖(一個大圓中心繪制一個小圓)初始化數(shù)據(jù)
paint = new Paint(); paint.setAntiAlias(true); paint.setStyle(Paint.Style.FILL_AND_STROKE); screenW = DensityUtils.getScreenWidth(context); int width = DensityUtils.dip2px(context, 15);//圓環(huán)寬度 int widthXY = DensityUtils.dip2px(context, 10);//微調(diào)距離 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、根據(jù)數(shù)據(jù)算出所占的角度
使用遞歸保證cakeValues的值的總和必為100,然后根據(jù)值求出角度
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、根據(jù)動畫獲取當前繪制的角度
curAngle就是當前繪制的角度,drawArc()就是繪制的方法
cakeValueAnimator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float mAngle = obj2Float(animation.getAnimatedValue("angle")); curAngle = mAngle; drawArc(); } });
4、根據(jù)當前角度獲取Paint使用的顏色
根據(jù)當前的角度,計算當前是第幾個item,通過paint.setColor(Color.parseColor(cakeValues.get(colorIndex).getColors()));來設(shè)置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、動態(tài)繪制即將繪制的 和 繪制已經(jīng)繪制的部分
最重要的一步,我的需求是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);
繪制已經(jīng)繪制的扇形。根據(jù)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鍵進入后臺,再次進入,不需要動態(tài)繪制)
@Override protected void onDraw(Canvas canvas) { if (isFirst && isDrawByAnim) { drawCakeByAnim(); } isFirst = false; }
isDrawByAnim判斷是否需要動畫繪制
drawCake()為靜態(tài)繪制餅狀圖
public void surfaceCreated(SurfaceHolder holder) { if (!isFirst||!isDrawByAnim) drawCake(); }
更新
增加立體效果,提取配置參數(shù)
<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"/>//圓環(huán)寬度 <attr name="solidWidth" format="integer|reference"/>//立體寬度 <attr name="fineTuningWidth" format="integer|reference"/>//微調(diào)寬度 </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"/>
以上就是簡單的實現(xiàn)動態(tài)繪制餅狀圖,待完善,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android實現(xiàn)動態(tài)改變shape.xml中圖形的顏色
- Android 拍照選擇圖片并上傳功能的實現(xiàn)思路(包含權(quán)限動態(tài)獲取)
- Android動態(tài)修改應(yīng)用圖標與名稱的方法實例
- Android繪制動態(tài)折線圖
- Android GridView擴展仿微信微博發(fā)圖動態(tài)添加刪除圖片功能
- Android將Glide動態(tài)加載不同大小的圖片切圓角與圓形的方法
- Android 動態(tài)加載二維碼視圖生成快照的示例
- Android實現(xiàn)動態(tài)改變app圖標的示例代碼
- Android用RecyclerView實現(xiàn)動態(tài)添加本地圖片
- Android自定義view實現(xiàn)動態(tài)柱狀圖
- Android如何實現(xiàn)動態(tài)滾動波形圖(心電圖)功能
相關(guān)文章
基于flutter?sound插件實現(xiàn)錄音與播放功能
這篇文章主要介紹了基于flutter?sound插件實現(xiàn)錄音與播放功能,介紹了如何錄音,如何播放本地和遠程音頻文件,以及如何實現(xiàn)動畫,在錄制完音頻文件后如何上傳,這些都是我們平常使用這個功能會遇到的問題。在使用的過程中遇到的問題也有列出,需要的朋友可以參考下2022-05-05flutter showModalBottomSheet常用屬性及說明
這篇文章主要介紹了flutter showModalBottomSheet常用屬性及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09