ImageLoader
Image loader library for Android. Allows you to build your own image loader that will do exactly what you want. Almost unlimited customization.
Usage
dependencies {
implementation 'com.budiyev.android:image-loader:1.8.2'
}
Basic usage sample
Basic implementation automatically cares about memory caching storage caching
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView view = findViewById(R.id.image_view);
ImageLoader.with(this).from("https://some.url/image").into(view).load();
}
}
Simple singleton implementation
You can build your own loader using builder
/** * Simple image loader that automatically cares about memory and storage caching, * read documentation for more info */ public final class MyImageLoader {
private static volatile MyImageLoader sInstance;
private final ImageLoader<Uri> mLoader;
private MyImageLoader(@NonNull Context context) {
mLoader = ImageLoader.builder(context).uri().memoryCache().storageCache().build();
}
/**
* Load image form {
@code url
}
to {
@code view
}
*
* @param url Source URL
* @param view Target image view
*/
@MainThread
public void load(@NonNull String url, @NonNull ImageView view) {
mLoader.load(DataUtils.descriptor(Uri.parse(url)), view);
}
/**
* Get (or create, if not exist) loader instance
*
* @param context Context
* @return Loader instance
*/
@NonNull
public static MyImageLoader with(@NonNull Context context) {
MyImageLoader instance = sInstance;
if (instance == null) {
synchronized (MyImageLoader.class) {
instance = sInstance;
if (instance == null) {
instance = new MyImageLoader(context);
sInstance = instance;
}
}
}
return instance;
}
/**
* Clear memory cache if it exists, would be good to call this method in
* {
@link Application#onTrimMemory(int)
}
method,
* read documentation for more info
*/
public static void clearMemoryCache() {
MyImageLoader instance = sInstance;
if (instance != null) {
instance.mLoader.clearMemoryCache();
}
}
}
Implementation usage sample
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView view = findViewById(R.id.image_view);
MyImageLoader.with(this).load("https://some.url/image", view);
}
}