This library was based on a library of DisplayingBitmaps that can be DiskCache and MemoryCache. We improved the following things:
-
Instead of using two DiskCache targets, use only one DiskCache
-
Can specify whether to use DiskCache every acquisition of image
-
You can initialize DiskCache on the way of using
It is mandatory to initialize ImageFetcher to the onCreate class of the class that inherits Application
Declare ImageCacheParams
public ImageCacheParams (Context context, String diskCacheDirectoryName) diskCacheDirectoryName: The name of the folder where the acquired image is saved
Declare whether to use DiskCache
public void setDiskCacheEnabled (boolean enabled) Regardless of whether DiskCache is used or not, MemoryCache initializes as MemoryCacheParams initializes
Declare ImageFetcher
public ImageFetcher (Context context, ImageCache.ImageCacheParams cacheParams) cacheParams is null, do not use MemoryCache and DiskCache
Specify the image to display while acquiring the image
public void setLoadingImage (int resId)
A case
ImageCache.ImageCacheParams cacheParams = new ImageCache.ImageCacheParams (this, IMAGE_CACHE_DIR);
cacheParams.setDiskCacheEnabled (true);
mImageFetcher = new ImageFetcher (this, cacheParams);
mImageFetcher.setLoadingImage (R. drawable.empty_photo);
- As for the disk cache, in order not to display the old image, we recommend that you clear the cache data here
Acquire ImageFetcher target for each Activity or Fragment
ImageFetcher mImageFetcher = ((App) getApplication ()). GetImageFetcher ();
mImageFetcher.setListener (this);
public void load (String urlString, ImageView imageView, boolean diskCacheEnabled);
imageView: View the acquired image View target diskCacheEnabled: Specify whether to store the image in DiskCache. Even if DiskCache target does not initialize yet at initialization stage, if it is true DiskCache target can be initialized from here
A case
mImageFetcher.load (image_url, mImageView, true);
public void setExitTasksEarly (boolean exitTasksEarly)
existTaskEarly
-
By default it is false
-
If true, ignore the process to the task that is acquiring the image and complete each task ahead of time
Best practice
@Override
protected void onResume () {
super.onResume ();
mImageFetcher.setExitTasksEarly (false);
}
@Override
protected void onPause () {
super.onPause ();
mImageFetcher.setExitTasksEarly (true);
}
public boolean cancelWork (ImageView imageView)
Link ImageView to Step 3 Cancel the task of the background thread
A case
mImageFetcher.cancleWork (mImageView);
public void clearCache ()
Clear the data of MemoryCache and DiskCache. If DiskCache does not initialize yet, it can not clear the data cached by DiskCache before
A case
mImageFetcher.clearCache ();
public void closeCache ();
You can call with the onTerminate method of the class that inherits Application
A case
@Override
public void onTerminate () {
super.onTerminate ();
mImageFetcher.closeCache ();
}