Objective
Universal-imageloader is an open source picture loading framework.
Hope to be able to pass the way of the daytime source flow, understand the method of picture loading. Instance creation
An instance of Imageloader is created.
public static Imageloader getinstance () {
if (instance = null) {
synchronized (imageloader.class) {
if (inst ance = = null) {
instance = new Imageloader ()
;
}}} return instance;
}
In a practical application, Imageloader is created in the app, along with the lifecycle of the entire app, which is a single example of the related operation.
Imageloader.getinstance (). init (config);
Public synchronized void init (Imageloaderconfiguration configuration) {
if (configuration = = null) {
throw new IllegalArgumentException (Error_init_config_with_null);
}
if (this.configuration = = null) {
L.D (log_init_config);
engine = new Imageloaderengine (configuration);
this.configuration = Configuration;
} else {
L.W (warning_re_init_config);
}
}
This time we'll take a look at what Init's configurable parameters are, and how they are configured.
From the code above, you can see that the configuration is a imageloaderconfiguration class, and what the usual configuration can be.
imageloaderconfiguration config = new Imageloaderconfiguration.builder (Getappcontext ())
. Denycacheimagemultiplesizesinmemory ()
. MemoryCache (New Weakmemorycache ())
. DiskCache (New Unlimiteddiskcache (Cachedir))
. Memorycachesize (1024 * 1024).
diskcachesize (* 1024 * 1024).
Diskcachefilecount (1000)
. Writedebuglogs ()
. build ();
Imageloaderconfiguration is a typical class to be designed in builder mode, and the method of creating the specific parameters is shown in the following source code.
private void Initemptyfieldswithdefaultvalues () {if (Taskexecutor = = null) {Taskexecutor = Defaultconfiguration
Factory. Createexecutor (ThreadPoolSize, threadpriority, Tasksprocessingtype);
else {customexecutor = true;
} if (taskexecutorforcachedimages = = null) {taskexecutorforcachedimages = Defaultconfigurationfactory
. Createexecutor (ThreadPoolSize, threadpriority, Tasksprocessingtype);
else {customexecutorforcachedimages = true; } if (diskcache = = null) {if (Diskcachefilenamegenerator = = null) {Diskcachefilenamegenerator = Defaul
Tconfigurationfactory.createfilenamegenerator (); } diskcache = Defaultconfigurationfactory. Creatediskcache (Context, Diskcachefilenamegenerator, DiskCach
Esize, Diskcachefilecount); } if (memorycache = null) {MemoryCache = Defaultconfigurationfactory.creatememorycache (Context, memorycachesize
); } if (Denycacheimagemultiplesizesinmemory) {memorycache = new Fuzzykeymemorycache (MemoryCache, Memorycacheutils.createfuzzykeycomparator ());
} if (downloader = = null) {Downloader = Defaultconfigurationfactory.createimagedownloader (context);
} if (decoder = = NULL) {decoder = Defaultconfigurationfactory.createimagedecoder (writelogs);
} if (defaultdisplayimageoptions = = null) {defaultdisplayimageoptions = Displayimageoptions.createsimple (); }
}The following one by one to briefly explain: Taskexecutor
tasks of loading and displaying images taskexecutorforcachedimages
Tasks of displaying cached on disk images
Defaultconfigurationfactory
. createexecutor
Source:
public static Executor createexecutor (int threadpoolsize, int threadpriority,
queueprocessingtype Tasksprocessingtype) {
Boolean LIFO = Tasksprocessingtype = = Queueprocessingtype.lifo;
blockingqueue<runnable> taskqueue =
LIFO? New Lifolinkedblockingdeque<runnable> (): New Linkedblockingqueue<runnable> ();
return new Threadpoolexecutor (ThreadPoolSize, ThreadPoolSize, 0L, Timeunit.milliseconds, Taskqueue,
Createthreadfactory (threadpriority, "uil-pool-"));
}
Related default parameters:
/* {@value}/
public static final int default_thread_pool_size = 3;
/* {@value}/
public static final int default_thread_priority = thread.norm_priority-2;
/* {@value}/
public static final Queueprocessingtype default_task_processing_type = Queueprocessingtype.fifo;
From here you can see that the imageloader is essentially a multithreaded loading of the picture, and then these three parameters can be configured, generally using the default values. DiskCache
Defaultconfigurationfactory.createfilenamegenerator ();
The final returns are: String.valueof (Imageuri.hashcode ()), imageloader files stored in DiskCache are encoded in hashcode.
Defaultconfigurationfactory
. Creatediskcache
Interested students can own the realization of the daytime disklrucache. MemoryCache
Defaultconfigurationfactory.creatememorycache (context, memorycachesize);
public static MemoryCache Creatememorycache (context context, int memorycachesize) {
if (memorycachesize = = 0) {
A Ctivitymanager am = (activitymanager) context.getsystemservice (context.activity_service);
int memoryclass = Am.getmemoryclass ();
if (Hashoneycomb () && islargeheap (context)) {
Memoryclass = getlargememoryclass (AM);
}
Memorycachesize = 1024 * 1024 * MEMORYCLASS/8;
}
return new Lrumemorycache (memorycachesize);
}
1/8 of the available memory is allocated by default. is actually allocated through Lrumemorycache.
He and DiskCache similar, all through linkedhashmap to achieve lrucache.
When the. Denycacheimagemultiplesizesinmemory () is turned on
Set deny caching in memory a picture multiple sizes default to allow, (same picture URL) save different size pictures according to ImageView of different sizes
MemoryCache = new Fuzzykeymemorycache (MemoryCache, Memorycacheutils.createfuzzykeycomparator ());
Downloader
Provides retrieving of {@link InputStream} of Image by URI from network or file system or app.
Download device, you can see the specific
Two files under the Com.nostra13.universalimageloader.core.download.
The downloader of Imageloader is older, this piece is inferior to Picasso directly use Okhttp encapsulation. This piece of follow-up specifically describes how to customize. Decoder
Finally, call the Bitmapfactory Decodestream method for decompression
Code See:
Com.nostra13.universalimageloader.core.decode
Decodedbitmap = Bitmapfactory.decodestream (ImageStream, NULL, decodingoptions);
The specific decompression parameters will be described in the following sections.
These are some of the personal shallow solutions, only for reference.