2012-09-04 38 views
2

因此,下面從這個問題在這裏:Rounded buttons,我想我需要創建一種視圖邊緣的地圖,可以說我有一個看起來像一個視圖這樣的:創建一個按鈕邊緣的「地圖」

enter image description here

按鈕不會是藍色或任何特定的顏色,所以我們不能檢查,如果該用戶觸摸是藍色的,由最後一個問題的答案的一個建議。

假設我收到一個觸摸事件,並且我在此視圖中獲取了觸摸的位置,該視圖是直接的,我只想在輸入爲藍色的部分時接受輸入。我怎麼弄出來的?

回答

1

我們假設圖形的邊界框完全填充視圖。我們可以按以下步驟進行。感興趣的區域由兩個同心圓和視圖界定。 (圓心位於視圖的右下角。)觸摸時,我們只需計算從觸摸座標到右下角的距離,並將其與兩個圓弧半徑進行比較。 (通過將平方距離與平方的半徑進行比較,可以避免平方根)。如果距離落在半徑之間,則觸摸處於藍色(或任何顏色)。我們不需要計算觸摸是否在邊界框內;我們已經知道,自事件發表以來的觀點。

下面是一些示例代碼。這是一個探測器,用於確定一個點是否位於兩個同心圓(一個圓環)內,如果是,則確定它碰到哪個象限。

public class ArcHitDetector { 
    public enum Quadrant { 
     TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT 
    } 
    private int xCenter, yCenter, innerR2, outerR2; 
    /** 
    * Construct an ArcHitDetector for an annulus centered at given 
    * points and with given inner and outer radii. 
    * 
    * @param xCenter the horizontal center coordinate 
    * @param yCenter the vertical center coordinate 
    * @param outerR the outer radius of the annulus 
    * @param innerR the inner radius of the annulus 
    */ 
    public ArcHitDetector(int xCenter, int yCenter, int outerR, int innerR) { 
     this.xCenter = xCenter; 
     this.yCenter = yCenter; 
     this.outerR2 = outerR * outerR; 
     this.innerR2 = innerR * innerR; 
    } 

    /** 
    * Classify a point with respect to the annulus. It assumes 
    * screen coordinates (x increases to the right; y increases 
    * down). 
    * 
    * @param x the x coordinate of the point to test 
    * @param y the y coordinate of the point to test 
    * 
    * @return the Quadrant of the annulus in which the point falls, 
    * or null if the point does not lie in the annulus 
    */ 
    public Quadrant classifyHit(int x, int y) { 
     int dx = x - xCenter; 
     int dy = y - yCenter; 
     int d2 = dx * dx + dy * dy; 
     if (d2 <= outerR2 && d2 >= innerR2) { 
      if (x >= xCenter) { 
       return y <= yCenter ? TOP_RIGHT : BOTTOM_RIGHT; 
      } else { 
       return y <= yCenter ? TOP_LEFT : BOTTOM_LEFT; 
      } 
     } else { 
      return null; 
     } 
    } 
} 
+0

我們有一些代碼來生成這個嗎?假設弧是「完美的」,但如果它們不是? – FabianCook

+0

@SmartLemon - 我添加了一些示例代碼。一般來說,不完美的弧線並不是太大的問題。只需在計算中添加一點(例如,增加外半徑並減小內半徑)。無論如何,觸摸事件並不是那麼準確。如果曲線與圓圈明顯不同(例如,它們是橢圓),請相應地更改方程(s)。 –

+0

嗯,謝謝,我會解決一些問題,謝謝你的幫助。 – FabianCook

0

我推薦創建代表該形狀頂部和底部曲線的方程,2個顛倒的拋物線,並檢查y是否大於給定x值下方曲線的y方程,以及y是小於在給定x值處的頂部曲線的y(並且y大於0,假設0是底部,並且x小於然後按鈕的距離很遠)。