创建类似于 Google Fit 的圆环图

Create Doughnut Chart Similar to Google Fit

有谁知道如何创建类似于 Google Fit.

中的圆环图

我也想要这个,但我能找到的最佳答案是 "make your own"。 所以我做到了。

这是非常基础的(我是 android 的新手)并且未完成,但它应该能给您一些启发。

基本上,您只需设置绘画对象

    paintPrimary = new Paint();
    paintPrimary.setAntiAlias(true);
    paintPrimary.setColor(colorPrimary);
    paintPrimary.setStyle(Paint.Style.STROKE);
    paintPrimary.setStrokeCap(Paint.Cap.ROUND);

并调用 canvas.drawArc

class FitDoughnutView extends View {

    private RectF _oval;

    public FitDoughnutView(Context ctx) {
        super(ctx);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawArc(_oval, 0, 360, false, paintSecondary);

        canvas.drawArc(_oval, 270, percentDeg, false, paintPrimary);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        _oval = new RectF(width, width, w - width, h - width);
    }
}

完整来源: github.com/tehmantra/fitdoughnut

某人的教程:hmkcode.com/android-canvas-how-to-draw-2d-donut-chart/

我会推荐 this library,因为它得到积极维护并且有很多选项。

它有一个 how to use it in Kotlin 的指南,但您也可以像这样在 Java 中使用它:

在您的布局文件中:

<app.futured.donut.DonutProgressView
        android:id="@+id/dpvChart"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_marginTop="8dp"
        app:donut_bgLineColor="@color/grey"
        app:donut_gapAngle="270"
        app:donut_gapWidth="20"
        app:donut_strokeWidth="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

然后在你的 Java Activity:

private DonutProgressView dpvChart;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    dpvChart = findViewById(R.id.dpvChart);

    DonutSection section = new DonutSection("Section 1 Name", Color.parseColor("#f44336"), 80.0f);
    dpvChart.setCap(100f);
    dpvChart.submitData(new ArrayList<>(Collections.singleton(section)));
}
        <com.google.android.material.progressindicator.CircularProgressIndicator
          app:indicatorSize="60dp"
          android:progress="60"
          app:trackCornerRadius="10dp"
          app:trackThickness="10dp"
          app:trackColor="@color/white"
          app:indicatorColor="@color/teal_200"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />