bumptech / bumptech/glide

Can't restart a running animation

Open
#5,082 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
35k
Forks
6.2k
Avg merge
1d 11h
Merged PRs (30d)
8

Description

**Glide Version**:
4.9.0
4.15.0
4.15.1

**Device/Android Version**:
Google Pixel 5 Android 13

**Issue details / Repro steps / Use case background**:
I'm using glide to load gif pictures.
I have a ViewPager2 holds some fragments which list pictures in RecyclerView,
Say, there're FragmentA ,FragmentB ...

I wanna pause the gif animtion when FragmentA is invisible to user and resume when visible,
so I called GifDrawable.stop() when FragmentA paused, and GifDrawable.start() when FragmentA resumed,
It runs well except there's some blank white flicker when FragmentA resume after start() called.

code as below:
```java
class PreImageView...{

override fun pauseAnim() {
val d = drawable
VLog.d("vvv", "pause Anim111")
if (d is GifDrawable) {
VLog.d("vvv", "pause Anim222 isRunning=${d.isRunning}")
if (d.isRunning)
d.stop()
}
}

override fun resumeAnim() {
val d = drawable
VLog.d("vvv", "resume Anim111")
if (d is GifDrawable) {
VLog.d("vvv", "resume Anim222 isRunning=${d.isRunning}")
d.start()
}
}

fun loadGif(gifUrl: String?, staticUrl: String?, animOnReady: Boolean) {
Glide.with(this)
.asGif()
.load(gifUrl)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(this)
}
}

```

then I try to use GifDrawable.startFromFirstFrame() ,and I got Exception:

```java
class SomeImageView...{

override fun pauseAnim() {
val d = drawable
VLog.d("vvv", "pause Anim111")
if (d is GifDrawable) {
VLog.d("vvv", "pause Anim222 isRunning=${d.isRunning}")
if (d.isRunning)
d.stop()
}
}

fun resumeAnim() {
val d = drawable
VLog.d("vvv", "resume Anim111")
if (d is GifDrawable) {
VLog.d("vvv", "resume Anim222 isRunning=${d.isRunning}")
if (!d.isRunning) {
d.startFromFirstFrame()
}
// d.start()
}
}

fun loadGif(gifUrl: String?, staticUrl: String?, animOnReady: Boolean) {
Glide.with(this)
.asGif()
.load(gifUrl)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(this)
}
}
```
```
vvv: pause Anim111
vvv: pause Anim222 isRunning=true
vvv: resume Anim111
vvv: resume Anim222 isRunning=false

java.lang.IllegalArgumentException: Can't restart a running animation
at com.bumptech.glide.util.Preconditions.checkArgument(Preconditions.java:17)
at com.bumptech.glide.load.resource.gif.GifFrameLoader.setNextStartFromFirstFrame(GifFrameLoader.java:243)
at com.bumptech.glide.load.resource.gif.GifDrawable.startFromFirstFrame(GifDrawable.java:198)
at com.anyue.widget.home.preview.PreImageView.resumeAnim(PreImageView.kt:42)
at com.widget.home.adapter.BaseListWidgetHolder.resumeAnim(ListWidgetHolder.kt:42)
at com.widget.home.adapter.HomeWidgetListAdapter.onBindViewHolder(HomeWidgetListAdapter.kt:103)
at com.widget.home.adapter.HomeWidgetListAdapter.onBindViewHolder(HomeWidgetListAdapter.kt:23)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7337)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6194)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6460)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
```

the RecyclerView Item Layout:

**Layout XML**:
```xml


```

I'v notice the cause as you can see in attachment picture , I just can't figure out why,cause the log and my code say's it's not running。
And if there's any Official API which can be simple used to pause and resume a gif Animation would be gratful !

Contributor guide

Open the contributing guide

Research direction

Start with the reported calls in PreImageView.kt and the stack-trace entry points GifDrawable.startFromFirstFrame() and GifFrameLoader.setNextStartFromFirstFrame(). Reproduce the pause/resume sequence using the provided Glide GIF loading code and RecyclerView setup, then compare the drawable state with the exception. Done means the restart behavior is explained or corrected and a regression test or documented API behavior covers it.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin
Domain
mobile
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.