android - 用 Kotlin 可取消的协程扩展替换动画回调(暂停视图)
问题描述
所以我在这里阅读了 Chris Banes 的这篇很棒的 Medium 文章,它解释了如何使用协程或特别挂起函数来协调动画,而不会在将动画链接在一起时陷入“回调地狱”。我设法让onAnimationEnd
监听器扩展按照他的示例工作,但我似乎无法为onAnimationStart
监听器做同样的事情,这是我的awaitEnd
方法
suspend fun Animator.awaitEnd() = suspendCancellableCoroutine<Unit> { continuation ->
continuation.invokeOnCancellation { cancel() }
this.addListener(object : AnimatorListenerAdapter() {
private var endedSuccessfully = true
override fun onAnimationCancel(animation: Animator) {
endedSuccessfully = false
}
override fun onAnimationEnd(animation: Animator) {
animation.removeListener(this)
if (continuation.isActive) {
// If the coroutine is still active...
if (endedSuccessfully) {
// ...and the Animator ended successfully, resume the coroutine
continuation.resume(Unit)
} else {
// ...and the Animator was cancelled, cancel the coroutine too
continuation.cancel()
}
}
}
})
}
在下面的代码中,我在异步块中使用它并首先等待动画完成,然后等待协程完成
val anim = async {
binding.splash.circleReveal(null, startAtX = x, startAtY = y).run {
start()
//...doStuff()
awaitEnd()
}
}
anim.await()
这很好用,将日志添加到正确的函数中向我展示了所有内容都被完全按预期调用,现在以相同的方式添加一个启动的扩展......
suspend fun Animator.started() = suspendCancellableCoroutine<Unit> { continuation ->
continuation.invokeOnCancellation { cancel() }
this.addListener(object : AnimatorListenerAdapter() {
private var endedSuccessfully = true
override fun onAnimationCancel(animation: Animator?) {
endedSuccessfully = false
}
override fun onAnimationStart(animation: Animator?) {
Log.d("DETAIL", "Animator.started() onAnimationStart")
animation?.removeListener(this)
if (continuation.isActive) {
// If the coroutine is still active...
if (endedSuccessfully) {
// ...and the Animator ended successfully, resume the coroutine
continuation.resume(Unit)
} else {
// ...and the Animator was cancelled, cancel the coroutine too
continuation.cancel()
}
}
}
})
}
并在 start 方法之后从同一个异步块调用它(这可能与事情有关)
val anim = async {
binding.splash.circleReveal(null, startAtX = x, startAtY = y).run {
start()
started()
//...doStuff()
awaitEnd()
}
}
anim.await()
现在发生的情况是started的协程被暂停但永远不会恢复,如果我添加一些日志记录语句,我可以看到它调用start()
,然后它调用started()
但不再继续,显然我已经尝试将操作顺序更改为无济于事,谁能看到我在这里做错了什么?
非常感谢
编辑
我也试过这个来添加一个 sharedElementEnter 过渡,但它又对我不起作用,
suspend fun TransitionSet.awaitTransitionEnd() = suspendCancellableCoroutine<Unit> { continuation ->
val listener = object : TransitionListenerAdapter() {
private var endedSuccessfully = true
override fun onTransitionCancel(transition: Transition) {
super.onTransitionCancel(transition)
endedSuccessfully = false
}
override fun onTransitionEnd(transition: Transition) {
super.onTransitionEnd(transition)
Log.d("DETAIL","enterTransition onTransitionEnd")
transition.removeListener(this)
if (continuation.isActive) {
// If the coroutine is still active...
if (endedSuccessfully) {
// ...and the Animator ended successfully, resume the coroutine
continuation.resume(Unit)
} else {
// ...and the Animator was cancelled, cancel the coroutine too
continuation.cancel()
}
}
}
}
continuation.invokeOnCancellation { removeListener(listener) }
this.addListener(listener)
}
并再次尝试使用 await 方法
viewLifecycleOwner.lifecycleScope.launch {
val sharedElementEnterTransitionAsync = async {
sharedElementEnterTransition = TransitionInflater.from(context)
.inflateTransition(R.transition.shared_element_transition)
(sharedElementEnterTransition as TransitionSet).awaitTransitionEnd()
}
sharedElementEnterTransitionAsync.await()
}
解决方案
你是对的 - 协程永远不会恢复。
为什么?
当你调用started()
方法时,动画已经开始了。这意味着onAnimationStart
在监听器中定义的started()
将不会被调用(因为它已经被调用)将底层协程置于永远等待状态。
如果你started()
之前调用也会发生同样的情况start()
:底层协程将等待onAnimationStart
被调用,但它永远不会发生,因为start()
方法调用被started()
方法创建的协程阻塞。
这几乎是一个死锁。
解决方案 1
start()
返回前调用started()
:
suspend fun Animator.started() = suspendCancellableCoroutine<Unit> { continuation ->
continuation.invokeOnCancellation { cancel() }
this.addListener(object : AnimatorListenerAdapter() {
...
})
// Before the coroutine is even returned we should start the animation
start()
}
解决方案 2
传入一个函数(可选地接受一个CancellableContinuation<Unit>
参数):
suspend fun Animator.started(executeBeforeReturn: (CancellableContinuation<Unit>) -> Unit) = suspendCancellableCoroutine<Unit> { continuation ->
continuation.invokeOnCancellation { cancel() }
this.addListener(object : AnimatorListenerAdapter() {
...
})
executeBeforeReturn(continuation)
}
它将允许您:
- 在开始动画之前使用协程(例如取消);
- 避免锁定。
例子:
val anim = async {
ObjectAnimator.ofFloat(view, View.ALPHA, 0f, 1f).run {
started { continuation ->
if (anything) {
continuation.cancel()
}
start()
}
awaitEnd()
}
}
anim.await()
推荐阅读
- reactjs - 在 React 中渲染来自 Firebase Firestore 的数据
- c++ - 如何让这个打印到outfile?(c++)
- android - java.lang.ClassCastException:com.example.readdoang.ui.login_regist_activity.LoginRegistActivity 无法转换为 com
- javascript - 如何使 React 组件可以访问并且需要异步数据才能使用的变量?
- php - 如何修复此错误 Undefined offset: 1 in line 178
- c++ - 预提交时cpp中的memleak
- ruby-on-rails - 如何为 API 版本化模型验证?
- amazon-ec2 - 设置 Github 操作以在 EC2 上构建和部署 java 代码
- java - 为什么我不能从本地存储库导入 maven jar?
- tensorflow - Rasa 训练数据集