Skip to content

Commit a150301

Browse files
AnwarShahriarsjudd
authored andcommitted
Added listener to notify loop completion of a gif (#3438)
1 parent c03564a commit a150301

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

library/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dependencies {
1313
api project(':third_party:disklrucache')
1414
api project(':annotation')
1515
api "com.android.support:support-fragment:${ANDROID_SUPPORT_VERSION}"
16+
api "com.android.support:animated-vector-drawable:${ANDROID_SUPPORT_VERSION}"
1617
compileOnly "com.android.support:appcompat-v7:${ANDROID_SUPPORT_VERSION}"
1718

1819
if (project.plugins.hasPlugin('net.ltgt.errorprone')) {

library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java

+54-1
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@
1414
import android.graphics.drawable.Drawable;
1515
import android.support.annotation.NonNull;
1616
import android.support.annotation.VisibleForTesting;
17+
import android.support.graphics.drawable.Animatable2Compat;
1718
import android.view.Gravity;
1819
import com.bumptech.glide.Glide;
1920
import com.bumptech.glide.gifdecoder.GifDecoder;
2021
import com.bumptech.glide.load.Transformation;
2122
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
2223
import com.bumptech.glide.util.Preconditions;
2324
import java.nio.ByteBuffer;
25+
import java.util.ArrayList;
26+
import java.util.List;
2427

2528
/**
2629
* An animated {@link android.graphics.drawable.Drawable} that plays the frames of an animated GIF.
2730
*/
2831
public class GifDrawable extends Drawable implements GifFrameLoader.FrameCallback,
29-
Animatable {
32+
Animatable, Animatable2Compat {
3033
/**
3134
* A constant indicating that an animated drawable should loop continuously.
3235
*/
@@ -76,6 +79,11 @@ public class GifDrawable extends Drawable implements GifFrameLoader.FrameCallbac
7679
private Paint paint;
7780
private Rect destRect;
7881

82+
/**
83+
* Callbacks to notify loop completion of a gif, where the loop count is explicitly specified.
84+
*/
85+
private List<AnimationCallback> animationCallbacks;
86+
7987
/**
8088
* Constructor for GifDrawable.
8189
*
@@ -351,10 +359,19 @@ public void onFrameReady() {
351359
}
352360

353361
if (maxLoopCount != LOOP_FOREVER && loopCount >= maxLoopCount) {
362+
notifyAnimationEndToListeners();
354363
stop();
355364
}
356365
}
357366

367+
private void notifyAnimationEndToListeners() {
368+
if (animationCallbacks != null) {
369+
for (int i = 0, size = animationCallbacks.size(); i < size; i++) {
370+
animationCallbacks.get(i).onAnimationEnd(this);
371+
}
372+
}
373+
}
374+
358375
@Override
359376
public ConstantState getConstantState() {
360377
return state;
@@ -390,6 +407,42 @@ public void setLoopCount(int loopCount) {
390407
}
391408
}
392409

410+
/**
411+
* Register callback to listen to GifDrawable animation end event after specific loop count
412+
* set by {@link GifDrawable#setLoopCount(int)}.
413+
*
414+
* Note: This will only be called if the Gif stop because it reaches the loop count. Unregister
415+
* this in onLoadCleared to avoid potential memory leak.
416+
* @see GifDrawable#unregisterAnimationCallback(AnimationCallback).
417+
*
418+
* @param animationCallback Animation callback {@link Animatable2Compat.AnimationCallback}.
419+
*/
420+
@Override
421+
public void registerAnimationCallback(@NonNull AnimationCallback animationCallback) {
422+
if (animationCallback == null) {
423+
return;
424+
}
425+
if (animationCallbacks == null) {
426+
animationCallbacks = new ArrayList<>();
427+
}
428+
animationCallbacks.add(animationCallback);
429+
}
430+
431+
@Override
432+
public boolean unregisterAnimationCallback(@NonNull AnimationCallback animationCallback) {
433+
if (animationCallbacks == null || animationCallback == null) {
434+
return false;
435+
}
436+
return animationCallbacks.remove(animationCallback);
437+
}
438+
439+
@Override
440+
public void clearAnimationCallbacks() {
441+
if (animationCallbacks != null) {
442+
animationCallbacks.clear();
443+
}
444+
}
445+
393446
static final class GifState extends ConstantState {
394447
@VisibleForTesting
395448
final GifFrameLoader frameLoader;

0 commit comments

Comments
 (0)