安卓:如何获取绘制路径的区域大小?

4

我的问题是测量路径的表面积。
我生成了一个随机路径并在画布上绘制它。在触摸这个闭合路径后,我想要获取所绘制路径的面积大小。

如何获得此路径的实际面积大小?

这些路径(形状)看起来像这样:
链接到图片


我不确定这是否正确,但如果路径使用 RectF 绘制,则可以从中获取区域大小。 - Opiatefuchs
请查看类似的问题[链接](https://dev59.com/CGYq5IYBdhLWcg3wmRsD) - Plato
2个回答

0
我找到了一个解决方案。我从路径生成了一个区域,并使用RegionIterator获取区域内的矩形。通过这些矩形,我可以计算出整个路径的面积。
private void calculateArea(Region region) {

        RegionIterator regionIterator = new RegionIterator(region);

        int size = 0; // amount of Rects
        float area = 0; // units of area

        Rect tmpRect= new Rect(); 

        while (regionIterator.next(tmpRect)) {
            size++;
            area += tmpRect.width() * tmpRect.height();
        }

        log.d("Rect amount=" + size);
        log.d("Area Size / units of area=" + area);
}

6
你能告诉我如何将你的路径转换为区域吗? - Anil
@Anil 这是如何将路径转换为区域的代码:Region region1 = new Region(); RectF rectF1 = new RectF(); tpath.computeBounds(rectF1, true); region1.setPath(tpath, new Region((int) rectF1.left, (int) rectF1.top, (int) rectF1.right, (int) rectF1.bottom)); - Mubashir Murtaza

0

这个简单地展示了如何将路径转换为矩形。

RectF getAreaFromPath(Path sourcePath){
    RectF rectF = new RectF();
    sourcePath.computeBounds(rectF, true);

    //You can get width and height by calling rectF.width(), rectF.height()

    return rectF;
}

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