Update 2018/9/25
After a long period of use in this code
I found that some phones did not really start running in onResourceReady
. Means that I get isRunning() == false
once onResourceReady
is called.
Solution: Sleeping at the beginning.
@Override
public boolean onResourceReady(final GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
resource.setLoopCount(1);
new Thread(new Runnable() {
@Override
public void run() {
Thread.sleep(200);
while(true) {
if(!resource.isRunning()) {
onGifFinished();//do your stuff
break;
}
}
}
}).start();
return false;
}
Origin
Because the GIF has a delay when playing, I use the thread to monitor the end of the play.
Not a very good but effective way.
Glide.with(this).asGif().load(R.raw.gif)
.apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE)).listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
onGifFinished();//do your stuff
return false;
}
@Override
public boolean onResourceReady(final GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
resource.setLoopCount(1);
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
if(!resource.isRunning()) {
onGifFinished();//do your stuff
break;
}
}
}
}).start();
return false;
}
}).into(iv);