https://github.com/nostra13/Android-Universal-Image-Loader
UIL can be regarded as the old most popular image loading library, the use of this framework can be said to teach you how to be a person, I first put the third party open source image loading framework into the project is this, at that time I felt instantly forced to rise, my mother no longer need to worry about OOM and ListView picture confusion. Unfortunately, the author stated in the project that he had stopped the maintenance of the project. This means that no future bugs will be fixed and no new features will be developed, so UIL is definitely not recommended for use in projects.
Usage:
1, the Application of global variables to configure ImageLoaderConfiguration, selective, specific code is as follows:
ImageLoaderConfiguration config =newImageLoaderConfiguration.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)// Number of thread pools in default
.threadPriority(Thread.NORM_PRIORITY – 1)// default
.tasksProcessingOrder(QueueProcessingType.FIFO)// default
.denyCacheImageMultipleSizesInMemory()
.memorycache (newLruMemoryCache(2 * 1024 * 1024))// memoryCache
.memoryCacheSize(2 * 1024 * 1024)
.disccache (newUnlimitedDiscCache(cacheDir))// Disk cache
.discCacheSize(50 * 1024 * 1024)
.discCacheFileCount(100)
.discCacheFileNameGenerator(newHashCodeFileNameGenerator())// default
.imageDownloader(newBaseImageDownloader(context))// default
.imageDecoder(newBaseImageDecoder())// default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())// default
.enableLogging()
.build();
2. Configure DisplayImageOptions for each load task
DisplayImageOptions options =newDisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.resetViewBeforeLoading()
.delayBeforeLoading(1000)
.cacheInMemory()
.cacheOnDisc()
.preProcessor(…)
.postProcessor(…)
.extraForDownloader(…)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)// default
.bitmapConfig(Bitmap.Config.ARGB_8888)// default
.decodingOptions(…)
.displayer(newSimpleBitmapDisplayer())// default
.handler(newHandler())// default
.build();
UIL supports the following image loading formats:
String imageUri =”site.com/image.png”; // from Web
String imageUri =”file:///mnt/sdcard/image.png”; // from SD card
String imageUri =”content://media/external/audio/albumart/13″; // from content provider
String imageUri =”assets://image.png”; // from assets
String imageUri =”drawable://”+ R.drawable.image; // from drawables (only images, non-9patch)
Call the imageloader. displayImage method and you’ll be fine!
Here is a simple analysis of the loading principle of the UIL framework:
1. ImageLoader, the main EXTERNAL API, adopts the singleton mode for loading and displaying images.
2, MemoryCache image memory. The LRU algorithm is used by default. LRU: Least Recently Used algorithm selects LinkedHashMap based on linked list structure as storage structure. Hypothetical scenario: The memory cache sets a threshold that is sufficient to store only two bitmaps, and when a third bitmap is put, the least recently used bitmap is removed.
DiskCache image cache. LruDiskCache algorithm is used by default. When the cache is full, the least recently used image is deleted. A file named Journal in the cache directory records all operations of the cache
4. Picture loading process
1. Check whether the image cache exists. If yes, go to Step 8.
2. Check whether the disk cache exists. If yes, go to Step 5.
3.ImageDownloader downloads images from the network.
4. Save the image to the disk.
5.ImageDecoder decode images into bitmap objects;
6. The BitmapProcessor pre-processes the Bitmap according to the DisplayImageOptions configuration;
7. Cache the Bitmap object into memory;
8. Post-process Bitmap according to DisplayImageOptions configuration;
9. Execute DisplayBitmapTask to display the image on the corresponding control.
Second, Picasso was
https://github.com/square/picasso
Picasso is an open source Android image-loading framework from Square. It’s so easy to use that it’s incredibly easy to load images for projects in a single phrase.
Picas.with (this).load(” URL “).placeholder(r.map.ic_default).into(imageView);
Brief analysis of principle:
Picasso. With (Context): Picasso
public static Picasso with(Contextcontext){
if(singleton==null){
synchronized(Picasso.class){
if(singleton==null){
singleton=newBuilder(context).build();
}
}
}
return singleton; }
Single-column mode ensures that there is only one instance in the case of multiple threads.
/** Create the {@link Picasso} instance. Create an instance of Picasso */
public Picassobuild(){
Context context=this.context;
if(downloader==null){
downloader=Utils.createDefaultDownloader(context);
}
if(cache==null){
cache=new LruCache(context);
}
if(service==null){
service=new PicassoExecutorService();
}
if(transformer==null){
transformer=RequestTransformer.IDENTITY;
}
Stats stats=newStats(cache);
Dispatcher dispatcher=new Dispatcher(context,service,HANDLER,downloader,cache,stats);
return new Picasso(context,dispatcher,cache,listener,transformer,
requestHandlers,stats,indicatorsEnabled,loggingEnabled);
}
The following parameters are initialized by default:
Downloader
DownLoader is the utility class used for downloading. In Picasso, OKHttp is used by default if it is available, and if it is not available, UrlConnectionDownloader(implemented using HttpURLConnection by default) is used.
Cache
The default implementation is LruCache, which is a Cache class implemented using LinkedHashMap. One thing to note is that in other places, we typically limit capacity by default, but in this case we limit the total amount of memory used. Therefore, when LruCache is implemented, it is simply to encapsulate the LinkedHashMap and implement the Cache method based on the LinkedHashMap method. When the Cache set() method is used, it will continuously calculate the size of the available space. The previously saved data is deleted.
ExecutorService
The default implementation is PicassoExecutorService, which is a relatively simple class, called ThreadPoolExecutor, that continues to encapsulate its functionality. Picasso used the PicassoExecutorService to set the number of threads to adjust performance on 2G/3G/4G/WiFi networks.
RequestTransformer
The ReqeustTransformer interface is used to pre-process Reqeust, which can be used to pre-process requests such as changing a domain name.
Stats
Statistics such as cache hits/misses, total file sizes downloaded, number of images downloaded, number of images converted, etc.
Dispatcher
In Picasso, the thread that dispatches tasks, which is a class that we’ll focus on later, just to mark it out, this Dispatcher does the following:
The DispatcherThread thread initializes a DispatcherHandler for processing messages. Note that by default, this Handler handles all data above the DispatcherThread. Initializes and registers a network status broadcast receiver.
2. Picture loading process:
1. Initialize Picasso, instantiating his unique object.
2. Build the ReqeustCreator object based on the passed Url, File, and resource Id
3. Build the Request object according to ReqeustCreator, and attempt to access data from the Cache according to the Reqeust attribute
4.Cache Hit: Completes the Reqeust by setting the Target or ImageView callback
5. If Cache Miss, create Action and submit it to DispatcherThread.
6. The Handler of Dispatcher receives the corresponding Message and calls Dispatcher.performSubmit (Action) to process it.
Create a BitmapHunter object and submit it to the PicassoExecutorService thread pool
8. Check that there is a Cache in the Cache. If Hit, read the Bitmap in the Cache
9. If a Cache miss occurs, the Action’s corresponding ReqeustHandler will handle it, such as a network request or reading an image from a File
10. After the result is returned, notify the Handler in the Dispatcher to process the result.
11. Batch the BitmapHunter results in the DispatcherThread, and pack them once at the fastest 200ms. Notify the main thread HANDLER for processing
12. The main thread HANDLER receives the packaged BitmapHunter and distributes the final result.
Note: The Picasso framework does not implement disk caching; it works with OkHttp.
Third, Glide
https://github.com/bumptech/glide
Glide, the work of a Google employee, is based entirely on Picasso, following the simple style of Picasso, but with a lot of refinement and improvement.
Glide’s default Bitmap format is RGB_565, while Picasso’s default Bitmap format is ARGB_8888, which has half the memory overhead.
In terms of disk caching, Picasso will only cache raw images, whereas Glide will cache multiple sizes, which means Glide will cache image sizes based on the size of your ImageView. For example, if your ImageView size is 200 x 200, The original was 400 by 400, whereas Glide caches 200 by 200, Picasso caches only 400 by 400. This improvement would result in Glide loading faster than Picasso, with less rerendering per cropping.
One of the most important features is that Glide supports loading Gif animations, which Picasso does not.
In addition, many other configuration options have been added.
In general, Glide is a redevelopment of Picasso, with a lot of improvements in all aspects, but it also resulted in a much larger bag than Picasso, but less than 500K, whereas Picasso was more than 100K, and a lot more ways than Picasso. But after all, the level is still quite small, the impact is not very big.
Four, Fresco”
https://github.com/facebook/fresco
Fresco is a new image loading library created by Facebook. We know that Android apps have limited memory available, so they often get OOM when loading images. We have a lot of tools to optimize and minimize the possibility of OOM, but we can never avoid it. Especially some low-end phones OOM is more serious. Facebook took a different approach, and since we couldn’t do it in the Java layer, we did it in the Native heap at the lower level. So Fresco puts the images in a special memory area called the Ashmem heap, which is the Native heap. The images will no longer occupy the memory of the App. There is nothing the Java layer can do about this.
I have used all four libraries, but Fresco is really powerful. Loading large pictures is the best. Some pictures Glide and Picasso can’t be loaded, so I have no problem with using Fresco. Glide and Picasso are enough for normal apps!