Android Open Source Framework Universal-image-loader full parsing (iii)---source code interpretation

Source: Internet
Author: User

Reprint please indicate this article from Xiaanming's blog (, please respect others ' hard work results, thank you!

This article is mainly to take you from the source of the angle above to read this powerful picture loading frame, oneself a long time did not write the article, feel unfamiliar a lot, distance on an article three months more, really is oneself usually busy, change the work many things to see to understand, and then add oneself also lazy, no before so have passion , I feel this rhythm is not right, I want to continue to maintain the previous passion, is the so-called good memory is inferior to the written, sometimes I will go to look at before writing things, I think the knowledge written down than in the mind retained longer, today to everyone to read the framework of the source, I feel this picture loading framework is really good, After reading the code, I learned a lot. I hope that we can first look at the Android Open source framework Universal-image-loader full parsing (a)---basic introduction and use, Android Open source framework Universal-image-loader full Parse (ii)--- Picture caching strategy in detail, I hope that you can insist on watching, after watching you absolutely have a harvest.

ImageView Mimageview = (ImageView) Findviewbyid (;            String IMAGEURL = " 252520photographer.jpg ";                        Displays the configuration of the picture            displayimageoptions options = new Displayimageoptions.builder ()                    . showimageonloading ( r.drawable.ic_stub)                    . Showimageonfail (r.drawable.ic_error).                    Cacheinmemory (True).                    Cacheondisk (True)                    . Bitmapconfig (Bitmap.Config.RGB_565)                    . Build ();                        Imageloader.getinstance (). DisplayImage (ImageUrl, Mimageview, Options);    

Most of the time we are using the above code to load the image, we first look at

public void DisplayImage (String uri, ImageView ImageView, displayimageoptions options) {          displayimage (URI, new Imageviewaware (ImageView), options, NULL, NULL);      }  

  From the above code, we can see that it will convert ImageView into Imageviewaware, imageviewaware What is the main thing to do? The class is mainly to ImageView to a wrapper, will imageview strong references into weak references, when the memory is not enough, you can better reclaim ImageView objects, there is to get imageview width and height. This allows us to crop the image according to ImageView's wide height, reducing the use of memory.

Next look at the specific DisplayImage method, because this method code is quite a lot of, so here I separate to read

checkconfiguration ();          if (Imageaware = = null) {throw new illegalargumentexception (error_wrong_arguments);          } if (listener = = NULL) {listener = Emptylistener;          } if (options = = null) {options = configuration.defaultdisplayimageoptions;              } if (Textutils.isempty (URI)) {engine.canceldisplaytaskfor (imageaware);              Listener.onloadingstarted (URI, Imageaware.getwrappedview ()); if (Options.shouldshowimageforemptyuri ()) {imageaware.setimagedrawable (Options.getimageforemptyuri (confi              guration.resources));              } else {imageaware.setimagedrawable (null);              } listener.onloadingcomplete (URI, Imageaware.getwrappedview (), NULL);          Return }  

  The 1th line of code is to check whether Imageloaderconfiguration is initialized, which is done in application

12-21 line is mainly for the time when the URL is empty, the 13th line of code, there is a hashmap in the Imageloaderengine, used to record the task being loaded, Loading the image will add the ImageView ID and the URL of the image to the HashMap, which will be removed after the loading is complete. Then the picture of the Displayimageoptions Imageresforemptyuri is set to ImageView, and the final callback to the Imageloadinglistener interface tells it that the task is complete.

ImageSize targetsize = Imagesizeutils.definetargetsizeforview (Imageaware, Configuration.getmaximagesize ());      String Memorycachekey = Memorycacheutils.generatekey (URI, targetsize);        Engine.preparedisplaytaskfor (Imageaware, Memorycachekey);        Listener.onloadingstarted (URI, Imageaware.getwrappedview ());      Bitmap bmp = Configuration.memoryCache.get (Memorycachekey);            if (BMP! = null &&!bmp.isrecycled ()) {L.D (Log_load_image_from_memory_cache, Memorycachekey); if (options.shouldpostprocess ()) {Imageloadinginfo imageloadinginfo = new Imageloadinginfo (URI, Imageaware              , Targetsize, Memorycachekey, Options, Listener, Progresslistener, Engine.getlockforuri (URI));                      Processanddisplayimagetask displaytask = new Processanddisplayimagetask (engine, BMP, Imageloadinginfo,              Definehandler (options));             if (options.issyncloading ()) { (); } else {engine.submit (displaytask);              }} else {Options.getdisplayer (). Display (BMP, Imageaware, Loadedfrom.memory_cache);          Listener.onloadingcomplete (URI, Imageaware.getwrappedview (), BMP);   }      }

  The 1th line is mainly to imageview the width of the package into ImageSize objects, if the imageview width of 0, will use the width of the phone screen as the width of the ImageView, we use the Listview,gridview to load the picture, The first page gets the width of 0, so the screen of the phone used on the first page is wide, and the rear gets the size of the control itself.

Line 7th Gets the bitmap object from the memory cache, and we can then configure the memory cache logic in Imageloaderconfiguration, which is used by default Lrumemorycache, which I said in the previous article

There is a judgment in line 11th that we enter the true logic if we set the Postprocessor in Displayimageoptions, but the default postprocessor is null, Bitmapprocessor interface is mainly for the processing of bitmap, this framework does not give a corresponding implementation, if we have their own needs can be implemented Bitmapprocessor interface (such as the picture is set to a circle)

第22-23 line is set bitmap to ImageView above, here we can configure the display requirements Displayer in Displayimageoptions, the default is to use Simplebitmapdisplayer, Directly set bitmap to ImageView above, we can configure other display logic, he provides here fadeinbitmapdisplayer (transparency from 0-1) Roundedbitmapdisplayer (4 corners are arcs), etc. Then callback to the Imageloadinglistener interface

if (Options.shouldshowimageonloading ()) {IMAGEAWARE.SETIMAGEDRAWABL              E (options.getimageonloading (configuration.resources));              } else if (options.isresetviewbeforeloading ()) {imageaware.setimagedrawable (null);                      } imageloadinginfo imageloadinginfo = new Imageloadinginfo (URI, Imageaware, Targetsize, Memorycachekey,              Options, Listener, Progresslistener, Engine.getlockforuri (URI)); Loadanddisplayimagetask displaytask = new Loadanddisplayimagetask (engine, Imageloadinginfo, Definehan              Dler (options));              if (options.issyncloading ()) { ();              } else {engine.submit (displaytask); }  

  This code is mainly bitmap not in the memory cache, from the file or the network to get bitmap object, instantiate a Loadanddisplayimagetask object, Loadanddisplayimagetask implemented Runnable, If Issyncloading is configured to True, execute Loadanddisplayimagetask's Run method directly, indicating synchronization, which defaults to false and commits loadanddisplayimagetask to the thread pool object

Next we look at Loadanddisplayimagetask's run (), this class is quite complicated, we are still a section of the analysis

if (waitifpaused ()) return;  if (Delayifneed ()) return;  

 If waitifpaused (), Delayifneed () returns True, returns directly from the run () method without executing the following logic, let's take a look at waitifpaused ()

private boolean waitifpaused () {Atomicboolean pause = Engine.getpause (); if (Pause.get ()) {synchronized (Engine.getpauselock ()) {if (Pause.get ()) {L.D (LO                  G_waiting_for_resume, Memorycachekey);                  try {engine.getpauselock (). Wait ();                      } catch (Interruptedexception e) {L.E (log_task_interrupted, Memorycachekey);                  return true;              } L.D (Log_resume_after_pause, Memorycachekey);  }}} return Istasknotactual (); }  

  This method is what to use, mainly when we use the Listview,gridview to load the picture, sometimes in order to slide more fluent, we will choose the finger in the slide or a sudden swipe to not load the picture, so we put forward such a method, then how to use it? The Pauseonscrolllistener class is used here, using a very simple Listview.setonscrolllistener (new Pauseonscrolllistener (Pauseonscroll, pauseonfling)), Pauseonscroll control We slowly slide listview,gridview whether to stop loading the picture, pauseonfling control the hard slide Listview,gridview whether to stop loading the picture

In addition, the return value of this method is determined by istasknotactual (), and we then look at the source code of Istasknotactual ().

Private Boolean istasknotactual () {          return isviewcollected () | | isviewreused ();      }  

isviewcollected () is to determine whether our imageview is recycled by the garbage collector, if it is recycled, The run () of the Loadanddisplayimagetask method returns directly, Isviewreused () determines if the ImageView is reused, and the Reuse run () method also returns directly, why use the isviewreused () method? Mainly Listview,gridview we will reuse the item object, if we first load Listview,gridview the first page of the picture, the first page of the picture is not all loaded we will quickly scroll, isviewreused () Method will avoid these invisible item to load the picture, and directly load the picture of the current interface

Reentrantlock loadfromurilock = Imageloadinginfo.loadfromurilock;          L.D (Log_start_display_image_task, Memorycachekey);          if (loadfromurilock.islocked ()) {L.D (log_waiting_for_image_loaded, Memorycachekey);          } loadfromurilock.lock ();          Bitmap bmp;                try {checktasknotactual ();              BMP = Configuration.memoryCache.get (Memorycachekey);                  if (BMP = = NULL | | bmp.isrecycled ()) {BMP = Tryloadbitmap (); if (BMP = = null) return;                  Listener callback already was fired Checktasknotactual ();                    Checktaskinterrupted ();                      if (options.shouldpreprocess ()) {L.D (log_preprocess_image, Memorycachekey);                      BMP = Options.getpreprocessor (). process (BMP);                      if (BMP = = null) {L.E (error_pre_processor_null, Memorycachekey);         }         } if (BMP! = null && options.iscacheinmemory ()) {L.D (log_cache_i                      Mage_in_memory, Memorycachekey);                  Configuration.memoryCache.put (Memorycachekey, BMP);                  }} else {loadedfrom = Loadedfrom.memory_cache;              L.D (log_get_image_from_memory_cache_after_waiting, Memorycachekey); } if (BMP! = null && options.shouldpostprocess ()) {L.D (Log_postprocess_image, mem                  Orycachekey);                  BMP = Options.getpostprocessor (). process (BMP);                  if (BMP = = null) {L.E (error_post_processor_null, Memorycachekey);              }} checktasknotactual ();          Checktaskinterrupted ();              } catch (Taskcancelledexception e) {firecancelevent ();          Return         } finally {Loadfromurilock.unlock (); }   

  The 1th line of code has a loadfromurilock, this is a lock, the method of acquiring the lock is in the Getlockforuri () method of the Imageloaderengine class

Reentrantlock Getlockforuri (String uri) {          Reentrantlock lock = Urilocks.get (URI);          if (lock = = null) {              lock = new Reentrantlock ();              Urilocks.put (URI, lock);          }          return lock;      }  

  As can be seen from the above, the lock object and the URL of the image is corresponding to each other, why do you want to do this? Also you do not understand, do not know whether we have considered a scene, if in a ListView, an item is in the process of acquiring the picture, and at this time we will roll the item out of the interface and roll it in, after rolling in, if there is no lock, the item will be loaded once the picture, Assuming in a very short period of time scrolling very frequently, then will appear several times to the network to request the picture, so here according to the image URL to a Reentrantlock object, so that the request with the same URL will be in line 7th wait, wait until the picture loading is complete, Reentrantlock is released, and requests for the same URL will continue to execute the code below line 7th

Come to line 12th, they will be taken from the memory cache first, if the memory cache is not going to execute the following logic, so the role of Reentrantlock is to avoid repeated in this case to request a picture from the network.

Line 14th Method Tryloadbitmap (), this method is really a bit long, I first tell you, this is the logic of the first to get from the file cache there are no bitmap objects, if not in the go from the network, and then save bitmap in the file system, we still specifically analyzed

File imagefile = Configuration.diskCache.get (URI);              if (imagefile! = null && imagefile.exists ()) {                  l.d (Log_load_image_from_disk_cache, memorycachekey);                  Loadedfrom = Loadedfrom.disc_cache;                    Checktasknotactual ();                  Bitmap = Decodeimage (Scheme.FILE.wrap (Imagefile.getabsolutepath ()));              }  

First judge the file cache there is no such file, if any, go directly to call Decodeimage () method to decode the picture, the method inside call Baseimagedecoder class decode () method, according to the imageview of the width of the high, ScaleType to cut the picture, the specific code I do not introduce, we go to see, we take a look down Tryloadbitmap () method

if (bitmap = null | | bitmap.getwidth () <= 0 | | bitmap.getheight () <= 0) {              L.D (Log_load_image_from_network, Memorycachekey);                Loadedfrom =;              String imageurifordecoding = URI; if (Options.iscacheondisk () && Trycacheimageondisk ()) {imagefile = Configuration.diskCache.get (u                  RI);                  if (imagefile! = null) {imageurifordecoding = Scheme.FILE.wrap (Imagefile.getabsolutepath ());              }} checktasknotactual ();                Bitmap = Decodeimage (imageurifordecoding); if (bitmap = = NULL | | bitmap.getwidth () <= 0 | | bitmap.getheight () <= 0) {firefailevent (FAILTYPE.D              Ecoding_error, NULL); }          }  

Private Bitmap Decodeimage (String Imageuri) throws IOException {Viewscaletype Viewscaletype = Imageaware.getscaletyp      E ();              Imagedecodinginfo decodinginfo = new Imagedecodinginfo (Memorycachekey, Imageuri, Uri, Targetsize, ViewScaleType,      Getdownloader (), options);  Return Decoder.decode (Decodinginfo); }/** @return <b>true</b>-If image was downloaded successfully; <b>false</b>-otherwise */private Boolean Trycacheimageondisk () throws Taskcancelledexception {L.D (LO        G_cache_image_on_disk, Memorycachekey);      Boolean loaded;          try {loaded = Downloadimage ();              if (loaded) {int width = Configuration.maximagewidthfordiskcache;                            int height = Configuration.maximageheightfordiskcache;                  if (Width > 0 | | height > 0) {l.d (log_resize_cached_image_file, Memorycachekey); Resizeandsaveimage (width, height); Todo:process bOolean result}}} catch (IOException e) {L.E (e);      loaded = false;  } return loaded; Private Boolean Downloadimage () throws IOException {InputStream is = Getdownloader (). GetStream (URI, Options.get      Extrafordownloader ());  Return (URI, is, this);   }

The Downloadimage () method in line 6th is responsible for downloading the picture and keeping it in the file cache, and will download the progress of the saved bitmap callback to the onbytescopied of the Ioutils.copylistener interface (int current, int Total) method, so we can set the Imageloadingprogresslistener interface to get the picture download save progress, here the picture saved in the file system is the original image

第16-17 row, gets whether imageloaderconfiguration sets the size of the picture saved in the file system, if Maximagewidthfordiskcache and Maximageheightfordiskcache are set, Will call the Resizeandsaveimage () method to crop the picture and then replace the original image, save the cropped picture to the file system, before a classmate asked me that the framework is saved in the file system of the picture is the original image, how to save the thumbnail image, Just set Maximagewidthfordiskcache and Maximageheightfordiskcache when you instantiate imageloaderconfiguration in application.

if (BMP = = null) return; Listener callback already was fired                    checktasknotactual ();                  Checktaskinterrupted ();                    if (options.shouldpreprocess ()) {                      l.d (log_preprocess_image, memorycachekey);                      BMP = Options.getpreprocessor (). process (BMP);                      if (BMP = = null) {                          L.E (error_pre_processor_null, Memorycachekey);}                  }                    if (BMP! = null && options.iscacheinmemory ()) {                      l.d (log_cache_image_in_memory, memorycachekey);                      Configuration.memoryCache.put (Memorycachekey, BMP);                  }  

Next here is simple, 6-12 row whether to deal with bitmap, this need to implement, 14-17 is to save the picture in the memory cache

Displaybitmaptask displaybitmaptask = new Displaybitmaptask (BMP, Imageloadinginfo, engine, loadedfrom);          RunTask (Displaybitmaptask, syncloading, Handler, engine);  

The last two lines of code are a display task that looks directly at the Displaybitmaptask class's Run () method

public void Run () {          if (imageaware.iscollected ()) {              L.D (log_task_cancelled_imageaware_collected, Memorycachekey);              Listener.onloadingcancelled (Imageuri, Imageaware.getwrappedview ());          } else if (isviewwasreused ()) {              l.d (log_task_cancelled_imageaware_reused, memorycachekey);              Listener.onloadingcancelled (Imageuri, Imageaware.getwrappedview ());          } else {              L.D (log_display_image_in_imageaware, Loadedfrom, Memorycachekey);              Displayer.display (Bitmap, Imageaware, loadedfrom);              Engine.canceldisplaytaskfor (imageaware);              Listener.onloadingcomplete (Imageuri, Imageaware.getwrappedview (), bitmap);          }      }  


If ImageView is recycled or reused, callback to Imageloadinglistener interface, otherwise call Bitmapdisplayer to show bitmap

Article written here has been written, do not know that there is no further understanding of the open source framework, the open source framework design is also very flexible, with a lot of design patterns, such as builder mode, decoration mode, proxy mode, strategy mode, etc., so as to facilitate our expansion, to achieve the function we want, Today's explanation is here, there is no understanding of the framework of the place can leave a message below, I will try to answer for everyone.

Android Open Source Framework Universal-image-loader full parsing (iii)---source code interpretation

Related Article

Contact Us

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: 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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.