When you use the glide to load an animated GIF, sometimes you need to set the number of plays, and then play it once or several times, you need to do some other action after playing, directly looking at the code:
Glide.with (this). Load (R.drawable.xiaoguo). Diskcachestrategy (Diskcachestrategy.source). Listener (New Requestlistener<integer, glidedrawable> () {@Override public boolean onexception (Exception
arg0, Integer arg1, Target<glidedrawable> arg2, Boolean Arg3) {return false; @Override public boolean Onresourceready (glidedrawable resource, Integer model, Targe
T<glidedrawable> Target, Boolean Isfrommemorycache, Boolean Isfirstresource) {//Calculate animation length
Gifdrawable drawable = (gifdrawable) resource;
Gifdecoder decoder = Drawable.getdecoder ();
for (int i = 0; i < Drawable.getframecount (); i++) {duration + = Decoder.getdelay (i);
//Send delay message, notify animation end Handler.sendemptymessagedelayed (message_success, duration);
return false; })//Only one GIF animation is loaded. into (new Glidedrawableimageviewtarget (ImageView, 1));
Glide Simple Load Animation usage:
Copy Code code as follows:
Glide.with (mainactivity.this). Load (URL). Asgif (). into (ImageView);
If you add. Asgif (), you can load the GIF file only, and if not, either load the picture or load the. gif.
Troubleshoot loading GIF files in a very slow situation:
Add caching policy to it
Copy Code code as follows:
Glide.with (mainactivity.this). Load (URL). Asgif (). Diskcachestrategy (Diskcachestrategy.source). into (ImageView);
Where the cache policy can be: source and none,none and not cached, source cache prototype. If it's all and result
Diskcachestrategy.none doesn't cache anything.
The Diskcachestrategy.source only caches the original full resolution image.
Diskcachestrategy.result only caches the final image, that is, after the resolution is lowered (or converted)
Diskcachestrategy.all cache All versions of images (default behavior)
Set GIF playback times:
This will be an error:
Glide.with (mainactivity.this). Load (URL). Asgif (). into (New Glidedrawableimageviewtarget (ImageView, 1));
When you set the number of times to play and listen, you should not add. Asgif ()
Glide.with (mainactivity.this). Load (URL). into (New Glidedrawableimageviewtarget (ImageView, 1));
New Glidedrawableimageviewtarget (ImageView, 1), IMageview is the control,1 is the playback times
To set playback listening:
Add Listener ()
Listener (new Requestlistener<integer, glidedrawable> () {@Override//Loaded Wrong public boolean onexception (Exception arg0, Integer arg1, target<glidedrawable> arg2, Boole
A arg3) {return false; @Override//Load Complete public boolean Onresourceready (glidedrawable resource, Integer model, Target<glidedrawable> Target, Boolean Isfrommemorycache, Boolean Isfirstresource) {//Calculation
Animation time Long gifdrawable drawable = (gifdrawable) resource;
Gifdecoder decoder = Drawable.getdecoder ();
for (int i = 0; i < Drawable.getframecount (); i++) {duration + = Decoder.getdelay (i);
//Send delay message, notify animation end Handler.sendemptymessagedelayed (message_success, duration);
return false; }
})
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.