在颤动中获取弧的路径长度

Get length of path of an arc in flutter

我目前正在研究 Flutter project,我试图了解 computeMetrics() 的工作原理。

Android

我之前实施了一个 Android 解决方案来处理圆弧,并且它有效。部分实现如下图:

   final RectF oval = new RectF(centerX - radiusArc, centerY - radiusArc, centerX + radiusArc, centerY + radiusArc);
    mPath.addArc(oval, startAngle, sweepAngle);

    PathMeasure pm = new PathMeasure(mPath, false);
    float[] xyCoordinate = {startPoint.x, startPoint.y};
    float pathLength = pm.getLength();

颤动

现在我正试图在 Flutter 中复制相同的内容 (.dart)。出于这个原因,我正在使用以下实现

  final Rect oval = Rect.fromLTRB(centerX - radiusArc, centerY - radiusArc, centerX + radiusArc, centerY + radiusArc);
  mPath.addArc(oval, startAngle, sweepAngle);

  List<num> xyCoordinate = new List<num>(2);
  xyCoordinate[0] = startPoint.x;
  xyCoordinate[1] = startPoint.y;

  List<PathMetric> pm = mPath.computeMetrics().toList();
  double pathLength = pm.iterator.current.length;

我尝试通过几个示例来了解如何获得路径的长度,但是在使用我的实现时,pm.iterator.currentnull 因此我无法获得 length

我假设我错误地理解了 computeMetrics() 的工作原理。

我认为您需要遍历列表并添加每个 PathMetriclength 以获得路径长度。

double pathLength = 0;
pm.forEach((contour){
  pathLength += contour.length;
});
print(pathLength);

这是因为,就像 computeMetrics() 的文档解释的那样 -

A Path is made up of zero or more contours.

A PathMetric object describes properties of an individual contour, such as its length etc.

此外,computeMetrics() returns 是单个 PathMetric 的可迭代集合,每个 PathMetric 对应于构成路径的各个轮廓。所以为了得到Path的长度,我们需要把列表中每个PathMetric(轮廓)的长度加起来,就是List<PathMetric> pm = mPath.computeMetrics().toList();