Last active
August 29, 2015 13:57
-
-
Save Hulzenga/9511528 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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