Java 用Glide控制GIF动画
我正在使用Java 用Glide控制GIF动画,java,android,kotlin,android-glide,animated-gif,Java,Android,Kotlin,Android Glide,Animated Gif,我正在使用Glide将动画GIF动画加载到ImageView中。它按预期工作,无限循环: GlideApp.with(getContext()) .load(R.raw.my_gif_animation) .into(this) 我想在每次GIF动画循环开始(或结束)时添加振动,但我找不到任何回调、侦听器或帧计数器,我可以使用它们来了解动画循环何时开始(或结束)。在Java和Kotlin中都欢迎答案。我找到了问题的解决方案,结果是在加载资源后,我
Glide
将动画GIF动画加载到ImageView
中。它按预期工作,无限循环:
GlideApp.with(getContext())
.load(R.raw.my_gif_animation)
.into(this)
我想在每次GIF动画循环开始(或结束)时添加振动,但我找不到任何回调、侦听器或帧计数器,我可以使用它们来了解动画循环何时开始(或结束)。在
Java
和Kotlin
中都欢迎答案。我找到了问题的解决方案,结果是在加载资源后,我们可以访问GIF动画的帧。通过在运行的线程中使用此信息,我不仅能够听到动画的结束/开始,而且能够非常精确地调整动画帧的计时
这是我在Kotlin
中的工作代码(在Java
中,它将非常类似):
GlideApp.with(getContext())
.asGif()
.load(R.raw.my\u gif\u动画)
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
.listener(对象:RequestListener{
覆盖加载失败(
e:例外情况?,
型号:任何,
目标:目标,,
isFirstResource:布尔值
):布尔值{
返回错误
}
覆盖资源就绪(
资源:可提取吗?,
型号:任何,
目标:目标,,
数据源:数据源,
isFirstResource:布尔值
):布尔值{
myThread=线程(可运行{
while(true){
if(资源?.isRunning==true){
if(resource.frameIndex==10.toInt()){
//此代码将在每次播放GIF动画的第10帧时执行。。
}
如果(Thread.interrupted())中断
}
}
})
myThread?.start()
返回错误
}
})
.进入(这个)
我找到了问题的解决方案,结果是在加载资源后,我们可以访问GIF动画的帧。通过在运行的线程中使用此信息,我不仅能够听到动画的结束/开始,而且能够非常精确地调整动画帧的计时
这是我在Kotlin
中的工作代码(在Java
中,它将非常类似):
GlideApp.with(getContext())
.asGif()
.load(R.raw.my\u gif\u动画)
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
.listener(对象:RequestListener{
覆盖加载失败(
e:例外情况?,
型号:任何,
目标:目标,,
isFirstResource:布尔值
):布尔值{
返回错误
}
覆盖资源就绪(
资源:可提取吗?,
型号:任何,
目标:目标,,
数据源:数据源,
isFirstResource:布尔值
):布尔值{
myThread=线程(可运行{
while(true){
if(资源?.isRunning==true){
if(resource.frameIndex==10.toInt()){
//此代码将在每次播放GIF动画的第10帧时执行。。
}
如果(Thread.interrupted())中断
}
}
})
myThread?.start()
返回错误
}
})
.进入(这个)
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)