-
Notifications
You must be signed in to change notification settings - Fork 6.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Gif loop end listener #3438
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,19 +14,22 @@ | |
import android.graphics.drawable.Drawable; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.VisibleForTesting; | ||
import android.support.graphics.drawable.Animatable2Compat; | ||
import android.view.Gravity; | ||
import com.bumptech.glide.Glide; | ||
import com.bumptech.glide.gifdecoder.GifDecoder; | ||
import com.bumptech.glide.load.Transformation; | ||
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; | ||
import com.bumptech.glide.util.Preconditions; | ||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* An animated {@link android.graphics.drawable.Drawable} that plays the frames of an animated GIF. | ||
*/ | ||
public class GifDrawable extends Drawable implements GifFrameLoader.FrameCallback, | ||
Animatable { | ||
Animatable, Animatable2Compat { | ||
/** | ||
* A constant indicating that an animated drawable should loop continuously. | ||
*/ | ||
|
@@ -76,6 +79,11 @@ public class GifDrawable extends Drawable implements GifFrameLoader.FrameCallbac | |
private Paint paint; | ||
private Rect destRect; | ||
|
||
/** | ||
* Callbacks to notify loop completion of a gif, where the loop count is explicitly specified. | ||
*/ | ||
private List<AnimationCallback> animationCallbacks; | ||
|
||
/** | ||
* Constructor for GifDrawable. | ||
* | ||
|
@@ -351,10 +359,19 @@ public void onFrameReady() { | |
} | ||
|
||
if (maxLoopCount != LOOP_FOREVER && loopCount >= maxLoopCount) { | ||
notifyAnimationEndToListeners(); | ||
stop(); | ||
} | ||
} | ||
|
||
private void notifyAnimationEndToListeners() { | ||
if (animationCallbacks != null) { | ||
for (int i = 0, size = animationCallbacks.size(); i < size; i++) { | ||
animationCallbacks.get(i).onAnimationEnd(this); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public ConstantState getConstantState() { | ||
return state; | ||
|
@@ -390,6 +407,42 @@ public void setLoopCount(int loopCount) { | |
} | ||
} | ||
|
||
/** | ||
* Register callback to listen to GifDrawable animation end event after specific loop count | ||
* set by {@link GifDrawable#setLoopCount(int)}. | ||
* | ||
* Note: This will only be called if the Gif stop because it reaches the loop count. Unregister | ||
* this in onLoadCleared to avoid potential memory leak. | ||
* @see GifDrawable#unregisterAnimationCallback(AnimationCallback). | ||
* | ||
* @param animationCallback Animation callback {@link Animatable2Compat.AnimationCallback}. | ||
*/ | ||
@Override | ||
public void registerAnimationCallback(@NonNull AnimationCallback animationCallback) { | ||
if (animationCallback == null) { | ||
return; | ||
} | ||
if (animationCallbacks == null) { | ||
animationCallbacks = new ArrayList<>(); | ||
} | ||
animationCallbacks.add(animationCallback); | ||
} | ||
|
||
@Override | ||
public boolean unregisterAnimationCallback(@NonNull AnimationCallback animationCallback) { | ||
if (animationCallbacks == null || animationCallback == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto here for animationCallback, I think you can skip the null check with the annotation (or make it a Precondition if you want one). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
return false; | ||
} | ||
return animationCallbacks.remove(animationCallback); | ||
} | ||
|
||
@Override | ||
public void clearAnimationCallbacks() { | ||
if (animationCallbacks != null) { | ||
animationCallbacks.clear(); | ||
} | ||
} | ||
|
||
static final class GifState extends ConstantState { | ||
@VisibleForTesting | ||
final GifFrameLoader frameLoader; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd either skip this check or make it a Precondition. I think with the
@NonNull
annotation you're fine just skipping the check.If you do want a check, I'd prefer a Precondition because it would immediately show the developer where they went wrong, where the existing check will just silently fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed