AsyncTask 中初始屏幕的动画序列不起作用



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

此解决方案的问题 ->如果我启动启动画面动画将启动(我检查了它(,但是当我移动到doInBackground()方法时它会立即冻结 ->第二个动画没有被调用。

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

异步任务调用和动画方法:

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. 
}

最新更新