我的问题是测量路径的表面积。我生成一个随机路径并在画布上绘制它。触摸此克隆路径后,我想获得此绘制路径的区域大小。
如何获得此路径的实际区域大小?
这里的pathes(形状)如下所示:
link to the image
答案 0 :(得分:0)
我找到了解决方案。我从路径生成一个Region并使用RegionIterator来获取Region内的Rects。使用此Rects,我可以计算路径的整个区域。
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);
}
答案 1 :(得分: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;
}