Glide realizes the effect of loading pictures and displaying progress bars

Let's take a look at the renderings:

Glide, as a picture loading library that has just become popular in recent years, has very powerful functions. I believe many people have begun to use it in the project. There are many online tutorials on glide. Recently, to realize the loading of picture progress bar in the project, I haven't seen any ready-made ones on the Internet, so I want to study them myself.

use

thinking

The bottom layer of glide's image download uses okhttp, which has been implemented. In order to load the progress bar, you must know the progress of image download and write down the image yourself. But does glide support it? I checked it on the Internet and found that there is a way

This method can specify the image request loader. We create a progressmodelloader class to implement the streammodelloader interface

Rewrite the getresourcefetcher method, which returns a datafetcher class, which is a data extraction class and an interface. Rewrite its loaddata method to download pictures. Let's take a look at the rewriting of the loaddata method by the progressdatafetcher I created

Download pictures using okhttp and add an interceptor

Rewrite the intercept method, create a progressresponsebody, get the picture download progress, and take a look at the method of reading the stream

Pass the read bytesread and ResponseBody. Contentlength () to the callback method progresslistener. Progress to calculate the progress.

This is the end of the general implementation logic. I want to see the steps of the complete code to https://github.com/chenpengfei88/GlideLoadImageProgress Or you can download it locally: http://xiazai.jb51.net/201705/yuanma/GlideLoadImageProgress (jb51.net).rar

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>