繁体   English   中英

使用Glide控制GIF动画

[英]Controlling GIF animation with Glide

我正在使用Glide将动画GIF动画加载到ImageView 它按预期工作,无限循环:

GlideApp.with(getContext())
            .load(R.raw.my_gif_animation)
            .into(this)

我想在每次GIF动画周期开始(或结束)时增加振动,但是我找不到任何回调,侦听器或帧计数器,可以用来了解动画周期何时开始(或结束)。 JavaKotlin都欢迎回答。

我找到了解决问题的方法,事实证明,加载资源后,我们可以访问GIF动画的帧。 使用正在运行的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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM