2012-11-11 29 views
0

的我有一個類和我所設定的範圍,象這樣:Touch是的getBounds()中繪製對象

tattoo.setBounds(getWidth()/2 - zoomController - x, getHeight()/2 - zoomController - y, getWidth()/ 2 + zoomController - x, getHeight()/ 2 + zoomController - y); 
tattoo.draw(c); 

我希望能夠執行一個動作時,在這些範圍內的用戶點擊。檢查點擊是否在界限內的最簡單方法是什麼?

回答

2

使用描述可繪製邊界的Rect,並調用contains(int x, int y)方法,傳入x和y觸摸座標。例如:

Rect bounds = tattoo.getBounds(); 
int x = ... 
int y = ... 
boolean withinBounds = bounds.contains(x, y); 

或者,你可以創建自己的(靜態)輔助方法;數學真的很基礎。 :)