1
我正在嘗試開發一個應用程序,在該應用程序中,我可以將圖像拖動到屏幕中的特定位置,並在其右側位置,現在拖動部分完成,我只需要驗證位置並在圖像位於特定位置時顯示警報將圖像拖動到屏幕上的特定位置並提醒其位置是否正確-Android-
我正在嘗試開發一個應用程序,在該應用程序中,我可以將圖像拖動到屏幕中的特定位置,並在其右側位置,現在拖動部分完成,我只需要驗證位置並在圖像位於特定位置時顯示警報將圖像拖動到屏幕上的特定位置並提醒其位置是否正確-Android-
獲取特定位置的Rect
座標並檢查拖動的項目座標是否與位置衝突Rect coords。您可以使用Rect.contains()
api進行檢查。如果它返回true,則可以顯示警報。
if (locationRect.contains(drag.left, drag.top, drag.right, drag.bottom)) {
// Show Alert dialog
}
我覺得你有onTouch方法在你的代碼..
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
.....
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int)event.getRawX();
int y_cord = (int)event.getRawY();
//if you want the alert when the image enters a square of (10,10) (25,25),(10,25) and (25,10).. then
if(x_cord>=10 && x_cord<=25)
{
if(y_cord>10 && y_cord<25){<-- these cordinates work if the image you are moving is a square of side 15 .. so you can change accordingly..
//alert here
}
}
......
}
這樣做幫助嗎?... – Ronnie 2012-04-24 12:10:30