Android-universal-image-loader three major components Displayimageoptions, Imageloader, Imageloaderconfiguration detailed

Source: Internet
Author: User

Android-universal-image-loader three major components Displayimageoptions, Imageloader, imageloaderconfiguration detailed introduction

Android-universal-image-loader is an open source UI component program designed to provide a reusable instrument for asynchronous image loading, caching, and display. So, if you need this feature in your program, try it. Because some classes and methods have been encapsulated. We can use them directly. Instead of having to write again. In fact, write a program in this area is still more troublesome, to consider multi-threaded cache, memory overflow and many other aspects.

Second, the specific use

The important feature of a good class library is that it is highly configurable. Let's start with the simple use of android-universal-image-loader, which is normally the case with the default configuration.

The following example uses Android-universal-image-loader to load a network picture into a wall of pictures.

1 public class Baseactivity extends Activity {2     imageloader imageloader; 3     @Override 4     protected void Oncreat E (Bundle savedinstancestate) {5           //Create global configuration and initialize Imageloader with the This configuration 6
   imageloaderconfiguration config = new Imageloaderconfiguration.builder (Getapplicationcontext ()) 7             . Build (); 8         imageloader.getinstance (). init (config); 9         super.oncreate (savedinstancestate);     }11}  
  1 public class Mainactivity extends Baseactivity {2 3 @Override 4 protected void onCreate (Bundle savedinsta  Ncestate) {5 super.oncreate (savedinstancestate);  6 Setcontentview (R.layout.activity_main);  7 8 Imageloader Imageloader = Imageloader.getinstance (); 9 GridView GridView = (GridView) This.findviewbyid (R.id.grdvimagewall); Gridview.setadapter (New Photowalladapter (constants.images));  The static class Viewholder {ImageView ImageView; ProgressBar ProgressBar; 17} public class Photowalladapter extends Baseadapter {string[] imageurls; Imageloader imag Eload; Displayimageoptions options; LinearLayout Gridviewitem; Public Photowalladapter (string[] imageurls) {$ assert imageurls! = null; Imageurls = Imageurls;  options = new Displayimageoptions.builder () 30                   . showimageonloading (r.drawable.ic_stub)//resource or 31                                                                 Drawable Showimageforemptyuri (r.drawable.ic_empty)//resource or 33 Drawable Showimageonfail (R.drawable.ic_err OR)//resource or//drawable. Rese                     Tviewbeforeloading (FALSE)//default PNS delaybeforeloading (+). Cacheinmemory (FALSE)//default 38                     . Cacheondisk (FALSE)//default. Considerexifparams (FALSE)//default 40 . Imagescaletype (imagescaletype.in_sample_power_of_2)//default. Bitmapconfig (bitmap.co Nfig. argb_8888)//default Displayer (New Simplebitmapdisplayer ())//default. Ha Ndler (New Handler ())//default. Build (); This.imageload = Imageloader.getinstance (); A. @Override () (), GetCount () {this.imageUrls.length return;  GetItem (int position) {(position <= 0) ||                         Position >= this.imageUrls.length) {$ throw new IllegalArgumentException (58 "Position<=0| | Position>=this.imageurls.length "); This.imageurls[position];         The @Override the public long getitemid (int position) {position return; 66             } @Override public View getView (int position, view Convertview, ViewGroup parent) {70 Determine if the image is already in the cache, and if not, download the final viewholder holder; if (Convertview = = null) {holder = new viewhoLder (); Gridviewitem = (linearlayout) getlayoutinflater (). Inflate (R.layout.image_wa Ll_item, NULL); Holder.imageview = (ImageView) gridviewitem. Findviewbyid (R.id.item_image); Holder.progressbar = (ProgressBar) gridviewitem. Findviewbyid (R.id.item_proc ESS); Gridviewitem.settag (holder); Bayi Convertview = Gridviewitem; Holder} else {Viewholder) gridviewitem.gettag ();                     Is.imageLoad.displayImage (This.imageurls[position], Holder.imageview, options, 87 New Simpleimageloadinglistener () {@Override Onlo                             Adingstarted (String Imageuri, view view) {holder.progressBar.setProgress (0); 92 HolDer.progressBar.setVisibility (view.visible); 94 @Override Onloadingfail                             Ed (String Imageuri, view view, Failreason Failreason) {98 Holder.progressBar.setVisibility (View.gone); }100 101 @Override102 public void Onloadingcomp Lete (String imageuri,103 view view, Bitmap loadedimage) {104 h Older.progressBar.setVisibility (View.gone);}106 107}, new Imageloadingpro Gresslistener () {108 109 @Override110 public void Onprogressupdate (String imageuri,111 view view, int current, int. total) {HOLDER.PR              Ogressbar.setprogress (Math.Round (100.0f113                       * current/total)); 114}115}); Determine if the picture has been downloaded by URL convertview;117}118 119}120}

The main objects inside are highlighted.

The relationship between three people

imageloaderconfiguration is a global configuration for the image cache, with the main thread class, cache size, disk size, image download and parsing, and log configuration.

Imageloader is specific to download pictures, cache pictures, display pictures of the specific execution class, it has two specific methods displayimage (...), LoadImage (...), But in the end their implementation is displayimage (...).

displayimageoptions is used to instruct each imageloader to display the corresponding picture according to the status of the network picture (blank, download error, downloading), whether to load the cache to disk, and how to deal with the picture after downloading.

From the collaborative relationship of the three, they are somewhat like the relationship between kitchen Rules, chefs, and individual tastes of customers. imageloaderconfiguration is like the rules in the kitchen, how to dress each chef, how to keep the kitchen clean, this is a rule for every chef, and does not allow personalized changes. Imageloader is like a chef who cooks a specific dish, and is responsible for making the recipes. displayimageoptions is like each customer's preference, according to the customer is a heavy taste or light, each Imageloader according to the requirements of displayimageoptions specific implementation.

Imageloaderconfiguration

In the example code above, we use the default configuration of Imageloaderconfiguration, which gives a more detailed configuration of imageloaderconfiguration, from the following configuration, It can be seen that the configuration of imageloaderconfiguration is mainly global configuration, the main thread class, cache size, disk size, picture download and parsing, log configuration.

imageloaderconfiguration config = new Imageloaderconfiguration.builder (context). memorycacheextraoptions (480, 800)        Default = Device screen dimensions. Diskcacheextraoptions (480, +, NULL). Taskexecutor (...)        . Taskexecutorforcachedimages (...) . ThreadPoolSize (3)//default. ThreadPriority (thread.norm_priority-1)//default. Tasksprocessingorder (Que  UEPROCESSINGTYPE.FIFO)//default. Denycacheimagemultiplesizesinmemory (). MemoryCache (New Lrumemorycache (2 * 1024x768). Memorycachesize (2 * 1024x768). Memorycachesizepercentage (+)//default. diskcache        (New Unlimiteddisccache (Cachedir))//default. Diskcachesize (* 1024x768). Diskcachefilecount (100) . Diskcachefilenamegenerator (New Hashcodefilenamegenerator ())//default. Imagedownloader (New Baseimagedownloader (context)) Default. Imagedecoder (New Baseimagedecoder ())//default. defaultdisplayimageoptioNS (Displayimageoptions.createsimple ())//default. Writedebuglogs (). build (); 

Imageloaderconfiguration 's main responsibility is to record the relevant configuration, which is actually a collection of some fields (such as the following source code). It has a builder's inner class, the field in this class is exactly the same as the field in Imageloaderconfiguration, it has some default values, and the imageloaderconfiguration can be configured by modifying the builder.

View Code

Display Options

You can use ImageLoader.displayImage(...) the Display Optionsfor each of them.

Displayimageoptions options = new Displayimageoptions.builder ()        . showimageonloading (r.drawable.ic_stub)// Resource or drawable        . Showimageforemptyuri (r.drawable.ic_empty)//resource or drawable        . Showimageonfail ( R.DRAWABLE.IC_ERROR)//resource or drawable        . Resetviewbeforeloading (FALSE)  //default        . Delaybeforeloading.        Cacheinmemory (FALSE)//default        . Cacheondisk (FALSE)///default        . Preprocessor (...)        . Postprocessor (...)        . Extrafordownloader (...)        . Considerexifparams (FALSE)//default.        Imagescaletype (imagescaletype.in_sample_power_of_2)//default        . Bitmapconfig (Bitmap.Config.ARGB_8888)//default        . Decodingoptions (...)        . Displayer (New Simplebitmapdisplayer ())//default.        Handler (new handler ())//default        . Build ();

The main function of the Display Options is to record the relevant configuration, which is actually a collection of some fields (such as the following source code). It has a builder's inner class, the field in this class is exactly the same as the field in Displayoption, it has some default values, and the displayoptions can be configured by modifying the builder.

View Code

Reference links

http://blog.csdn.net/wangjinyu501/article/details/8091623

Https://github.com/nostra13/Android-Universal-Image-Loader

Http://www.intexsoft.com/blog/item/74-universal-image-loader-part-3.html

Android-universal-image-loader three major components Displayimageoptions, Imageloader, Imageloaderconfiguration detailed

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

  • 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.