Skip to content

Instantly share code, notes, and snippets.

@Hulzenga
Last active August 29, 2015 13:57
Show Gist options
  • Save Hulzenga/9511528 to your computer and use it in GitHub Desktop.
Save Hulzenga/9511528 to your computer and use it in GitHub Desktop.
private static final long SLEEP_TIME = 16;
private volatile float mTargetX, mTargetY;
private volatile boolean mMoving = false;
@Override
public boolean onTouchEvent(MotionEvent event) {
mTargetX = event.getX();
mTargetY = event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
startMoving();
} else if (
event.getAction() == MotionEvent.ACTION_UP ||
event.getAction() == MotionEvent.ACTION_CANCEL) {
stopMoving();
}
return true;
}
private void startMoving() {
mMoving = true;
new Thread( new Runnable() {
@Override
public void run() {
while (mMoving && !Thread.interrupted()) {
post(new Runnable() {
@Override
public void run() {
//update UI from inside this runnable
}
});
SystemClock.sleep(SLEEP_TIME);
}
}
}).start();
}
private void stopMoving() {
mMoving = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment