Package com.castlabs.android.utils
Class LazyImageLoader
- java.lang.Object
-
- com.castlabs.android.utils.LazyImageLoader
-
public class LazyImageLoader extends Object
An instance of this class can be used to load images from a given URL as Bitmaps lazily and apply them to a givenImageView
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 theshutdown()
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 callerprivate 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
Constructors Constructor Description LazyImageLoader(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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clearCache()
Clears the in-memory bitmap cache and removed all cached files.void
loadImage(String url, ImageView imageView, int targetWidth)
Asynchronously download the image from the given image URL and display the resulting bitmap on the correspondingImageView
.void
loadImage(String url, String name, ImageView imageView, int targetWidth)
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 Detail
-
LazyImageLoader
public LazyImageLoader(@NonNull Context context)
Create a new lazy image loader for a given context- Parameters:
context
- the context
-
LazyImageLoader
public LazyImageLoader(@NonNull Context context, @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- 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 Detail
-
loadImage
public void loadImage(@NonNull String url, @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 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
public void setPlaceHolder(@NonNull ImageView imageView)
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.
-
-