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

时间:2018-09-04 11:07:33

标签: android kotlin android-asynctask android-animation

我要在启动屏幕中运行动画序列,因为启动数据正在加载到应用程序中。在将动画放入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?) {
            }

        })

    }

1 个答案:

答案 0 :(得分:0)

考虑像下面的方法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. 
}