Through the previous narration, I believe you have a certain understanding of the principle of volley. This chapter will give some examples that we can use directly in the application, the first example being
Networkimageview class, in fact, Networkimageview as the name implies is the asynchronous operation encapsulated in the control itself, this design can fully preserve the portability and maintenance of the control. networkimageview specifies a specific URL by calling Setimageurl:
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); }
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? 0:width; int maxheight = Wrapheight? 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 Imageco Ntainer response, Boolean isimmediate) {//If This is a immediate response that is delivered in Side of a layout//pass does 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; }
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 for the bitmap is the same as the URL for container. Otherwise, a get is passed through an object called Imageloader.
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);Imageloader.get (String requesturl, Imagelistener Imagelistener,
int maxWidth, int maxheight):
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; }Imageloader first step you may be hard to understand why you need to get it yourself from the cache? Requestqueue have done all this for you? In fact, this cache is a Networkimageview custom Cache:imagecache. And this cache is volley to the user to achieve, you can decorate the adapter mode to become your own cache, can also be decorated as diskbasecache.
With the green segment code we can know that when Imageloader returns in Volley, the callback ongetimagesuccess (CacheKey, Response) method. This method is intended to complement the callback Imageloader's own defined Imagelistener callback
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); } }
As you can see, all of these commands occur in the UI thread. Finally, the Imagelistener callback is adopted to implement the picture setting.
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);}}} ; }