2012-03-23 51 views
0

如何設置邊界,以便使用acclerometer移動的圖像只能移動一定的距離?真的需要幫助一直嘗試一段時間,沒有得到任何地方。我有一個圖像與我的加速度計一起移動,但我想設置邊界,因此它只能在它們內部移動,例如我不希望它向後移動,並且只向左右兩側移動一定距離。如何設置邊界? android java

+0

可能重複的[?加速度計圖像helpp](http://stackoverflow.com/questions/9843833/accelerometer-image-helpp) – 2012-03-23 19:07:01

回答

1

那麼你在這裏沒有代碼,但一般的答案是簡單地跟蹤舊的位置。

例如:

// view is the view you are working with 

int currentLeft = view.getLeft(); 
int currentTop = view.getTop(): 

// get new desired positions here using accelerometer, you must insert your code. 
int newLeft; 
int newTop; 

// do any checks you want here, for example, I am only going to allow moving right 
if (newLeft >= currentLeft){ 
// move the view and do whatever you want 
}else{ 
// don't allow movement and do what you want here 
} 

// update new positions 
int currentLeft = view.getLeft(); 
int currentTop = view.getTop():