I have a few animated objects.
I use a custom class to store their ImageViews and ObjectAnimators
And I have a method for pausing them. It looks pretty simple:
public void pause_animations(View view) {
for (int i = 0; i < num_of_objects; i++) {
objects[i].animator.pause();
}
}
This method being called in 2 cases:
- As a button onClick.
- At some random time.
When I press a button and this code runs - everything works perfectly. All objects stop their moving. But in the second case, objects just freeze and teleport.
UPDATE. I have a touch listener. Every time user touches screen, a method try_to_pause(); runs.
private void try_to_pause() {
if (number_of_touches % touches_to_pause == 0) {
pause_animations(null);
}
}
From the documentation of the Animator#pause() method, another reason may be that you started the animation from a different thread than main (emphasis is mine).