Canvas.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。 FlashShapeView.java类具有绘制不同形状的示例。https://github.com/jdgreene2008/android_custom_views/blob/master/CustomUiComponents/app/src/main/java/com/jarvis/dragdropresearch/views/FlashShapeView.java