Android:如何获取绘制路径的区域大小? [英] Android: How to get the area size of a drawn path?
本文介绍了Android:如何获取绘制路径的区域大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是测量路径的表面积.
我生成一个随机路径并将其绘制在画布上.触摸这个封闭路径后,我想获得这个绘制路径的区域大小.
我怎样才能得到这条路径的实际面积大小?
路径(形状)在此处如下所示:
图片链接
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);
}
这篇关于Android:如何获取绘制路径的区域大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文