Package com.castlabs.android.utils
Class LazyImageLoader
java.lang.Object
com.castlabs.android.utils.LazyImageLoader
An instance of this class can be used to load images from a given URL as Bitmaps lazily
and apply them to a given
ImageView
once loaded.
The image loader offers in-memory caching for bitmaps as well as local file caching for
downloaded content.
This implementation manages its own executor thread and if you use it, make sure you shut down
the image loader using the shutdown()
method wither when all your resources are loaded,
or, once the parent activity is destroyed. For example, in your activity use the image loader
like this:
Note that the API needs WRITE_EXTERNAL_STORAGE permission which should be added and handled by the caller
private LazyImageLoader imageLoader;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.imageLoader = new LazyImageLoader(this);
}
protected void onDestroy() {
super.onDestroy();
this.imageLoader.shutdown();
}
- Since:
- 1.0.0
-
Constructor Summary
ConstructorsConstructorDescriptionLazyImageLoader
(Context context) Create a new lazy image loader for a given contextLazyImageLoader
(Context context, int defaultImg) Create a new lazy image loader for a give context with a default image id that is applied to all image viewsLazyImageLoader
(Context context, String sdcardDirName, int defaultImg) Create a new lazy image loader for a give context with a default image id that is applied to all image views. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Clears the in-memory bitmap cache and removed all cached files.void
Asynchronously download the image from the given image URL and display the resulting bitmap on the correspondingImageView
.void
Asynchronously download the image from the given image URL and display the resulting bitmap on the correspondingImageView
.void
setPlaceHolder
(ImageView imageView) Apply the configured placeholder drawable to the given image view.void
shutdown()
Shut down this image loader.
-
Constructor Details
-
LazyImageLoader
Create a new lazy image loader for a given context- Parameters:
context
- the context
-
LazyImageLoader
Create a new lazy image loader for a give context with a default image id that is applied to all image views- Parameters:
context
- the contextdefaultImg
- resource id of a default image
-
LazyImageLoader
public LazyImageLoader(@NonNull Context context, @Nullable String sdcardDirName, @DrawableRes int defaultImg) Create a new lazy image loader for a give context with a default image id that is applied to all image views. This image loader uses a sub directory created under theEnvironment.getExternalStorageDirectory()
if its available.- Parameters:
context
- teh contextsdcardDirName
- the sub folder name underEnvironment.getExternalStorageDirectory()
used as cache folderdefaultImg
- the default image resource id
-
-
Method Details
-
loadImage
Asynchronously download the image from the given image URL and display the resulting bitmap on the correspondingImageView
.- Parameters:
url
- Image download urlimageView
- View which will render the imagetargetWidth
- If width is different from -1 the image will be scaled down while decoding. In-app memory will be saved- Since:
- 1.2.0
-
loadImage
public void loadImage(@NonNull String url, @NonNull String name, @NonNull ImageView imageView, int targetWidth) Asynchronously download the image from the given image URL and display the resulting bitmap on the correspondingImageView
.- Parameters:
url
- Image download urlname
- Image key name for cacheimageView
- View which will render the imagetargetWidth
- If width is different from -1 the image will be scaled down while decoding. In-app memory will be saved- Since:
- 1.2.0
-
setPlaceHolder
Apply the configured placeholder drawable to the given image view.- Parameters:
imageView
- the image view
-
shutdown
public void shutdown()Shut down this image loader. You will not be able to use it anymore after this method was called.- Since:
- 1.2.0
-
clearCache
public void clearCache()Clears the in-memory bitmap cache and removed all cached files.
-