简体   繁体   English

旋转后如何还原正在运行的动画

[英]How to restore running animation after rotation

I have some view states like StartingGame and GameStarted . 我有一些视图状态,例如StartingGameGameStarted If the user starts a game, I emit both states one after another and start a progress in the first event like following: 如果用户开始游戏,我会依次发出两个状态,并在第一个事件中开始进度,如下所示:

var d = Observable.interval(1000L, TimeUnit.MILLISECONDS)
            .takeWhile({ t -> t <= 45 })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                 // update a progress view that shows the remaining time of this turn...
            }

After rotation, my view state says, that it already has handled the start of the animation, so the animation is not shown. 旋转后,我的视图状态说,它已经处理了动画的开始,因此不显示动画。

How could I solve this in a mvi way? 我如何以MVI方式解决此问题?

Should I really emit all those events through the mvi state stream and increase the progress in my state on every event and update the view there? 我真的应该通过mvi状态流发出所有这些事件,并增加每个事件的状态进度并在那里更新视图吗?

Should I really emit all those events through the mvi state stream and increase the progress in my state on every event and update the view there? 我真的应该通过mvi状态流发出所有这些事件,并增加每个事件的状态进度并在那里更新视图吗?

Yes you should. 是的你应该。 It makes sense to do this and makes your app so much more testable. 这样做很有意义,并使您的应用程序更具可测试性。

You know how long the progress is running right (4500 milliseconds). 您知道进度运行正确的时间(4500毫秒)。 You can make this part of the state 您可以将这部分状态

state = {
   ...
   animationRun = 90 ms,
   totalAnimationTime = 4500 ms
}

and in your Observable.interval() stream you basically increase animationRun . 在您的Observable.interval()流中,基本上可以增加animationRun

Then do the following: 然后执行以下操作:

  1. Your screen starts with: 屏幕开始于:

    state = { ... animationRun = 0 ms, totalAnimationTime = 4500 ms } 状态= {... animationRun = 0毫秒,totalAnimationTime = 4500毫秒}

    just start an animation for 4500ms 刚开始播放4500ms的动画

  2. After rotating the screen: You know the last state like 旋转屏幕后:您知道最后一个状态,例如

    state = { ... animationRun = 1000 ms, totalAnimationTime = 4500 ms } 状态= {... animationRun = 1000毫秒,totalAnimationTime = 4500毫秒}

    Then you know that your animation must start with progress "1000" and run for 3500ms (3500 + 1000 = 4500) 然后,您知道动画必须以进度“ 1000”开始并运行3500ms(3500 + 1000 = 4500)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM