为什么动画在onPause时自动停止

问题描述 投票:0回答:1
view.startAnimation(myAnimation)

我为某个视图设置了动画,一切正常。

但是如果视图进入

onPause
生命周期(activity、fragment、customView等..)并返回到
onResume

动画将停止。

有没有办法让视图动画自动停止?

android animation lifecycle
1个回答
0
投票

试试这个,

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.