From d6e91fe84e6138224eda4937ea26bb8f3beed864 Mon Sep 17 00:00:00 2001 From: rejigtian <875283604@qq.com> Date: Fri, 21 Apr 2023 17:59:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Ddetach=E4=BB=A5=E5=90=8E?= =?UTF-8?q?=E7=AB=8B=E5=8D=B3attach=E5=AF=BC=E8=87=B4=E7=9A=84=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/qgame/animplayer/AnimView.kt | 45 ++++++++++++++++--- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimView.kt b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimView.kt index 8729dcf0..153fbb84 100644 --- a/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimView.kt +++ b/Android/PlayerProj/animplayer/src/main/java/com/tencent/qgame/animplayer/AnimView.kt @@ -47,6 +47,12 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute companion object { private const val TAG = "${Constant.TAG}.AnimView" } + + //标记正在销毁的状态 + private var isDetaching: Boolean = false + + //标记detach后立即attach的状态 + private var delayAttach: Boolean = false private lateinit var player: AnimPlayer private val uiHandler by lazy { Handler(Looper.getMainLooper()) } @@ -74,12 +80,12 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute } override fun onVideoComplete() { - hide() + hide(false) animListener?.onVideoComplete() } override fun onVideoDestroy() { - hide() + hide(true) animListener?.onVideoDestroy() } @@ -106,7 +112,7 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute init { - hide() + hide(false) player = AnimPlayer(this) player.animListener = animProxyListener } @@ -137,9 +143,9 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute ALog.i(TAG, "onSurfaceTextureDestroyed") this.surface = null player.onSurfaceTextureDestroyed() + innerTextureView?.surfaceTextureListener = null + innerTextureView = null uiHandler.post { - innerTextureView?.surfaceTextureListener = null - innerTextureView = null removeAllViews() } return true @@ -166,8 +172,16 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute override fun onAttachedToWindow() { ALog.i(TAG, "onAttachedToWindow") super.onAttachedToWindow() + if (isDetaching) { + delayAttach = true + return + } + restart() + } + + private fun restart() { player.isDetachedFromWindow = false - // 自动恢复播放 + /* 自动恢复播放 */ if (player.playLoop > 0) { lastFile?.apply { startPlay(this) @@ -176,12 +190,22 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute } override fun onDetachedFromWindow() { + isDetaching = true ALog.i(TAG, "onDetachedFromWindow") super.onDetachedFromWindow() player.isDetachedFromWindow = true player.onSurfaceTextureDestroyed() } + private fun checkDetaching() { + if (isDetaching) { + isDetaching = false + if (delayAttach) { + restart() + } + } + } + override fun setAnimListener(animListener: IAnimListener?) { this.animListener = animListener @@ -275,6 +299,10 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute ALog.e(TAG, "AnimView is GONE, can't play") return@ui } + if (isDetaching) { + ALog.e(TAG, "AnimView is detaching, can't play") + return@ui + } if (!player.isRunning()) { lastFile = fileContainer player.startPlay(fileContainer) @@ -297,10 +325,13 @@ open class AnimView @JvmOverloads constructor(context: Context, attrs: Attribute return scaleTypeUtil.getRealSize() } - private fun hide() { + private fun hide(isDestroy: Boolean) { lastFile?.close() ui { removeAllViews() + if (isDestroy) { + checkDetaching() + } } }