Android uses glide animated GIF animation to set playback times _android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.