ImageLoaderFramework Documentation

repository·master·Indexed 19 days ago

https://github.com/ladingwu/imageloaderframework

An abstraction layer for Android image loading that unifies Glide and Fresco, allowing developers to switch between these libraries without changing business logic. Features include a fluent API for image loading with support for Gaussian blur, circular cropping, placeholders, and real-time loading progress monitoring via OnLoaderProgressCallback.

Tokens
1.4K
Snippets
6
Records
6
Agent score
15%

What's inside ImageLoaderFramework

  1. Initialize ImageLoaderManager

    master

    Initialization must be performed in your Application class. You use ImageLoaderConfig.Builder to specify the loader type (via LoaderEnum) and the specific loader implementation (e.g., GlideImageLocader). You can also configure the memory cache size in bytes using .maxMemory().

    // Initialization code in Application
    ImageLoaderConfig config = new ImageLoaderConfig.Builder(LoaderEnum.GLIDE, new GlideImageLocader())
            .maxMemory(40 * 1024 * 1024L)  // Configure memory cache in Bytes
            .build();
    ImageLoaderManager.getInstance().init(this, config);
  2. Install ImageLoaderFramework

    master

    To use the framework, add the core library and choose one of the underlying implementation libraries (Glide or Fresco) to your dependencies. You must include imageloader-framework, but you only need one of the implementation packages.

    // Choose one of these two based on your needs
    compile 'com.ladingwu.library:fresco:0.0.9'
    compile 'com.ladingwu.library:glide:0.0.9'
    
    // This is required
    compile "com.ladingwu.library:imageloader-framework:0.0.9"
  3. Load images using the fluent API

    master

    Since version 0.0.9, you can use a streamlined, chainable API to load images in a single step. This method is preferred for its conciseness and supports features like Gaussian blur, circular cropping, and placeholders.

    ImageLoader.createImageOptions(img2, url)
            .blurImage(true)
            .blurValue(35)
            .isCircle()
            .placeholder(R.mipmap.ic_launcher)
            .build()
            .show();
  4. Load images using ImageLoaderManager (Legacy API)

    master

    The older API involves building an ImageLoaderOptions object and passing it to ImageLoaderManager.getInstance().showImage(options). This method is still supported for backward compatibility.

    // Load rounded image
    ImageLoaderOptions op = new ImageLoaderOptions.Builder(img1, url).imageRadiusDp(12).build();
    ImageLoaderManager.getInstance().showImage(op);
    
    // Advanced options
    ImageLoaderOptions options = new ImageLoaderOptions.Builder(img2, url)
            .blurImage(true)             // Gaussian blur
            .blurValue(35)               // Blur intensity
            .isCircle()                  // Circular image
            .placeholder(R.mipmap.ic_launcher)
            .build();
    
    // If using both Fresco and Glide, you can force a specific loader
    ImageLoaderManager.getInstance().showImage(options, LoaderEnum.GLIDE);
  5. Handle real-time loading progress

    master

    You can monitor the loading progress of an image by attaching an OnLoaderProgressCallback to the options. This works with both the new fluent API and the legacy API.

    // New API (0.0.9+)
    ImageLoader.createImageOptions(img1, url)
        .setOnLoaderProgressCallback(new OnLoaderProgressCallback() {
            @Override
            public void onProgress(int progress) {
                Log.w("progress", "" + progress);
            }
        })
        .imageRadiusDp(12)
        .build()
        .show();
    
    // Legacy API
    ImageLoaderOptions op = new ImageLoaderOptions.Builder(img1, url)
        .setOnLoaderProgressCallback(new OnLoaderProgressCallback() {
            @Override
            public void onProgress(int progress) {
                Log.w("progress", "" + progress);
            }
        })
        .imageRadiusDp(12)
        .build();
    ImageLoaderManager.getInstance().showImage(op);
  6. Pause and resume image loading

    master

    The framework provides methods to pause and resume image loading via the ImageLoaderManager to optimize performance during specific lifecycle events or user interactions.

    // Pause loading
    void pause(Context context);
    
    // Resume loading
    void resume(Context context);