[英]Controlling GIF animation with Glide
I'm using Glide
to load animated GIF animation into ImageView
. 我正在使用
Glide
将动画GIF动画加载到ImageView
。 It works as intended, looping indefinitely: 它按预期工作,无限循环:
GlideApp.with(getContext())
.load(R.raw.my_gif_animation)
.into(this)
I want to add vibration every time the GIF animation cycle starts (or ends), but I couldn't find any callback, listener or frame counter, which I could use to learn when the animation cycle has started (or ended). 我想在每次GIF动画周期开始(或结束)时增加振动,但是我找不到任何回调,侦听器或帧计数器,可以用来了解动画周期何时开始(或结束)。 Answers welcomed both in
Java
and Kotlin
. Java
和Kotlin
都欢迎回答。
I found a solution to the problem, it turns out that we have access to frames of the GIF animation after the resource was loaded. 我找到了解决问题的方法,事实证明,加载资源后,我们可以访问GIF动画的帧。 Using this information inside a running
Thread
, I was able not only to listen to the end/start of the animation but event to adjust very precisely the timing with respect to the frame of the animation. 使用正在运行的
Thread
此信息,我不仅能够侦听动画的结束/开始,而且还能够通过事件非常精确地调整相对于动画帧的时间。
Here is my working code in Kotlin
(in Java
it would be very similar): 这是我在
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.