【问题标题】:Create Doughnut Chart Similar to Google Fit创建类似于 Google Fit 的甜甜圈图
【发布时间】:2015-03-26 14:36:30
【问题描述】:

有谁知道如何创建类似于 Google Fit 中的圆环图。

【问题讨论】:

标签: android user-interface google-fit


【解决方案1】:

我也想要这个,但我能找到的最佳答案是“自己制作”。 所以我做到了。

这是非常基本的(我是 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/

【讨论】:

    【解决方案2】:

    我会推荐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)));
    }
    

    【讨论】:

    • 如何实现点击切片?我希望切片能够直观地表明它已被选中。
    • 我没有看到任何地方可以用这个库做到这一点。也许试试这个:github.com/furkanaskin/ClickablePieChart
    【解决方案3】:
            <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" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多