Android的:如何获得绘制路径的面积大小?如何获得、路径、大小、面积

2023-09-06 18:36:30 作者:残留的回忆不如遗忘

我的问题是测量路径的表面积。 我生成一个随机的路径,并绘制在画布上。触摸此closen道路上,我想这个绘制路径的面积大小。

我怎样才能得到这条道路的实际面积大小?

该pathes(形状)看起来像这样的位置: 链接到图片

My problem is to measure the surface area of a path. I generate a random Path and draw it on a canvas. After touching on this closen path, i want to get the area size of this drawn path.

How can i get the real area size of this path?

The pathes (shapes) looks like this here: link to the image

推荐答案

我找到了解决办法。我生成路径的区域,并使用RegionIterator使区域内Rects。与此Rects我可以计算路径的整个区域。

I found a solution. I generate a Region from the path and use a RegionIterator to get Rects inside the Region. With this Rects i can calculate the whole area of the path.

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);
}
 
精彩推荐
图片推荐