As an ordinary person, I can’t create a complete image loading library, but ordinary people can choose which good library to use for image loading.

Github has many excellent image loading libraries: UIL, Picasso, Glide, Fresco, etc. All of them are worthy of our lifelong admiration…

However, we often encounter the need to switch between different libraries. For example, the other day, I switched from UIL to Glide, which makes the app smoother and more comfortable to load images.

So the question is, how do you switch quickly? It’s a design problem.

After searching for ImageLoader, I found it useful in many places and it was a pain to change. With that in mind, I rewrote the image loading part.

According to our analysis, we often use these poses:

  • Load from file
  • From the Assets to load
  • Load from res
  • Load from the network
  • Clear Disk Cache
  • Clear Memory cache
  • Resume loading
  • Pause to load…

Interfaces are a good thing!

step1

First, we define an inner class Options:

public int loadingResId = RES_NONE; Public int loadErrorResId = RES_NONE; Public static final int RES_NONE = -1;Copy the code

The two variables are the resource in load and the resource that failed to load

step 2

I define an abstract class LoadCallback to load state callbacks:

public abstract class LoadCallback { void onLoadFailed(Throwable e) { } public abstract void onLoadReady(Bitmap bitmap);  void onLoadCanceled() { } }Copy the code

OnLoadFailed loads failed callback. You can overload onLoadCanceled callback. You can overload onLoadReady callback that loads successfully

step3

Define interface ILoader, mainly define some operation apis:

void init(Context context); Init void loadNet(ImageView target, String URL, Options Options); Void loadNet(Context Context, String URL, Options Options, LoadCallback callback); // Load network resources (with callback) void loadResource(ImageView target, int resId, Options Options); Void loadAssets(ImageView Target, String assetName, Options Options); Void loadFile(ImageView target, File File, Options Options); // Load file void clearMemoryCache(Context Context); //clear memory cache void clearDiskCache(Context Context); //clear disk cache void resume(Context Context); //resume load void pause(Context Context); / / pause to loadCopy the code

step4

GlideLoader implements ILoader

step5

I made a singleton ILFactory

public static ILoader getLoader() {
        if (loader == null) {
            synchronized (ILFactory.class) {
                if (loader == null) {
                    loader = new GlideLoader();
                }
            }
        }
        return loader;
    }Copy the code

How to call

ILFactory.getLoader().loadNet(target,url,options)Copy the code

Very important question. How do I switch?

For example, if I wanted to switch to Picasso, how would I switch? First, write a class that implements the ILoader interface, such as PicassoLoader, which of course can be implemented using Picasso

void loadNet(ImageView target, String url, Options options){
    Picasso.with(context)
            .load(url)
            ....
            .into(imageView);
}Copy the code

Then change GlideLoader to PicassoLoader in ILFactory and you don’t need to change a line of code elsewhere..

How w do you have a better way, I hope you can tell me.

Recently, I got a lot of free time and started a library to celebrate my two years of Android development

Github.com/limedroid/X…

This is a lightweight rapid development framework that combines my two years of development experience and has been proven on several projects. It is not a simple collection of utility classes, but contains its own understanding of Java and Android. Hope to have a fragment design, and you echo.

If you are big, want to get your Pointers, and reflect on it, to make Xdroid better.