The Universal Tween Engine only requirement to be able to compute anything is that the Tweens
and Timelines
you create are updated regularly (on each frame).
In most situations, you should use a TweenManager
to handle the update process of all your tweens. Therefore, all you need to do is to call the .update()
method of the manager on each frame. For android games, this is really easy to do since every game has a "game loop", an infinite loop which recomputes and redraws the world on each frame. Therefore, all you need to do is to insert the manager update call in this loop, and voilà. However, pure android UI applications do not expose their loop, so you cannot just insert your update call anywhere.
What you should do however is to create a separate thread with an infinite loop inside it, and put the manager.update() method inside it. It should work without a breeze :)
First you need to create a manager somewhere:
private final TweenManager tweenManager = new TweenManager();
As well as a boolean to stop the animations in the onPause() method of your ativity:
private boolean isAnimationRunning = true;
Then you need to create the thread (in the Activity constructor for instance):
new Thread(new Runnable() {
private long lastMillis = -1;
@Override
public void run() {
while (isAnimationRunning) {
if (lastMillis > 0) {
long currentMillis = System.currentTimeMillis();
final float delta = (currentMillis - lastMillis) / 1000f;
view.post(new Runnable() {) {
@Override public void run() {
tweenManager.update(delta);
}
};
lastMillis = currentMillis;
} else {
lastMillis = System.currentTimeMillis();
}
try {
Thread.sleep(1000/60);
} catch(InterruptedException ex) {
}
}
}
}).start();
Then you can create tweens anywhere in your android UI code. The update call is surrounded with a view.post()
as you can see, so the update will be done in the main Android UI thread and therefore you won't have any synchronization issue.
I never tried the engine with anything else than games, but this solution should be working without any problem.