在Android中绘制水平填充的圆形。

10

为了在Android中绘制圆形和饼状图,我们可以使用AChartEngine Android框架,如此处所示。

但是,如何在Android中绘制部分水平(或垂直)填充的圆形呢?我是指例如根据Java代码中指定的百分比从底部到顶部填充的圆形。

以下是我们所需内容的预览:

Preview


3
我在我的回答中提供了一种方法来实现这个目标,实际上还有另外一种方法,如果你查看最初的帖子的修订历史记录。然而,我的初始答案仅适用于API 19及以上版本。 - Mike M.
1
你的答案更好,而且很简单(只有一个实用类)。谢谢@MikeM。 - Omar
2个回答

7

尝试使用这个库 Circle Progress
以下是该库作者提供的示例:

<com.github.lzyzsd.circleprogress.CircleProgress
    android:id="@+id/circle_progress"
    android:layout_marginLeft="50dp"
    android:layout_width="100dp"
    android:layout_height="100dp"
    custom:circle_progress="20"/>

circle


我发现实现和成功运行这个库/代码有些困难。虽然这个库提供了许多在Android中进度条的详细信息和方法。谢谢@Minhtdh。 - Omar

0
扩展 Drawable 并使您的 draw 方法如下所示:
public void draw(Canvas c) {
    float size = ...; // The size of your circle
    Paint color = new Paint();
    color.setColor(...); // Color of your circle
    c.drawCircle(size / 2, size / 2, size / 2, color);
    Path p = new Path();
    float ratio = ...; // How many of the circle you want to have filled
    float offset = size * (float) Math.sqrt(1-((double) (0.5-ratio) * 2)*((double) (0.5-ratio) * 2)) / 2;
    float angle = (float) Math.asin((double) (0.5-ratio) * 2);
    p.addArc(offset, size * (1-percent), size - offset, size, angle, Math.PI - angle);
    p.close();
    c.drawPath(p, color);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接