2016-12-01 66 views
0

我用這個來得到一個路徑區域:如何獲得路徑的精確區域在畫布上

RectF rectF = new RectF(); 
Path currentPath = getCurrentPath(); 
currentPath.computeBounds(rectF, true); 
Region region = new Region(); 

這給了我這樣一個矩形:

enter image description here

綠色rect是我可以從圈子中獲得的區域。

我想通過拖動來移動紅圈,所以我需要它的確切邊界而不是這個矩形。

這個圓可以從這個方法的中心移動,但我不想要它。我希望它可以從其確切的紅點移動。

如何確定確切的邊界?

回答

0

我只是通過使用兩個區域來解決這個問題。下面是示例代碼:

首先,讓大圈地區爲clickableRegion:

Region clickableRegion = new Region(); 
RectF rectF = new RectF(); 
// bigCirclePath is the red ring 
Path bigCirclePath = getBigCirclePath(); 
bigCirclePath.computeBounds(rectF, true); 
Rect rect = new Rect((int)rectF.left, (int)rectF.top, (int)rectF.right, (int)rectF.bottom); 
// this will get the circle Region 
clickableRegion.setPath(bigCirclePath, new Region(rect)); 

然後,使用類似的代碼來獲取小圓圈區域爲disClickableRegion。小圓圈應該與你的bigcircle具有相同的中心。

定義contains函數;

public boolean conatins(Point p) { 
    if(disClickableRegion.contains(p.x, p.y)) { 
     return false; 
    } 

    return clickableRegion.contains(p.x, p.y); 
}