Android图片加载之ImageLoader配置

ImageLoader作用

Android-Universal-Image-Loader是一个开源的UI组件程序,该项目的目的是提供一个可重复使用的仪器为异步图像加载,缓存和显示。

使用ImageLoader

首先我们需要导入ImageLoader的库文件

下载地址https://github.com/nostra13/Android-Universal-Image-Loader

我们也可以直接导入ImageLoder的包

implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

ImageLoader的具体配置

首先我们需要创建一个类,并且继承Application

具体代码如下

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        initLoader();
    }

    /**
     * 初始化ImageLoader
     */
    private void initLoader() {
        // DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.
        // See the sample project how to use ImageLoader correctly.
        // 缓存目录
        File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "/images");

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
                .diskCacheExtraOptions(480, 800, null)
                .threadPoolSize(3) // 线程池数量
                .threadPriority(Thread.NORM_PRIORITY - 2) // default
                .tasksProcessingOrder(QueueProcessingType.FIFO) // default
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                .memoryCacheSize(2 * 1024 * 1024)
                .memoryCacheSizePercentage(13) // default
                .diskCache(new UnlimitedDiskCache(cacheDir)) // 内存卡缓存
                .diskCacheSize(50 * 1024 * 1024)        // 内存卡缓存大小
                .diskCacheFileCount(100)        // 缓存文件数量
                .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
                //.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
                .defaultDisplayImageOptions(createDisplayOption()) // default
                .writeDebugLogs()
                .build();

        ImageLoader.getInstance().init(config);
    }

    private DisplayImageOptions createDisplayOption() {
        // DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.
// See the sample project how to use ImageLoader correctly.
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_launcher_background) // resource or drawable
                .showImageForEmptyUri(R.drawable.ic_launcher_background) // resource or drawable
                .showImageOnFail(R.drawable.ic_launcher_background) // resource or drawable
                .resetViewBeforeLoading(false)  // default
                .delayBeforeLoading(1000)
                .cacheInMemory(true) // 是否开启内存缓存
                .cacheOnDisk(true) // 是否开启SD卡缓存
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default
                .bitmapConfig(Bitmap.Config.RGB_565) // default
		        .displayer(new SimpleBitmapDisplayer()) // default
		        //.displayer(new RoundedBitmapDisplayer(40)) // default
		        //.displayer(new CircleBitmapDisplayer()) // default
                .build();
        return options;
    }

}

这个配置是从GitHub上扒下来的,default都是默认配置

一些重要的配置都打了注释,这里我把缓存的地址设置为sd卡,大家可以根据需要做其他配置。

简单配置

import android.app.Application;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;

/**
 * 自定义Application
 */
public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        initImageLoader();
    }

    /**
     * 初始化ImageLoader
     */
    private void initImageLoader() {
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration
                .Builder(getApplicationContext())
                .defaultDisplayImageOptions(options())
                .build();
        ImageLoader.getInstance().init(configuration);
    }

    private DisplayImageOptions options() {
        DisplayImageOptions options = new DisplayImageOptions
                .Builder()
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .displayer(new SimpleBitmapDisplayer())
                .build();
        return options;
    }
}

最后不要忘了在清单文件中注册


        android:name=".application.App"

在代码中的使用

在配置完成后,我们可以在java代码中直接使用ImageLoader

ImageLoader.getInstance().displayImage(url,imageView);

只需要通过一串代码,就可以把网络图片的路径加载为本地图片

第一个参数是图片路径,第二参数是布局文件中的ImageView。

完成之后就可以愉快的展示图片了。

猜你喜欢

转载自blog.csdn.net/Do_the_best_/article/details/81805600