【问题标题】:Animation sequence for SplashScreen inside AsyncTask doesn't workAsyncTask 中 SplashScreen 的动画序列不起作用
【发布时间】:2018-09-04 11:07:33
【问题描述】:

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

这个解决方案的问题 -> 如果我开始启动闪屏动画将开始(我检查过)但是当我移动到 doInBackground() 方法时它立即冻结 -> 不调用第二个动画。

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

AsyncTask 调用和动画方法:

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?) {
            }

        })

    }

【问题讨论】:

    标签: android kotlin android-asynctask android-animation


    【解决方案1】:

    考虑在方法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. 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      相关资源
      最近更新 更多