AsyncTask中SplashScreen的动画序列不起作用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AsyncTask中SplashScreen的动画序列不起作用相关的知识,希望对你有一定的参考价值。

我想在启动数据加载到应用程序时在启动画面中运行动画序列。在我将动画放入asyncTask之前,它运行得很好,但在启动数据加载之前不应该运行动画。所以我为此做了asyncTask API调用。在onPreExecute()里面我想开始动画序列,在doInBackground()startupRequest()

这个解决方案的问题 - >如果我开始启动屏幕动画将启动(我检查它)但它立即冻结,因为我移动到doInBackground()方法 - >第二个动画没有被调用。

我甚至试图在runOnUiThread()方法中调用动画(这是无用的,因为onPreExecute()应该在UI线程上运行 - 它适用于ProgressBar或类似的东西)。

AsyncTask调用和Animation方法:

fun splashScreen(splashActivity: Splash){
        class GetSplashAsync: AsyncTask<Void, Void, Boolean>() {

            override fun onPreExecute() {
                createLog("SplashScreen: ", "Starting onPreExecute() --> anim on UIThread")
                splashActivity.runOnUiThread {
                    splashActivity.splashAnimation()
                }
            }

            override fun doInBackground(vararg params: Void?): Boolean {
                return splashActivity.startupRequest()
            }

        }

        GetSplashAsync().execute()
    }


fun splashAnimation(){

        val firstAnim = AnimationUtils.loadAnimation(this, R.anim.splash_first_anim)
        val secondAnim = AnimationUtils.loadAnimation(this, R.anim.splash_second_anim)

        firstAnimImg.visibility = View.INVISIBLE
        secondAnimImg.visibility = View.INVISIBLE

        createLog("SplashScreenAnim: ", "Starting anim 1")
        firstAnimImg.startAnimation(firstAnim)


        firstAnim.setAnimationListener(object: Animation.AnimationListener{
            override fun onAnimationStart(p0: Animation?) {
                firstAnimImg.visibility = View.VISIBLE
            }

            override fun onAnimationRepeat(p0: Animation?) {

            }

            override fun onAnimationEnd(p0: Animation?) {
                createLog("SplashScreenAnim: ", "Starting anim 2")
                secondAnimImg.startAnimation(secondAnim)
            }

        })

        secondAnim.setAnimationListener(object: Animation.AnimationListener{
            override fun onAnimationStart(p0: Animation?) {
                secondAnimImg.visibility = View.VISIBLE
            }

            override fun onAnimationRepeat(p0: Animation?) {

            }

            override fun onAnimationEnd(p0: Animation?) {
            }

        })

    }
答案

考虑在方法splashAnimation()中更改如下所示的执行顺序:

fun splashAnimation(){

    val firstAnim = AnimationUtils.loadAnimation(this, R.anim.splash_first_anim)
    val secondAnim = AnimationUtils.loadAnimation(this, R.anim.splash_second_anim)

    firstAnimImg.visibility = View.INVISIBLE
    secondAnimImg.visibility = View.INVISIBLE

    firstAnim.setAnimationListener(object: Animation.AnimationListener{
        override fun onAnimationStart(p0: Animation?) {
            firstAnimImg.visibility = View.VISIBLE
        }

        override fun onAnimationRepeat(p0: Animation?) {

        }

        override fun onAnimationEnd(p0: Animation?) {
            createLog("SplashScreenAnim: ", "Starting anim 2")
            secondAnimImg.startAnimation(secondAnim)
        }

    })

    secondAnim.setAnimationListener(object: Animation.AnimationListener{
        override fun onAnimationStart(p0: Animation?) {
            secondAnimImg.visibility = View.VISIBLE
        }

        override fun onAnimationRepeat(p0: Animation?) {

        }

        override fun onAnimationEnd(p0: Animation?) {
        }

    })

    createLog("SplashScreenAnim: ", "Starting anim 1")
    firstAnimImg.startAnimation(firstAnim) //Started this animation at last after setting up all animation listener, be sure if you've duration in xml. 
}

以上是关于AsyncTask中SplashScreen的动画序列不起作用的主要内容,如果未能解决你的问题,请参考以下文章

启动屏幕的离子动画

切换活动时冻结动画

Android 12 启动画面-SplashScreen

Android 12 启动画面-SplashScreen

android asynctask导致另一个活动

使用 AsyncTask 进行方形图像旋转的自定义进度对话框