<span id="Label3"></p><p><p><span style="font-size:18px">Through the previous conversation, I believe you have volley have a certain understanding of the Principle. This chapter will give some of our applications can be used directly in the sample, the first sample is</span></p></p><p><p><span style="font-size:18px">The Networkimageview class, in fact, Networkimageview as the name implies that the asynchronous operation is encapsulated in the control itself, so that the design can fully preserve the portability and maintenance of the Control.</span></p></p><p><p><span style="font-size:18px">Networkimageview specifies a detailed URL by calling Setimageurl:</span><br></p></p><p><p><span style="font-size:18px"><span style="font-size:18px"></span></span></p></p><pre name="code" class="java"><pre name="code" class="java">public void Setimageurl (String url, imageloader imageloader) { murl = url; Mimageloader = imageloader; The URL has potentially changed. See if we need to load it. Loadimageifnecessary (false); }</pre></pre><br><pre name="code" class="java">void Loadimageifnecessary (final boolean isinlayoutpass) {int width = getwidth (); int height = getheight (); Boolean wrapwidth = false, wrapheight = false; If (getlayoutparams () = Null) {wrapwidth = Getlayoutparams (). width = = layoutparams.wrap_content; Wrapheight = Getlayoutparams (). height = = layoutparams.wrap_content; }//if The view ' s bounds aren ' t known yet, and this isn't a wrap-content/wrap-content//view, hold off on Loading the Image. Boolean isfullywrapcontent = Wrapwidth && wrapheight; If (width = = 0 && Height = = 0 &&!isfullywrapcontent) {return; }//if the URL to being loaded in this view are empty, cancel any old requests and clear the//currently Loade D Image. If (textutils.isempty (murl)) {if (mimagecontainer! = Null) {mimagecontainer.cancelrequest (); Mimagecontainer = null; } setdefaultimageornull (); Return }//if there was a old request in this view, check if it needs to be canceled. If (mimagecontainer! = null && mimagecontainer.getrequesturl ()! = Null) {if (mimagecontainer.getreques Turl (). equals (murl)) {//if the request is from the same URL, Return. Return } else {//if There is a pre-existing request, cancel it if it ' s fetching a different URL. Mimagecontainer.cancelrequest (); Setdefaultimageornull (); }}//Calculate the max image width/height to use while ignoring wrap_content dimens. int maxWidth = wrapwidth?<p><p></p></p><p><p>0:width; int maxheight = wrapheight?</p></p><p> 0:height; The pre-existing content of this view didn ' t match the current URL. Load the new Image//from the Network. Imagecontainer Newcontainer = mimageloader.get (murl, new Imagelistener () {@Override public void Onerrorresponse (volleyerror error) {if (merrorimageid! = 0) { Setimageresource (merrorimageid); }} @Override public void Onresponse (final Imagecontainer respons e, Boolean Isimmediate) {//If This is a immediate response that is delivered inside of a layou T//pass do not set the image immediately as it would trigger a requestlayout Inside of a Layout. Instead, defer setting the image by posting back To//the main thread. If (isimmediate &&Isinlayoutpass) {post (new Runnable () {@Override public void Run () {onresponse (response, false); } }); Return } if (response.getbitmap () = Null) {setimagebitmap (response.getbitmap ( )); } else if (mdefaultimageid! = 0) {setimageresource (mdefaultimageid); }}}, maxWidth, maxheight); Update the Imagecontainer to be the new bitmap container. Mimagecontainer = newcontainer; }</p></pre><br>It is not difficult for us to see from this logic that the owner of the URL attribute is imagecontainer. volley will return directly when the corresponding URL of the bitmap is the same as the container corresponding Url.<p><p></p></p><p><p>otherwise, a get is passed through an object called Imageloader.</p></p><p><p><span style="font-size:18px"><span style="font-size:18px"></span></span></p></p><pre name="code" class="java">Imagecontainer Newcontainer = mimageloader.get (murl, new Imagelistener () {@Override public void Onerrorresponse (volleyerror error) {if (merrorimageid! = 0) { Setimageresource (merrorimageid); }} @Override public void Onresponse (final Imagecontainer respons e, Boolean Isimmediate) {//If This is a immediate response that is delivered inside of a layou T//pass do not set the image immediately as it would trigger a requestlayout Inside of a Layout. Instead, defer setting the image by posting back To//the main thread. If (isimmediate && isinlayoutpass) {post (new Runnable () { @Override Public void Run () {onresponse (response, false); } }); Return } if (response.getbitmap () = Null) {setimagebitmap (response.getbitmap ( )); } else if (mdefaultimageid! = 0) {setimageresource (mdefaultimageid); }}}, maxWidth, maxheight);</pre>Imageloader.get (String requesturl, Imagelistener imagelistener,<br>int maxWidth, int maxheight):<p><p></p></p><p><p><span style="font-size:18px"><span style="font-size:18px"></span></span></p></p><pre name="code" class="java">Public Imagecontainer get (String requesturl, imagelistener imagelistener, int maxWidth, int Maxheight) { Only fulfill requests this were initiated from the main thread. Throwifnotonmainthread (); Final String CacheKey = Getcachekey (requesturl, maxWidth, maxheight); Try to look up the request in the cache of the remote Images. Bitmap Cachedbitmap = Mcache.getbitmap (cacheKey); If (cachedbitmap! = Null) {//Return the cached bitmap. Imagecontainer container = new Imagecontainer (cachedbitmap, requesturl, null, null); Imagelistener.onresponse (container, true); Return container; }//the bitmap didn't exist in the cache, fetch it! Imagecontainer Imagecontainer = new Imagecontainer (null, requesturl, cacheKey, imagelistener); Update the caller to let them know this they should use the default bitmap. Imagelistener.onresponse (imagecontainer, TRue); Check to see if a request is already in-flight. Batchedimagerequest request = Minflightrequests.get (cacheKey); If (request! = Null) {//if It is, add this request to the list of Listeners. Request.addcontainer (imagecontainer); Return imagecontainer; }//the request is not a already in flight. Send the new request to the network and//track it. request<?> newrequest = new Imagerequest (requesturl, new listener<bitmap> () {<SP An style= "color: #009900;" > @Override public void onresponse (Bitmap Response) {ongetimagesuccess (cacheKey, resp onse); }</span>}, maxWidth, maxheight, config.rgb_565, new Errorlistener () {@Overrid E public void Onerrorresponse (volleyerror error) {ongetimageerror (cacheKey, error); } }); Mrequestqueue.add (newrequest); Minflightrequests.put (cacheKey, new Batchedimagerequest (newrequest, imagecontainer)); Return imagecontainer; }</pre>Imageloader The first step in the beginning you may be very difficult to understand, why do you need to get it from the cache? Requestqueue have done all this for you? In fact, the cache is Networkimageview's own definition of cache:imagecache. The cache is volley to the user, and you can decorate your own cache with the adapter mode and decorate it as a diskbasecache.<p><p></p></p><p><p><span style="font-size:18px"><span style="font-size:18px">With the green segment code we can know that when Imageloader returns in volley, the callback ongetimagesuccess (cacheKey, Response) method.</span></span></p></p><p><p>This method is intended to complement the callback Imageloader custom Imagelistener callback</p></p><p><p><span style="font-size:18px"><span style="font-size:18px"></span></span></p></p><pre name="code" class="java">private void Batchresponse (String cacheKey, batchedimagerequest request) {mbatchedresponses.put (cacheKey, request) ; If we don ' t already has a batch delivery runnable in flight, make a new one. Note that this would be the used to deliver responses to all callers in Mbatchedresponses. if (mrunnable = = Null) {mrunnable = new Runnable () {@Override public void Run () {for (batchedimagerequest bir:mBatchedResponses.values ())} {for (imagecontai NER Container:bir.mContainers) {//If One of the callers in the batched request canceled the Request//after the response is received but before it is delivered, Skip Them. if (container.mlistener = = Null) {continue; } If (bir.geterror ()= = Null) {container.mbitmap = bir.mresponsebitmap; Container.mListener.onResponse (container, false); } else {container.mListener.onErrorResponse (bir.geterror ()); }}} Mbatchedresponses.clear (); Mrunnable = null; } }; Post the Runnable. mhandler.postdelayed (mrunnable, mbatchresponsedelayms); } }</pre><br>Can see that all of these commands occur in the UI Thread. finally, The Imagelistener callback is adopted to implement the picture Setting.<p><p></p></p><p><p></p></p><p><p><span style="font-size:18px"><span style="font-size:18px"></span></span></p></p><pre name="code" class="java"><pre name="code" class="java">public static Imagelistener Getimagelistener (final ImageView view, final int defaultimageresid, final int Errorimageresid) { return new Imagelistener () { @Override public void Onerrorresponse (volleyerror Error) { if (errorimageresid! = 0) { view.setimageresource (errorimageresid); } } @Override public void Onresponse (imagecontainer response, boolean Isimmediate) { if (response.getbitmap ()! = Null) { view.setimagebitmap (response.getbitmap ()); } else if (defaultimageresid! = 0) { View.setimageresource (defaultimageresid);}}} ; }</pre></pre><br><br><br><br><p><p></p></p><p><p><span style="font-size:18px"><br></span></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article blog original Article. blogs, without consent, may not be reproduced.</p></p> <p><p>[Android] Volley source code Analysis (store) Application</p></p></span>
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