Preface:An open source framework that can download a level three cache mechanism from Https://github.com/nostra13/Android-Universal-Image-Loader. The following is a brief introduction to the main common methods in this framework, and mastering these methods will basically deal with the needs of most image downloads.
Note:The following code is a schematic snippet, read it carefully and you should know how to use it. Blue is represented as a class in the open source framework.
1. Initialize the Imageloader class object:
Imageloader Imageloader = Imageloader.getinstance (); Imageloader.stop (); Stop loading picture Imageloader.clearmemorycache (); Clear Memory cache Imageloader.cleardisccache (); Clear the cache from the SD card
2. In the adapter GetView () method, display the Imageloader.displayimage () method of the ImageView Picture:
Imageloadinglistener Animatefirstlistener = new Animatefirstdisplaylistener (); /** * Show Picture * parameter 1: Picture URL * Parameter 2: Control showing picture * Parameter 3: Display picture Settings * Parameter 4: Listener */Imageloader.displayimage (imag Eurl, Holder.image, Options, Animatefirstlistener); Add the picture display task to the execution pool, and the picture will be displayed to ImageView when it turns to this imageView imageloader.displayimage (IMAGEURL, ImageView, Options); Parameters passed in anonymous inner class imageloader.displayimage (IMAGEURL, ImageView, Options, new Simpleimageloadinglistener () { @Override public void onloadingstarted (String imageuri, view view) {//To display a progress bar } @Override public void onloadingfailed (String imageuri, view view, Failre Ason Failreason) {String message = NULL; Switch (Failreason.gettype ()) {//Get Picture failure type Case IO_ERROR://File I/O error Message = "Input/output error"; Break Case DECODING_ERROR://Decoding error message = "Image can ' t be decoded"; Break Case network_denied://Network delay message = "Downloads is DENIED"; Break Case Out_of_memory://Insufficient memory message = "Out of the memory error"; Break Case UNKNOWN://reason unknown message = "UNKNOWN error"; Break } toast.maketext (context, message, Toast.length_short). Show (); } @Override public void Onloadingcomplete (String imageuri, view view, Bitmap Loadedimag e) {//Do not show circular progress bar}});3. The parameters of the above 2. Imageloader.displayimage () method are options:
Displayimageoptions options; Displayimageoptions is the class for setting up picture display options = new Displayimageoptions.builder () . Showstubimage (r.drawable.ic_stub ) //Set the picture to be displayed during picture download . Showimageforemptyuri (R.drawable.ic_empty) //Set the picture to be displayed when the image URI is empty or wrong. Showimageonfail (R.drawable.ic_error) //sets the picture to display when an error occurs during picture loading or decoding . Cacheinmemory (True) // Sets whether the downloaded picture is in memory . Cacheondisc (True) //sets whether the downloaded picture is slow to exist on the SD card . Bitmapconfig (Bitmap.Config.RGB_565) Sets the decoding type of the picture . Displayer (new Roundedbitmapdisplayer) //Set rounded picture . Build (); To create a configured Displayimageoption object
4. Parameter Animatefirstlistener of the above 2. Imageloader.displayimage () Method:
/*** picture loading the first time the listener is displayed * @author administrator**/ private static class Animatefirstdisplaylistener extends Simpleimageloadinglistener { static final list<string> displayedimages = collections.synchronizedlist (new Linkedlist<string> ()); @Override public void Onloadingcomplete (String imageuri, view view, Bitmap loadedimage) { if (loadedimage! = null { ImageView ImageView = (ImageView) view; Whether to display the boolean firstdisplay =!displayedimages.contains (Imageuri) for the first time; if (firstdisplay) { //Picture fade- in effect fadeinbitmapdisplayer.animate (ImageView, n); Displayedimages.add (Imageuri);}}}
5. The framework is primarily the above approach, which can also be understood below:
Listview.setonscrolllistener (New Pauseonscrolllistener (Imageloader, True, false));// A bunch of picture links com.nostra13.example.universalimageloader.constants.images;//the keys for passing extra parameters in intent: " Com.nostra13.example.universalimageloader.IMAGES "; com.nostra13.example.universalimageloader.constants.extra.images//the key to pass the Extra parameter in intent: " Com.nostra13.example.universalimageloader.IMAGE_POSITION " Com.nostra13.example.universalimageloader.Constants.Extra.IMAGE_POSITION
Android Open source Framework Imageloader: three-level caching mechanism for loading images