How to use Universal-Image-Loader: universalimageloader

Source: Internet
Author: User

How to use Universal-Image-Loader: universalimageloader
Advantages and disadvantages of open-source frameworks

The open-source framework provides developers with convenience and avoids repeated wheel creation, but hides some development details. If they do not focus on their internal implementation, it will be difficult for developers to master core technologies, of course, it cannot be better used, and the integrated use and low-level implementation of the planning and analysis project.

Basic Quartet
  1. ImageLoaderConfiguration
  2. -> ImageLoader. init (imageLoaderConfiguration)
  3. DisplayImageOptions
  4. -> ImageLoader. displayImage (..., displayImageOptions ,...)
1. ImageLoaderConfiguration (with default)

Configure through ImageLoaderConfiguration
Two methods:

  1. Default
ImageLoaderConfiguration configuration = ImageLoaderConfiguration                  .createDefault(this);

  

  1. Detailed
File cacheDir = StorageUtils.getCacheDirectory(context);ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)        .memoryCacheExtraOptions(480, 800) // default = device screen dimensions        .diskCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)        .taskExecutor(...)        .taskExecutorForCachedImages(...)        .threadPoolSize(3) // default        .threadPriority(Thread.NORM_PRIORITY - 1) // default        .tasksProcessingOrder(QueueProcessingType.FIFO) // default        .denyCacheImageMultipleSizesInMemory()        .memoryCache(new LruMemoryCache(2 * 1024 * 1024))        .memoryCacheSize(2 * 1024 * 1024)        .memoryCacheSizePercentage(13) // default        .diskCache(new UnlimitedDiscCache(cacheDir)) // default        .diskCacheSize(50 * 1024 * 1024)        .diskCacheFileCount(100)        .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default        .imageDownloader(new BaseImageDownloader(context)) // default        .imageDecoder(new BaseImageDecoder()) // default        .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default        .writeDebugLogs()        .build();

  

2. Configure the previous information for ImageLoader
ImageLoader.getInstance().init(configuration);

ImageLoader uses the singleton mode: source code

Public static ImageLoader getInstance () {/* lock for thread security * 1. But the ImageLoader object has, skip synchronization directly * this is the idea of Singleton design (refer to HeadFirst design mode) */if (instance = null) {synchorinized (ImageLoader. class) {if (instance = null) {instance = new ImageLoader () ;}} return instance ;}

  

public synchronized void init(){    if(configuration == null){        throw new IlleagalArgumentException(ERROR_INIT_CONFIG_WITH_NULL);    if(this.configuration == null){        this.configuration = configuration;        emptyListener = new SimpleIageLoadingListener();        fakeBitmapDisplayer = new FakeBitmapDisplayer();    }  }}

  

3. Download image preferences
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(1000)        .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();

  

4. Officially load the image

Two Methods: loadImage (); displayImage ()-This is generally used;

Final ImageView mImageView = (ImageView) findViewById (R. id. image); String imageUrl = "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg"; ImageSize mImageSize = new ImageSize (100,100); // display the image configuration DisplayImageOptions options = new DisplayImageOptions. builder (). cacheInMemory (true ). cacheOnDisk (true ). bitmapConfig (Bitmap. config. RGB_565 ). build (); ImageLoader. getInstance (). loadImage (imageUrl, mImageSize, options, new SimpleImageLoadingListener () {@ Overridepublic void onLoadingComplete (String imageUri, View, Bitmap loadedImage) {super. onLoadingComplete (imageUri, view, loadedImage); mImageView. setImageBitmap (loadedImage );}});

  

GirdView, used in ListView

Using the GridView and ListView to display a large number of images, and when we quickly slide the GridView and ListView, we hope to stop loading the images while in the GridView, when the ListView stops sliding, it loads the image of the current interface. This framework also provides this function and is easy to use. It provides the PauseOnScrollListener class to control the ListView, stopping image loading during the GridView Sliding Process

listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));gridView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));
Reference
  1. Https://github.com/nostra13/Android-Universal-Image-Loader
  2. Http://blog.csdn.net/xiaanming/article/details/26810303

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.