Preface
The recently taken over project involves playing and monitoring GIF animations. In the previous version, the processing of GIFs was implemented by H5. Because considering the user experience, the current demand is to native this area and almost go astray on the way! Let’s take a look at the detailed introduction below.
GIF animation processing and monitoring in Android
The first thing I thought of was glide at first, but I thought glide could not control GIFs or monitor them, so I searched for other methods online. I saw that there is a solution to frame the pictures and use frame-by-frame animation to implement them.
I began to doubt the people who gave this solution, and what era they have done is this kind of thankless thing that occupies a lot of user memory! So I took a closer look at Google before I started, and the answer really embarrassed me. Glide can control GIF animations! ! !
On the code: Use glide to load GIF animations
().load().into(openDoorGif);
It can be done in a very simple line of code. The above methods can be loaded with pictures and animations (web links are also available)
Here is how to write GIF animations that can only be loaded:
().load().asGif().into(openDoorGif);
In fact, there is an extra asGIF
However, the playback of the animated image in this way has always been looped, so what is the way to stop! If you haven’t encountered it, don’t say it’s unnecessary. If there is a requirement now, you need to click the button to play GIF to achieve interaction with users? I think you must get this skill, because you will encounter similar needs sooner or later!
The following writing method realizes control of GIF animations
//The status code of the successful sending of the handlerprivate static final int MESSAGE_SUCCESS = 4424; //The parameters (duration) carried by the handler sends the messageprivate int duration; /** * Load the door-opening Gif animation (only play once) * @param view */ public void loadGif(View view){ (this) .load() .diskCacheStrategy() .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, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { // Calculate the animation duration GifDrawable drawable = (GifDrawable) resource; GifDecoder decoder = (); for (int i = 0; i < (); i++) { duration += (i); } //Send a delay message and notify the end of the animation //The following two parameters are int type, remember the statement above (MESSAGE_SUCCESS, duration); return false; } }) //Only load the gif animation once //The parameter here 1 Timely indicates the number of playbacks .into(new GlideDrawableImageViewTarget(openDoorGif, 1)); }
This way, you can perfectly implement GIF control and provide healthy support for the needs I encountered!
Summarize
The above is the entire content of this article. I hope that the content of this article will be of some help to Android developers. If you have any questions, you can leave a message to communicate. Thank you for your support.