我知道 SO 上有很多类似的问题,但大多数都使用 MOVIE 类,该类已被弃用。现在 Android 建议对 GIF 使用 AnimatedImageDrawable 类,但它没有提供任何机制来获取 GIF 的持续时间/长度。
如果有任何机制可用于相同的情况,请告诉我。
对我有用的解决方案是使用 github/koral--/android-gif-drawable。
对于放置在
error.gif
中的我的文件 res/drawable
,以下内容提供了正确的预期长度。
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.25'
import pl.droidsonroids.gif.GifDrawable
val gifDrawable = GifDrawable(resources, R.drawable.error)
val duration = gifDrawable.duration