<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mark Cheng on Medium]]></title>
        <description><![CDATA[Stories by Mark Cheng on Medium]]></description>
        <link>https://medium.com/@mr.johnnyne?source=rss-adeb551aecaf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RP5kl-STd-nfuOGcwp0U7w.jpeg</url>
            <title>Stories by Mark Cheng on Medium</title>
            <link>https://medium.com/@mr.johnnyne?source=rss-adeb551aecaf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 02:38:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mr.johnnyne/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to use Glide v4 load Image with loading progress update]]></title>
            <link>https://medium.com/@mr.johnnyne/how-to-use-glide-v4-load-image-with-progress-update-eb02671dac18?source=rss-adeb551aecaf------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb02671dac18</guid>
            <category><![CDATA[glide-progress]]></category>
            <category><![CDATA[okhttp]]></category>
            <category><![CDATA[glide]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[glide-image-progress]]></category>
            <dc:creator><![CDATA[Mark Cheng]]></dc:creator>
            <pubDate>Thu, 25 Jan 2018 02:45:45 GMT</pubDate>
            <atom:updated>2019-12-12T09:37:56.220Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*f82fDTCyMUyRTePMC3xuCQ.png" /></figure><h3><strong>Goal</strong></h3><p>If your app wants to load a lot of images or a large image, it is important to give a good user experience with the progress update.</p><p>I will explain how to use <a href="http://bumptech.github.io/glide/"><strong>Glide v4</strong></a> with <a href="http://square.github.io/okhttp/"><strong>OkHttp</strong></a> to achieve the target in this article.</p><p>At the end of the article, you only need to follow <strong>3 steps</strong> and you can have progress update feature of image downloading.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*cQTI2P3GtuxAbzxC-6jsCg.gif" /></figure><p><strong>The main idea is the following:</strong></p><blockquote>Step 1: Using OkHttp to capture the internet transmission progress.</blockquote><blockquote>Step 2: Register listener callback to notify progress view progress status.</blockquote><blockquote>Step 3: Handle Glide v4 life cycle from start, loading, finish.</blockquote><p><strong>Here is my customize ProgressAppGlideModule component</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*aLsyL4HAVOiuIEBsW8YyCQ.jpeg" /></figure><p><strong>OkHttpClinet compnent</strong></p><blockquote>register Glide OkHttpClient with our OkHttpProgressResponseBody which responses for trigger download progress</blockquote><p>Glide v4 provide options to use <a href="http://bumptech.github.io/glide/int/about.html"><strong>integration library</strong></a> such OkHttp or Volley rather than default setting. Here, we rewire OkHttp request body to get the transmission progress.</p><pre>    private Source source(Source source) {<br>        return new ForwardingSource(source) {<br>            long totalBytesRead = 0L;<br><br>            @Override<br>            public long read(Buffer sink, long byteCount) <br>            throws IOException <br>            {<br>                long bytesRead = super.read(sink, byteCount);<br>                long fullLength = responseBody.contentLength();<br>                if (bytesRead == -1) { // this source is exhausted<br>                    totalBytesRead = fullLength;<br>                } else {<br>                    totalBytesRead += bytesRead;<br>                }<br>               <strong>// update listener callback</strong><br>               progressListener.update(url, <br>                                       totalBytesRead, <br>                                       fullLength);<br>                return bytesRead;<br>            }<br>        };<br>    }<br></pre><p>Register OkHttp Client as Our Glide v4 network library.</p><pre>  OkHttpClient client = new OkHttpClient.Builder()<br>                          .addNetworkInterceptor(new Interceptor() {</pre><pre>  @Override<br>  public Response intercept(Chain chain) throws IOException {</pre><pre>    Request request = chain.request();<br>    Response response = chain.proceed(request);<br>    <strong>// initial listener</strong><br>    ResponseProgressListener listener = new    <br>                                    DispatchingProgressListener();<br>    <strong>// replace with our customize OkhttpBody</strong><br>    return response.newBuilder()<br>           .body(new OkHttpProgressResponseBody(request.url(), <br>                                                response.body(),<br>                                                listener))<br>                                                .build();<br>  }<br>}).build();</pre><p><strong>DispatchingProgressListener</strong></p><blockquote>responses for handle add/remove URL listener and trigger ProgressView update</blockquote><pre><strong>//responses for HTTP progress update</strong><br>private interface ResponseProgressListener {<br>    void update(HttpUrl url, long bytesRead, long contentLength);<br>}</pre><pre><strong>//response for UI View progress handle</strong><br>public interface UIonProgressListener {<br>    void onProgress(long bytesRead, long expectedLength);<br><em><br>    </em>float getGranualityPercentage();<br>}</pre><p>public method to add/remove URL listener</p><pre><strong>// remove URL listener when finished or failed</strong><br>public static void forget(String url) {<br>    DispatchingProgressListener.<em>forget</em>(url);<br>}</pre><pre><strong>// add URL listener</strong><br>public static void expect(String url, <br>                          UIonProgressListener listener) {<br>    DispatchingProgressListener.<em>expect</em>(url, listener);<br>}</pre><p>Dispatch progress</p><pre>    @Override<br>    public void update(HttpUrl url, <br>                       final long bytesRead, <br>                       final long contentLength) {<br><br>        String key = url.toString();</pre><pre>        <strong>//Find listener via Key(URL)</strong><br>        final UIonProgressListener listener = <em>LISTENERS</em>.get(key);<br>        if (listener == null) {<br>            return;<br>        }<br>        if (contentLength &lt;= bytesRead) {<br>            <em>forget</em>(key);<br>        }</pre><pre>        if (needsDispatch(key, bytesRead, contentLength, listener.getGranualityPercentage())) {<br>            handler.post(new Runnable() {<br>                @Override<br>                public void run() {<br>                    listener.onProgress(bytesRead, contentLength);<br>                }<br>            });<br>        }<br>    }</pre><p>This URL listener only update view when the progress don’t reach 100% and have added URL listener before.</p><pre>private boolean needsDispatch(String key, <br>                              long current, <br>                              long total, float granularity) {<br>        if (granularity == 0 || current == 0 || total == current) {<br>            return true;<br>        }<br>        float percent = 100f * current / total;<br>        long currentProgress = (long) (percent / granularity);<br>        Long lastProgress = <em>PROGRESSES</em>.get(key);<br>        if (lastProgress == null || currentProgress != lastProgress)     <br>        {<br>            <em>PROGRESSES</em>.put(key, currentProgress);<br>            return true;<br>        } else {<br>            return false;<br>        }<br>    }</pre><p>Here is my <strong>GlideImageLoader </strong>flow</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/781/1*ZP2xeN72V-VVyvSP8EPpqA.jpeg" /></figure><ol><li>onConnecting : set YOUR.progress view to be VISIBLE</li><li>Set UIonProgressListener: add URL listener callback</li><li>Use Glide with options: You can change any Glide Options you want</li><li>You can have a coffee and wait for magic.</li></ol><p>Complete ProgressAppGlideModule</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/35852a62cb4a8d3ab3d8e4220bcb7f06/href">https://medium.com/media/35852a62cb4a8d3ab3d8e4220bcb7f06/href</a></iframe><p>Write YOUR Glide Loader</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/609c5b039aaf1ae8e14677cd0f9e75be/href">https://medium.com/media/609c5b039aaf1ae8e14677cd0f9e75be/href</a></iframe><h3>How to implement with 3 Steps:</h3><ol><li>Build.gradle</li></ol><pre>//Glide<br>implementation &#39;com.github.bumptech.glide:glide:4.4.0&#39;<br>annotationProcessor &#39;com.github.bumptech.glide:compiler:4.4.0&#39;<br>implementation &#39;com.github.bumptech.glide:okhttp3-integration:4.4.0&#39;</pre><p>2. Clone GlideImageLoader.java &amp; ProgressAppGlideModule.java into Your Project</p><p>3. Simple Use anywhere</p><pre>RequestOptions options = new RequestOptions()<br>                    .centerCrop()<br>                    .placeholder(R.drawable.placeholder)<br>                    .error(R.drawable.<em>ic_pic_error</em>)<br>                    .priority(Priority.<em>HIGH</em>);<br><br>new GlideImageLoader(YOUR.imageView,  <br>                     YOUR.progressBar).load(url,options);</pre><p>That is it !!!</p><p>Hope you guys will enjoy this.</p><p>Reference:</p><ol><li><a href="http://bumptech.github.io/glide/">http://bumptech.github.io/glide/</a></li><li><a href="http://square.github.io/okhttp/">http://square.github.io/okhttp/</a></li><li><a href="https://github.com/bumptech/glide/issues/232">https://github.com/bumptech/glide/issues/232</a></li><li><a href="https://gist.github.com/TWiStErRob/08d5807e396740e52c90">https://gist.github.com/TWiStErRob/08d5807e396740e52c90</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb02671dac18" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>