Blog Source:http://www.open-open.com/lib/view/open1433940304473.html
Android-UNIVERSAL-IMAGE-LOADER (corresponding version number 1.9.4 is the latest version)
Original Author: Gu Xuewu
Edit Organization:Quick code crowdsourcing
directory
1. Personalized ImageLoader configuration
1.1. Where to configure and how to configure
2. All kinds of cache methods will be for you to choose
2.1.diskcache disk cache
2.2.MemoryCache memory cache
2.3. Don’t forget the cleaning of cache
3. Online picture acquisition and stop if you want to stop
4. The image size is controlled by you
5. Precautions
6. Download address
1. Personalized ImageLoader configuration
1.1. Where to configure and how to configure
Generally configures the images
(1) ImageLoaderConfiguration each attribute explanation
l can directly use the default configuration of ImageLoaderConfiguration:
ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
l can also customize ImageLoaderConfiguration:
ImageLoaderConfiguration class Field detailed explanation:
Filed name | Filed meaning | default value | Builder setting method |
Int maxImageWidthForMemoryCacheInt maxImageHeightForMemoryCache | Maximum wide height of memory cache pictures
The width height of |
screen | memoryCacheExtraOptions(…) |
Int maxImageWidthForDiskCacheInt maxImageHeightForDiskCache | Disk cache picture maximum width height | 0,0 | DiscacheeExtraoptions (…) or DiskCacheextRaoptions (…) |
BitmapProcessor processorForDiskCache | Image processor, responsible for reading or writing from the disk cache or writing | null | DiscacheeExtraoptions (…) or DiskCacheextRaoptions (…) |
Executor taskExecutor | cache thread pool | DefaultConfigurationFactory.createExecutor(…); | taskExecutor(…) |
Executor taskExecutorForCachedImages | Download the thread pool of the cache picture | DefaultConfigurationFactory.createExecutor(…); | taskExecutorForCachedImages(…) |
Boolean customExecutor | Whether to customize the thread pool | false | None |
Boolean customExecutorForCachedImages | Whether to customize the thread pool of downloading the cache picture | false | None |
Int threadPoolSize | thread pool number | 3 | threadPoolSize(…) |
Int threadPriority | thread priority | Thread.NORM_PRIORITY – 2 | threadPriority(…) |
QueueProcessingType tasksProcessingType | Piece download and displayed work queue sorting | QueueProcessingType.FIFO | tasksProcessingOrder(…) |
MemoryCache memoryCache | Specify the realization of memory cache | DefaultConfigurationFactory.createMemoryCache(…) | memoryCache(…) |
DiskCache diskCache | Specify the implementation of disk cache | DefaultConfigurationFactory.createDiskCache(…); | disccache (…) or diskcache (…) |
ImageDownloader downloader | picture downloader, responsible for getting input stream from the sources of the picture | imageDownloader(…) | DefaultConfigurationFactory.createImageDownloader(context); |
ImageDecoder decoder | Image decoder, responsible for converting the picture input flow inputStream to the Bitmap object | DefaultConfigurationFactory.createImageDecoder(writeLogs); | imageDecoder(…) |
DisplayImageOptions defaultDisplayImageOptions; | default picture display configuration item | DisplayImageOptions.createSimple(); | defaultDisplayImageOptions(…) |
ImageDownloader networkDeniedDownloader | picture downloader that is not allowed to access the network | networkDeniedDownloader = new NetworkDeniedImageDownloader(downloader); | None |
ImageDownloader slowNetworkDownloader | pictures downloader under the situation of slow network | slowNetworkDownloader = new SlowNetworkImageDownloader(downloader); | None |
(2) DisplayImageOptions each attribute explanation
l Create the default DisplayImageOptions:
DisplayImageOptions option_0 = DisplayImageOptions.createSimple();
L can also customize DisplayImageOptions:
DisplayImageOptions Class Field Detailed Explanation
Filed name | FIELD meaning | Default value | Builder Setting method |
int imageResOnLoadingDrawable imageOnLoading | Pictures displayed during download | 0 | Showstubimage (…) or ShowImageonloading (…) |
int imageResForEmptyUriDrawable imageForEmptyUri | 7 7 7 7 7 7 | 0 | showImageForEmptyUri(…) |
int imageResOnFailDrawable imageOnFail | picture download failed to display the picture | showImageOnFail(…) | |
boolean resetViewBeforeLoading | Whether to reset View before loading | false | ResetViewBeForeLoading () or resetViewBeforeloading (…) |
boolean cacheInMemory | Whether to enable a memory cache | false | cacheinmemory () or cacheinmemory (…) |
boolean cacheOnDisk | Whether to enable a disk cache | false | Cacheondisc () or cacheNDISC (…)
or cacheondisk (…) |
ImageScaleType imageScaleType | Scaling type | ImageScaleType.IN_SAMPLE_POWER_OF_2 | imageScaleType(ImageScaleType imageScaleTy) |
Options decodingOptions | is BitMapFactory.Options, for BitmapFactory.Decodestream (ImageStream, NULL, DecodingOptions) to get information and other information | Options decodingOptions = new Options(); | bitmapConfig(Bitmap.Config bitmapConfig)decodingOptions(Options decodingOptions) |
int delayBeforeLoading | Set the delay time before starting, the unit is milliseconds | 0 | delayBeforeLoading(int delayInMillis) |
boolean considerExifParams | Whether to consider the EXIF information of the picture (including the fuselage, the lens model, the shooting time, the number of the camera shutter) | false | considerExifParams(…) |
Object extraForDownloader | Auxiliary information required by the downloader | null | extraForDownloader(Object extra) |
BitmapProcessor preProcessor | The processing program before memory | null | preProcessor(…) |
BitmapProcessor postProcessor | The processing procedure after cache in memory | null | postProcessor(…) |
BitmapDisplayer displayer | picture display method | DefaultConfigurationFactory.createBitmapDisplayer(); | displayer(…) |
Handler handler | Handler object, message processing | null | handler(…) |
boolean isSyncLoading | Whether to load synchronously | false | syncLoading(…) |
(3) Custom configuration summary:
1) The default ImageLoaderConfiguration provided by ImageLoader and the default DisplayImageOptions can basically meet our normal needs. If there is no special needs, you can no longer modify it; 2) the diskCACHEHE (DISKHECHECHER (DISKHECHECHECHE method in ImageLoaderConfiguration. The method and DiskCacheFileCount () method overlap each other’s functions. If one of them is set, do not set the other;
3) DiskCache () method and diskcachefilenamegenrator () method in the builder of ImageLoaderConfiguration and DiskCacheFilenamegenrator () are overlapped. If one of them is set, do not set the other;
4) The MemoryCache () method in the builder of ImageLoaderConfiguration and the MemoryCachesize () method overlap each other. If one of them is set, do not set the other.
1.2. Supplementary understanding: Three key object relationships
1) ImageLoader:
Specific download pictures, cache pictures, displaying the specific execution class of the picture, it has two specific methods DisplayImage (…), LoadImage (…), but in fact, in the end their implementation is displayImage (…)
2) ImageLoaderConfiguration:
Global configuration of the cache, the main thread category, cache size, disk size, picture download and analysis, and log configuration
3) DisplayImageOptions:
Instruct each ImageLoader to display the corresponding picture according to the status of the network picture (blank, download error, downloading), whether to load the cache to the disk, what kind of processing of the picture after downloading is downloaded, what kind of processing the picture is processed after downloading
4) The image metaphor of the three relationships:
These three are like the company’s execution agency, company rules and regulations, and customer needs. ImageLoaderConfiguration is like a company’s rules and regulations, which stipulates the overall nature of a company; ImageLoader is like a company’s executive agency, which will give it to the company’s executive agency, which will give it to the company’s executive agency. Customers create products that customers want; DisplayImageOptions is like the specific needs of each customer. The company’s execution structure will finally decide how to produce that product based on this demand.
2. All kinds of cache methods will be for you to choose
2.1.diskcache disk cache
1) UnlimitedDISCCCACHE does not limit the cache size; 2) UnlimitedDISCCACHE is simple.
3) LIMITEDAGEDISCCACHE limit cache time;
4) LIMITEDAGEDISCCACHE This class implements files that have been loaded in the cache in the cache: When the following conditions are met, the file is deleted from the cache: the latest time of the system-the file of the file> maxfileage;
2.2.MemoryCache memory cache
)
2) USINGFREQLIMITEDCACHE (The minimum used object will be deleted)
3) USINGELIMITEDCACHE (the earliest added object will be deleted)
4) LargetLimitedCache (the largest object occupied by space will be deleted)
5) FIFOLIMITEDCACHE
2.3. Don’t forget the cleaning of cacheGenerally, the following two methods are called when the entire application is over, and the condition of the memory cache and disk cache are separated to improve the user experience:
i. imageLoader.clearMemoryCache();
ii. imageLoader.clearDiskCache();
3. Online picture acquisition and stop if you want to stop
is mainly used in the situation of girdView, ListView loading pictures, because these two controls show the sliding operation when displaying pictures. For a more friendly user experience, you can set the Setonscrollistener listening event of GirdView, ListView.
listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));gridView.setOnScrollListener(new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling));
Pauseonscroll — Whether to stop loading the picture during sliding
Pauseonfling — Whether to stop loading pictures in fast sliding
4. The size of the image is controlled by you
In the ImageLoadingListener interface, there are several states monitoring. It is used to monitor the different states of downloading pictures:
1) onLoadingStarted(String imageUri, View view);2) onLoadingFailed(String imageUri, View view, FailReason failReason);
3) onLoadingComplete(String imageUri, View view, Bitmap loadedImage):
After the picture is loaded successfully, you can set various size settings, graphic cutting operations, and animation effect adding, etc.
Finally add pictures to the control.
4) onLoadingCancelled(String imageUri, View view)。
5. Note
Use ImageLoader to effectively avoid the situation where the program loads the picture, but it is not 100 % shielding this problem. Sometimes, it will still occur. At this time, we can optimize our ImageLoader from the following ideas.
1) Reduce the size of the medium thread pool, (. ThreadPoolsize). Recommended 1-5; 2) Use .bitmapconfig (bitmap.config.rgb_65) instead of argb_8888;
3) Use .imagescaletype (ImageScaletype.in_sample_int) or Try.imageScaletype (ImageScaletype.exactly);
4) Avoid using RoundedBitMapdisplayer. He will create a new argb_8888 format in the Bitmap object;
5) Use .MemoryCache (New WeakMemoryCache ()), do not use .cacheinMemory ();