user1169775
user1169775

Reputation: 53

How to i set boundaries? android java

How can i set bounds so my image which moves using acclerometer can only move a certain distance? Really need help been trying for a while and getting no where. I have an image moveing with my accelerometer but i want to set boundaries so it only moves within them, example i dont want it to move backwards and only a certain distance to the left and right and forward.

Upvotes: 0

Views: 1217

Answers (1)

Kyle
Kyle

Reputation: 711

Well you have no code here, but a general answer is to simply keep track of the old positions.

for example:

// 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():

Upvotes: 1

Related Questions