Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中绘制这样的动态饼图?_Android_Pie Chart - Fatal编程技术网

如何在Android中绘制这样的动态饼图?

如何在Android中绘制这样的动态饼图?,android,pie-chart,Android,Pie Chart,在不使用任何第三方库的情况下,绘制这样的饼图最简单的方法是什么 画布。drawArc()是一条路要走 使用3个距离进行采样: float distanceA = 20.0f; float distanceB = 30.0f; float distanceC = 40.0f; float distanceTotal = distanceA + distanceB + distanceC; /* Calculate the percentage that each distance repres

在不使用任何第三方库的情况下,绘制这样的饼图最简单的方法是什么

画布。drawArc()
是一条路要走

使用3个距离进行采样:

float distanceA = 20.0f;
float distanceB = 30.0f;
float distanceC = 40.0f;

float distanceTotal = distanceA + distanceB + distanceC;

/* Calculate the percentage that each distance represents out of the whole. That percentage represents the fraction of the the arc. */

float percentageA = distanceA/distanceTotal;
float percentageB = distanceB/distanceTotal;
float percentageC = distanceC/distanceTotal;

// Now draw the arc. For our bounds, just use a 40 X 40 
// rectangle, but change it for your dimensions.
RectF bounds = new RectF(0.0,0.0,40.0,40.0);

float startAngle = 0.0;
float angleA = percentageA * 360.0f;
float angleB = percentageB * 360.0f;
float angleC = percentageC * 360.0f;

// Draw the arc representing distanceA:
paint.setColor(colorA)
canvas.drawArc(bounds, startAngle,
                        angleA, true, paint);

/* Next arc will be drawn starting at the point where the arc representing distance A ends */
startAngle+=angleA;
paint.setColor(colorB);
canvas.drawArc(bounds, startAngle,
                        angleB, true, paint);

startAngle+=angleB;

// And so on for angle C. Adjust the stroke width on // the paint object to increase your arc thickness.

昨天实现了一个类似圆形饼图的功能,可以在用户滚动时绘制线段。可以在这里参考我的github。java类提供了绘制不同形状的示例。

我建议您使用MpAndroidChart。

创建您自己的视图或复合视图。这不是一件容易的工作,但就是这样做的。如果要避免使用任何第三方库,请尝试Canvas.drawArc