如何设置界限? android java

时间:2012-03-23 18:53:20

标签: android

我如何设置边界,以便使用acclerometer移动的图像只能移动一定距离?真的需要帮助已经尝试了一段时间,没有在哪里。我有一个用我的加速度计移动的图像,但我想设置边界,所以它只在它们内移动,例如我不希望它向后移动,只有一定距离向左,向右和向前。

1 个答案:

答案 0 :(得分: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():