I am trying to avoid garbage creation by reusing runnable. However, I am stuck on how to implement this correctly.
Here is my runnable wrapper class:
public class RotateRunnable implements Runnable {
Maze maze;
int i;
int steps;
int originalAngle;
int dir;
public RotateRunnable(Maze maze) {
this.maze = maze;
}
public void setRunnable(int i, int steps, int originalAngle, int dir) {
this.i = i;
this.steps = steps;
this.originalAngle = originalAngle;
this.dir = dir;
}
@Override
public void run() {
maze.setIsRotateRunning(true);
maze.setAngle(originalAngle + dir*(90*(i+1))/steps);
maze.rotateStep();
}
}
And here is where I have implemented it:
private void smoothAnimateRotate(final int i, final int steps, final int originalAngle, final int dir) {
// handler.postDelayed(new Runnable() {
// @Override
// public void run() {
// isRotateRunning = true;
// angle = originalAngle + dir*(90*(i+1))/steps;
// rotateStep();
// }
// }, 25 * (i));
rotateRunnable.setRunnable(i, steps, originalAngle, dir);
handler.postDelayed(rotateRunnable, 25 * i);
}
The commented out section is working code. The problem I am running into is setting the variables inside rotateRunnable
before postDelay()
is called. Right now that is not working as rotateRunnable
is likely only executing the last variables that are set. This is because smoothAnimateRotate()
is executed inside a for-loop which updates the variables very quickly.
How do I make setting the variables part of the run
method so that when run
is executed later, it is executing with the correctly set variables?