Android Universal-image-loader Primary Use

Source: Internet
Author: User

Reference to http://blog.csdn.net/xiaanming/article/details/26810303 This article, thanks to the original author.

Excellent without much to say, GitHub Project address: Https://github.com/nostra13/Android-Universal-Image-Loader.

Its main features:

    1. Multi-threaded download pictures, images can be from the network, file system, project folder assets and drawable medium
    2. Supports arbitrary configuration of imageloader, such as thread pool, picture downloader, memory cache policy, hard disk cache policy, picture display options, and other configuration
    3. Memory cache for image support, file system cache or SD card cache
    4. Support for monitoring the image download process
    5. Bitmap is clipped based on the size of the control (ImageView), reducing bitmap memory consumption
    6. Better control of the loading process of the picture, such as pause picture loading, restart loading pictures, generally used in the Listview,gridview, the sliding process to pause loading pictures, stop the slide when to load the picture
    7. Provides loading of pictures on a slower network


Simple to use:

1. Create a new Android Appication. The name is Iapplicaiton.

2. Copy the jar under the Libs from the sample in the downloaded project to this project.

3. Create a new class Myapplicaiton inherit application:

Package Com.example.iapplication;import Com.nostra13.universalimageloader.core.imageloader;import Com.nostra13.universalimageloader.core.imageloaderconfiguration;import Android.app.application;public Class MyApplication extends Application {@Override public      void OnCreate () {          super.oncreate ();            Create the default imageloader configuration parameter          imageloaderconfiguration config = imageloaderconfiguration                  . Createdefault ( this);                    Initialize imageloader with configuration.          Imageloader.getinstance (). init (configuration);      }  }

4. Configure the Mainfest.xml file:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.iapplication "android:versioncode=" 1 "android:versionname=" 1.0 "> &LT;USES-SD K android:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission android:name=" android.pe Rmission. INTERNET "/> <!--Include Next permission if you want to allow UIL to cache images on SD card---<use S-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:name= "myappl Ication "android:allowbackup=" true "android:icon=" @drawable/ic_launcher "android:label=" @string/app_n Ame "Android:theme=" @style/apptheme "> <activity android:name=" Com.example.iapplication.Mai Nactivity "android:label=" @string/app_name "> <intent-filter> <action and Roid:name= "Android.intent.actiOn. MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filte R> </activity> </application></manifest>
Note the addition of application nodes and permissions.

Next, load the picture.

5. Define a layout:

<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent ">    <imageview        android:layout_gravity= "center"        android:id= "@+id/image"        android:src= "@drawable/ic_empty"        Android:layout_width= "Wrap_content"        android:layout_height= "Wrap_content"/></framelayout>

6. Load the image with DisplayImage ():

Load public void AdvanceLoad2 () {final ImageView Mimageview = (ImageView) Findviewbyid (r.id.image) using the DisplayImage method;          String imageUrl = "http://smartcost.com.cn/global/upload/img/20150206/14223432211081942.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);  }

Get. If it is necessary to show the progress of loading the picture, just pass in the Imageloadingprogresslistener interface in the DisplayImage () method:

Imageloader.displayimage (IMAGEURL, Mimageview, Options, New Simpleimageloadinglistener (), New Imageloadingprogresslistener () {                            @Override public              void Onprogressupdate (String imageuri, view view, int Current,                      int total) {                                }          });  

load a picture of another resource:

String ImagePath = "/mnt/sdcard/image.png";          String imageUrl = Scheme.FILE.wrap (ImagePath);      String imageUrl = "http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg";                    Imageloader.displayimage (IMAGEURL, Mimageview, Options);  

The image comes from content provider          String contentprividerurl = "CONTENT://MEDIA/EXTERNAL/AUDIO/ALBUMART/13";                    Image from ASSETS          String assetsurl = Scheme.ASSETS.wrap ("Image.png");                    The image is from          String drawableurl = Scheme.DRAWABLE.wrap ("R.drawable.image");  
When we quickly slide the Gridview,listview, we want to stop the loading of the image and load the picture of the current interface when the Gridview,listview stops sliding, which of course also provides this function, which is also very simple to use. It provides pauseonscrolllistener this class to control the Listview,gridview sliding process to stop loading pictures

Listview.setonscrolllistener (New Pauseonscrolllistener (Imageloader, Pauseonscroll, pauseonfling));          

The first parameter is our picture loading object Imageloader, the second is to control whether to suspend loading the picture during the sliding process, if need to pause to pass true on the line, the third parameter controls the fierce sliding interface when the picture is loaded

OutOfMemoryError


Although this framework has a good caching mechanism, effectively avoid the production of oom, the general situation of the probability of generating oom is relatively small, but does not guarantee that outofmemoryerror never happen, this framework for OutOfMemoryError do a simple catch, Make sure our program encounters Oom without being crash, but if we use this framework often to get oom, we can use the way to improve.

    • Reduce the number of threads in the thread pool, configured in Imageloaderconfiguration (. threadpoolsize), recommended configuration 1-5
    • Configure Bitmapconfig as Bitmap.Config.RGB_565 in the displayimageoptions option, because the default is argb_8888, using rgb_565 consumes twice times less memory than using argb_8888
    • The memory cache for the configuration picture in Imageloaderconfiguration is MemoryCache (new Weakmemorycache ()) or does not use a memory cache
    • Set the. Imagescaletype (Imagescaletype.in_sample_int) or Imagescaletype (ImageScaleType.EXACTLY) in the displayimageoptions option.

When we use this framework, we try to use the DisplayImage () method to load the image, and LoadImage () is the Onloadingcomplete () method that recalls the picture object to the Imageloadinglistener interface. Need us to manually set up to ImageView above, DisplayImage () method, the ImageView object uses weak references, convenient garbage collector to reclaim ImageView objects, if we want to load fixed-size images, Using the LoadImage () method requires passing a ImageSize object, and the DisplayImage () method is based on the measured value of the ImageView object, or Android:layout_width and Android:layout_ Height setting value, or Android:maxwidth and/or android:maxheight set value to crop the picture


Android Universal-image-loader Primary Use

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