以编程方式制作自定义饼图形状

3
我希望能够以编程方式绘制一些内容,将其设置为ImageView的背景。它应该看起来像这样:enter image description here。所以它有点像饼图,但它不是饼图。我找到了一些可以在圆圈中绘制圆圈的东西:
    ShapeDrawable biggerCircle= new ShapeDrawable( new OvalShape());
    biggerCircle.setIntrinsicHeight( 60 );
    biggerCircle.setIntrinsicWidth( 60);
    biggerCircle.setBounds(new Rect(0, 0, 60, 60));
    biggerCircle.getPaint().setColor(Color.BLUE);

    ShapeDrawable smallerCircle= new ShapeDrawable( new OvalShape());
    smallerCircle.setIntrinsicHeight( 10 );
    smallerCircle.setIntrinsicWidth( 10);
    smallerCircle.setBounds(new Rect(0, 0, 10, 10));
    smallerCircle.getPaint().setColor(Color.BLACK);
    smallerCircle.setPadding(50,50,50,50);
    Drawable[] d = {smallerCircle,biggerCircle};

    LayerDrawable composite1 = new LayerDrawable(d);

    btn.setBackgroundDrawable(composite1); 

我需要的是能够使用起始角度和结束角度绘制彩色区域的工具。这种工具是否可行?
1个回答

4
您可以使用库https://github.com/PhilJay/MPAndroidChart来实现此目的。
在xml中添加以下内容:
 <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

在Java代码中:

PieChart chart = (Piechart) findViewById(R.id.chart);

示例代码:

https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/PieChartActivity.java

下面是使用它的效果:

图示


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