I have a really simple custom animation that I am trying to run, and I can’t seem to figure out where I have messed up, though I suspect it has something to do with multithreading.
Basically, when a fling occurs I need to redraw the canvas over and over a bunch of times.
This is part of what I have right now:
gestureDetector = new GestureDetector(NamePickerActivity.this,
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent arg0,
MotionEvent arg1, float arg2, float arg3) {
final long startTime = System.currentTimeMillis();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (System.currentTimeMillis()
- startTime < 2000) {
System.out.println("running...");
// incrementing this causes the draw to
// occur differently
pathCount++;
invalidate();
}
System.out.println("done...");
}
});
thread.run();
return true;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
});
At this point, I’d like to have it continue to redraw for a couple seconds and then be done.
Does anyone know what I’ve done wrong here? If so, can you explain it?
A thread is started with
thread.start(), notthread.run().thread.run()just calls therunmethod sequentially on the main thread.