[GitHub] uses Universal-Image-Loader.

Source: Internet
Author: User

1. Quick use (ensure that ImageLoader is initialized only once, so that the image cache will be better .)

Scenario: Set an image with a specified Uri for ImageView.

1. Export the package and configure the Internet and read/write SD card permissions.

2. Initialization: (if you perform initialization in the Custom Application, remember to change the name attribute of the Application node in the manifest file .)

ImageLoaderConfiguration config = new ImageLoaderConfiguration. Builder (context). build ();

ImageLoader. getInstance. init (config );

3. parameter configuration

DisplayImageOptions displayImageOptions = new DisplayImageOptions. Builder ()
. ShowStubImage (R. drawable. ic_launcher)
. ShowImageOnFail (R. drawable. ic_launcher)
. ImageScaleType (ImageScaleType. EXACTLY)
. ShowImageForEmptyUri (R. drawable. ic_launcher). cacheInMemory (true)
. CacheOnDisc (true). displayer (new FadeInBitmapDisplayer (300 ))
. ImageScaleType (ImageScaleType. EXACTLY). build ();

4. Set the image.

ImageLoader. displayImage (URI, mImageView, displayImageOptions );

2. Functions (translated from GitHub)

Source: https://github.com/dodola/Android-Universal-Image-Loader

Function: 1. multi-threaded image loading;

2. Coordinates the widest possible image loading configurations (sub-thread, download, decoding, memory & hard disk cache, display image, and others );

3. Listen to the download process as much as possible;

4. Customize the display image call for various parameters as much as possible;

5. Supports small desktop space

Supports Android 2.0 and later versions.

3. initialization parameters:

Configuration

All options are optional in the configuration builder. Use those that you really want to customize.

// DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.
File cacheDir = StorageUtils.getCacheDirectory(context);ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)        .memoryCacheExtraOptions(480, 800) // default = device screen dimensions        .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75)        .taskExecutor(AsyncTask.THREAD_POOL_EXECUTOR)        .taskExecutorForCachedImages(AsyncTask.THREAD_POOL_EXECUTOR)        .threadPoolSize(3) // default        .threadPriority(Thread.NORM_PRIORITY - 1) // default        .tasksProcessingOrder(QueueProcessingType.FIFO) // default        .denyCacheImageMultipleSizesInMemory()        .memoryCache(new LruMemoryCache(2 * 1024 * 1024))        .memoryCacheSize(2 * 1024 * 1024)        .discCache(new UnlimitedDiscCache(cacheDir)) // default        .discCacheSize(50 * 1024 * 1024)        .discCacheFileCount(100)        .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default        .imageDownloader(new BaseImageDownloader(context)) // default        .imageDecoder(new BaseImageDecoder()) // default        .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default        .enableLogging()        .build();
 
 
Display Option
Display options, which can be requested by any display thread.
// DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.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)
// If You Want To load images from the cache, set the parameters in the following two statements to true. . CacheInMemory (false) // default.
        .cacheOnDisc(false) // default        .preProcessor(...)        .postProcessor(...)        .extraForDownloader(...)        .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default        .bitmapConfig(Bitmap.Config.ARGB_8888) // default        .decodingOptions(...)        .displayer(new SimpleBitmapDisplayer()) // default        .handler(new Handler()) // default        .build();
Iv. Use Cases
The uri used to display the image:
String imageUri = "http://site.com/image.png"; // from WebString imageUri = "file:///mnt/sdcard/image.png"; // from SD cardString imageUri = "content://media/external/audio/albumart/13"; // from content providerString imageUri = "assets://image.png"; // from assetsString imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
Note: Use drawable: // unless you really need it. Always pay attention to the local image loading method: setImageResource tape instead of ImageLoader.
5. Useful Information
1. ImageLoader. getInstance (). init (config); // initialize the application when it is enabled.
2. <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/> the SD card cache requires write permission.
3. ImageLoader determines the width and height of the Image Based on the width and height of the ImageView.
4. If OOM occurs frequently
① Reduce the thread pool size in the configuration (. threadPoolSize). 1-5 is recommended;
② Use. bitmapConfig (Bitmap. config. RGB_565) instead of ARGB_8888;
③ Use.imageScaleType(ImageScaleType.IN_SAMPLE_INT)Or try.imageScaleType(ImageScaleType.EXACTLY);
④ Avoid using RoundedBitmapDisplayer. It will create a new Bitmap object in ARGB_8888 format;
⑤ Use. memoryCache (new WeakMemoryCache (), do not use. cacheInMemory ();
5. memory cache, SD card cache, and image display can be implemented through initialization;
6. To avoid using list, grid, and scroll, you can use
boolean pauseOnScroll = false; // or trueboolean pauseOnFling = true; // or falsePauseOnScrollListener listener = new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);listView.setOnScrollListener(listener);

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.