2011-11-01 57 views
12

一段時間後,我問了一個問題,看看我是否能找到路徑中的一對特定點;然而,這次我想知道是否有辦法知道路徑中的所有點? (我找不到這樣做的方法,這是不幸的,因爲Java提供了一種方法來做到這一點,而不是Android?)如何在Android中查找路徑中的所有點?

我問這個的原因是因爲我有多個幾何圖形,我想比較點看他們相交的地方。

我明白任何有用的反應

回答

2

如果您已經創建了一個Path這意味着在你的代碼的某些時候,你知道確切的(GEO)點。你爲什麼不把這一點放在ArrayList或類似的東西上?

因此,例如在做之前:

path.lineTo(point.x, point.y); 

你可以這樣做:

yourList.add(point); 
path.lineTo(point.x, point.y); 

,以後你可以從ArrayList你的所有的點。 請注意,您可以利用Enhanced For Loop SyntaxArrayList,其執行速度提高三倍。

PathMeasure pm = new PathMeasure(myPath, false); 
//coordinates will be here 
float aCoordinates[] = {0f, 0f}; 

//get point from the middle 
pm.getPosTan(pm.getLength() * 0.5f, aCoordinates, null); 

或者這樣:

+0

這就是我要做的事情,如果沒有任何選擇。我會將問題留待更多天,如果沒有其他建議,我會接受這個答案。 – StartingGroovy

+0

@StartingGroovy好的,看看我的更新後的帖子,以便利用性能提升,如果你最終選擇這個解決方案。 – Manos

+0

有趣的是,我沒有意識到增強的for循環速度快了3倍:)感謝那些珍聞! – StartingGroovy

24

您可以使用PathMeasure獲得任意點的座標上path.For例如這個簡單的代碼段(即我看到here)在路徑的一半返回點的座標片斷返回FlaotPoint s.That陣列的陣列包括在路徑上的20點的座標:

private FlaotPoint[] getPoints() { 
     FlaotPoint[] pointArray = new FlaotPoint[20]; 
     PathMeasure pm = new PathMeasure(path0, false); 
     float length = pm.getLength(); 
     float distance = 0f; 
     float speed = length/20; 
     int counter = 0; 
     float[] aCoordinates = new float[2]; 

     while ((distance < length) && (counter < 20)) { 
      // get point from the path 
      pm.getPosTan(distance, aCoordinates, null); 
      pointArray[counter] = new FlaotPoint(aCoordinates[0], 
        aCoordinates[1]); 
      counter++; 
      distance = distance + speed; 
     } 

     return pointArray; 
    } 

在上面的代碼中,FlaotPoint是一個封裝的點的座標的類:

class FlaotPoint { 
     float x, y; 

     public FlaotPoint(float x, float y) { 
      this.x = x; 
      this.y = y; 
     } 

     public float getX() { 
      return x; 
     } 

     public float getY() { 
      return y; 
     } 
    } 

參考
stackoverflow
Animating an image using Path and PathMeasure – Android

相關問題