Loading pictures asynchronously using UIL (Universal-image-loader)

Source: Internet
Author: User

Overview :Android-universal-image-loader is an open-source UI component program that is designed to enable reusable asynchronous image loading, caching, and display. So, if you need this feature in your program, use it because you've encapsulated some classes and methods.     In fact, writing a program in this area is still more troublesome, to consider many aspects of multi-threading, caching, memory overflow and so on. Feature Description:

A: Multi-threaded picture loading;
B: Flexibility to change the basic configuration of Imageloader, including the maximum number of threads, cache mode, picture display options, etc.;
C: Picture asynchronous load caching mechanism, including memory cache (soft reference) and local cache;
D: Monitoring and event handling for the loading process;

E: Ability to configure display options for loading pictures, including picture fillet processing and loading complete display animation;

Steps to use:

1. Download the jar package Universal-image-loader-x.x.x-with-sources.jar and import the project

2 . Configure the manifest file to add network requests and access to external storage because network requests and local caches are required

<uses-permission android:name= "Android.permission.INTERNET"/>    <uses-permission android:name= " Android.permission.READ_EXTERNAL_STORAGE "/>    <uses-permission android:name=" android.permission.WRITE_ External_storage "/>    <uses-permission android:name=" Android.permission.MOUNT_UNMOUNT_FILESYSTEMS "/>

3, implement the Custom MyApplication class, and initialize the Imageloader, note the application tag in manifest to indicate

android:name= ". MyApplication ", otherwise the default application class is used.

<application        android:name= ". Uilapplication "

  

Package Com.wjt.day36_01_uillistview;import Java.io.file;import Com.nostra13.universalimageloader.cache.disc.impl.unlimiteddisccache;import Com.nostra13.universalimageloader.cache.disc.naming.hashcodefilenamegenerator;import Com.nostra13.universalimageloader.cache.memory.impl.usingfreqlimitedmemorycache;import Com.nostra13.universalimageloader.core.displayimageoptions;import Com.nostra13.universalimageloader.core.imageloader;import Com.nostra13.universalimageloader.core.imageloaderconfiguration;import Com.nostra13.universalimageloader.core.assist.queueprocessingtype;import Com.nostra13.universalimageloader.core.download.baseimagedownloader;import Android.app.application;import Android.content.context;import Android.graphics.bitmap.compressformat;public class UILApplication extends application{@Overridepublic void OnCreate () {super.oncreate (); Initimageloader (Getapplicationcontext ());} private void Initimageloader (context context) {File Cachedir = Context.getexternalcachedir (); ImageloaderConfiguration configuration = new Imageloaderconfiguration.builder (Getapplicationcontext ()). Memorycacheextraoptions (480)//Max width, max height, which is the maximum length of each cache file saved. disccacheextraoptions (480, 800, Compressformat.png, +, NULL)//Set the cache details, preferably do not set this. ThreadPoolSize (5)//Line Cheng Chinega the number of loads. ThreadPriority (thread.norm_ PRIORITY-2). Denycacheimagemultiplesizesinmemory (). MemoryCache (New Usingfreqlimitedmemorycache (4 * 1024 * 1024))// You can implement it through your own memory cache. Memorycachesize (4 * 1024x768 * 1024x768). Disccachesize (* 1024x768). Disccachefilenamegenerator (New Hashcodefilenamegenerator ())//The URI name that will be saved is encrypted with MD5. Tasksprocessingorder (Queueprocessingtype.lifo). Disccachefilecount (100)//number of files cached. Disccache (new Unlimiteddisccache (Cachedir))// Custom cache path. Defaultdisplayimageoptions (Displayimageoptions.createsimple ()). Imagedownloader (New Baseimagedownloader ( Context, 5 * (+)). Build ();//Start building imageloader.getinstance (). init (configuration);}}

4, configure the picture Display option--displayimageoptions settings (I'll take the ListView here as an example and create a custom adapter)

Part code public class Mygridviewadapter extends Baseadapter {private string[] Arrurls = null;private Layoutinflater inflater = null;private displayimageoptions options = null;private Imageloader Imageloader = null;public MyGridViewAdapter (Contex T context, string[] arrurls) {this.arrurls = Arrurls;inflater = (layoutinflater) context.getsystemservice ( Context.layout_inflater_service); options = new Displayimageoptions.builder (). showimageonloading (R.drawable.ic_ Stub). Showimageforemptyuri (R.drawable.ic_empty). Showimageonfail (R.drawable.ic_error). Cacheinmemory (True). Cacheondisc (True). Considerexifparams (True). Displayer (new Fadeinbitmapdisplayer). Displayer (New Roundedbitmapdisplayer). Build (); Imageloader = Imageloader.getinstance ();}

A few important methods are as follows:

5, use the DisplayImage () method where the image is to be generated, generally choose three parameters or four parameters

Public View GetView (int position, View Convertview, ViewGroup parent) {final Viewholder mholder;if (Convertview = = null) { Mholder = new Viewholder (); Convertview = Inflater.inflate (R.layout.item_gridview_uil, Parent,false); Mholder.imageview_item_icon = (ImageView) Convertview.findviewbyid (R.id.imageview_item_icon); mHolder.progrssBar_ Item = (ProgressBar) Convertview.findviewbyid (R.id.progressbar_item); Convertview.settag (Mholder);} else {Mholder = (Viewholder) Convertview.gettag ();} Load picture with UIL frame imageloader.displayimage (Arrurls[position],mholder.imageview_item_icon, options,new Imageloadinglistener () {@Overridepublic void onloadingstarted (String imageuri, view view) {Mholder.progrssbar_ Item.setvisibility (view.visible);} @Overridepublic void onloadingfailed (String imageuri, View View,failreason failreason) {Mholder.progrssbar_ Item.setvisibility (View.gone);} @Overridepublic void Onloadingcomplete (String imageuri, View view,bitmap loadedimage) {Mholder.progrssbar_ Item.setvisibility (View.gone);} @Overridepublic void onloadingcancelled (String imageuri, view view) {//TODO auto-generated Method stub}}); return conver TView;}

As follows:

  

Loading pictures asynchronously using UIL (Universal-image-loader)

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.