我试图弄清楚如何检测帧的最后一个索引以及何时是最后一帧(即结束帧)。然后,我想实现将其转到下一个活动。
这是SplashActivity。
但是问题是,我一直都在获取-1
索引,而索引却根本没有增加。
这是我的Glide代码:
Glide.with(this)
.asGif()
.load(R.drawable.bg_splash)
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE)) // Saves no data to cache.
.listener(object : RequestListener<GifDrawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<GifDrawable>?, isFirstResource: Boolean): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
return false
}
override fun onResourceReady(resource: GifDrawable?, model: Any?, target: Target<GifDrawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
resource!!.setLoopCount(1)
resource!!.start()
while (true) {
Timber.d("resource: ${resource.frameIndex} / ${resource.frameCount}")
if (!resource!!.isRunning) {
break
} else {
if (resource.frameIndex == resource.frameCount - 1) {
val intent = Intent(this@SplashActivity, MainActivity::class.java) // Intent 선언
startActivity(intent)
finish()
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out)
}
}
}
return false
}
})
.into(iv_splash)
logcat中的结果是:
resource: -1 / 216
resource: -1 / 216
resource: -1 / 216
resource: -1 / 216
...
我该如何解决这个问题!?
答案 0 :(得分:0)
问题在于,一个thead(主线程)卡在while循环中。
因此,它需要另一个线程来检查当前索引。并让UI线程(主线程)播放。
我的解决方法是:
Glide.with(this)
.asGif()
.load(R.drawable.bg_splash)
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE)) // Saves no data to cache.
.listener(object : RequestListener<GifDrawable> {
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<GifDrawable>?, isFirstResource: Boolean): Boolean {
return false
}
override fun onResourceReady(resource: GifDrawable?, model: Any?, target: Target<GifDrawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
resource!!.setLoopCount(1)
gifFrameCheckThread = Thread(Runnable {
while (true) {
Timber.d("resource: ${resource.frameIndex} / ${resource.frameCount}")
if (resource.frameIndex + 1 == resource.frameCount - 1) {
val intent = Intent(this@SplashActivity, MainActivity::class.java) // Intent 선언
startActivity(intent)
gifFrameCheckThread.join()
finish()
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out)
}
}
})
gifFrameCheckThread.start()
return false
}
})
.into(iv_splash)