1 回答
TA贡献1785条经验 获得超8个赞
我找到了解决问题的方法,事实证明我们可以在资源加载后访问 GIF 动画的帧。在 running 中使用此信息Thread,我不仅能够听到动画的结束/开始,而且还能根据动画帧非常精确地调整时间。
这是我的工作代码Kotlin(其中Java非常相似):
GlideApp.with(getContext())
.asGif()
.load(R.raw. my_gif_animation)
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
.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 {
myThread = Thread(Runnable {
while (true) {
if (resource?.isRunning == true) {
if (resource.frameIndex == 10).toInt()) {
// This code will be executed every time the 10th frame of the GIF animation is played..
}
if (Thread.interrupted()) break
}
}
})
myThread?.start()
return false
}
})
.into(this)
添加回答
举报