Why does animation auto stop while onPause

28 Views Asked by At
view.startAnimation(myAnimation)

I set an animation to some view, everything works.

but if the view go to onPause lifecycle (activity、fragment、customView etc..) and go back to onResume

the animation will stop.

Is there any idea to make view animation dont auto stop?

1

There are 1 best solutions below

2
VishV_dev On

Try this,

private long animationPausedTime;

@Override
protected void onPause() {
    super.onPause();
    // Pause the animation and record the paused time
    animationPausedTime = myAnimation.getStartTime() + myAnimation.getDuration() - System.currentTimeMillis();
    view.clearAnimation();
}

@Override
protected void onResume() {
    super.onResume();
    // Resume the animation with remaining time
    myAnimation.setStartTime(System.currentTimeMillis() - myAnimation.getDuration() + animationPausedTime);
    view.startAnimation(myAnimation);
}