2014-03-28 54 views
0

我建立一個鎖屏,我有如下設計:獲取圖像視圖的位置矩形 - Android電子

design

現在紅色和綠色的顏色不會在那裏的中端產品,它只是爲了顯示圖像瀏覽的填充。

我需要得到紅色圓圈的座標在中間(填充設爲25dp)。我有一個上的觸摸的方法,將寄存器按下數字按鈕(1 - 3跨越,4 - 圖6和7 - 9)

這是我的當前代碼:

public void makeRects(){ 
    // row 1 rectangles 
    b1r = new Rect(b1.getLeft(), b1.getTop(), b1.getRight(), b1.getBottom()); 
    b2r = new Rect(b2.getLeft(), b2.getTop(), b2.getRight(), b2.getBottom()); 
    b3r = new Rect(b3.getLeft(), b3.getTop(), b3.getRight(), b3.getBottom()); 

    // row 2 rectangles 
    b4r = new Rect(b4.getLeft(), b4.getTop()+b1.getBottom(), b3.getRight(), b3.getBottom()+b1.getBottom()+b1.getTop()); 
    b5r = new Rect(b5.getLeft(), b5.getTop()+b2.getBottom(), b5.getRight(), b5.getBottom()+b2.getBottom()+b2.getTop()); 
    b6r = new Rect(b6.getLeft(), b6.getTop()+b3.getBottom(), b6.getRight(), b6.getBottom()+b3.getBottom()+b3.getTop()); 

    // row 3 rectangles 
    b7r = new Rect(b7.getLeft(), b7.getTop()+b1.getBottom(), b7.getRight(), b7.getBottom()+b1r.height()); 
    b8r = new Rect(b8.getLeft(), b8.getTop()+b2.getBottom(), b8.getRight(), b8.getBottom()+b2.getBottom()+b2.getTop()); 
    b9r = new Rect(b9.getLeft(), b9.getTop()+b3.getBottom(), b9.getRight(), b9.getBottom()+b3.getBottom()+b3.getTop()); 
} 

然而,它僅註冊數字按鈕位於塊的頂部(紅色或綠色),並記錄整行(1,2,3和4,5,6,7,8,9在第二行)。我試圖只在懸停或拖動每個紅色圖標時才讓它註冊數字按鈕。

我該怎麼做?

+0

行2:爲什麼使用b4.getTop()+ b1.getBottom()?不會b4.getTop()就夠了。 另外,爲什麼你不把所有的bx對象的onTouch處理程序? –

+0

因爲b4.getTop()返回與b1.getTop()相同的最高值。 – user3241507

+0

和因爲那麼它只註冊相對於該視圖的點擊,所以它會在我點擊的位置返回1。 – user3241507

回答

0

你可以是按次一個處理工作:

b1.setOnClickListener(myClickListener); 
b2.setOnClickListener(myClickListener); 
b3.setOnClickListener(myClickListener); 
... 

// And in myClickListener: 

void onClick(View v) { 
    if (v == b1) { 
    // 1 was pressed. 
    } else if (v == b2) { 
    // 2 was pressed. 
    } 
... 
} 

您還可以使用getLocationOnScreengetLeft等方法在父視圖返回的座標。

+0

我需要能夠拖動按鈕,看看訂單是什麼。不只是點擊。我已經試過了。 – user3241507

+0

然後getLocationOnScreen是要走的路。 –