2012-03-23 89 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():