ImageLoader简单使用

1.导入依赖

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

2.写Application实现三级缓存

注:Application需要在清单文件中配置
在这里插入图片描述


(1).全局图片显示配置

public class MyImageLoader extends Application {
    private File file = new File(Environment.getExternalStorageDirectory()+"/"+"img");
    @Override
    public void onCreate() {
        super.onCreate();
        //三级缓存
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
                //设置自定义缓存目录
                .diskCache(new UnlimitedDiskCache(file))
                //线程最大数(系统默认为3)
                .threadPoolSize(3)
                //线程优先级 分为1,5,10 这里设置为Thread.NORM_PRIORITY 也就是5
                .threadPriority(Thread.NORM_PRIORITY)
                .build();
        ImageLoader.getInstance().init(configuration);
    }
}

(2).具体图片显示配置

  public static DisplayImageOptions getDisplay() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.mipmap.ic_launcher)//设置图片uri为空或错误时显示的图片
                .showImageOnFail(R.mipmap.ic_launcher)//设置图片加载/解码过程中错误时候显示的图片
                .showImageOnLoading(R.mipmap.ic_launcher)//设置图片在下载期间显示的图片
                .cacheInMemory(true)//是否缓存到内存中
                .cacheOnDisk(true)//是否缓存到SD卡中
                .displayer(new RoundedBitmapDisplayer(100))//设置图片圆角样式
                .build();
        return options;
    }

在Activity中调用

 //首先找到ImageView控件
        ImageView imager = findViewById(R.id.DrawImage);
        //实例化三级缓存方法
        ImageLoader instance = ImageLoader.getInstance();
        
        //设置图片三级缓存并设置具体图片样式
          //参数一:图片路径
          //参数二:图片的控件
         // 参数三:具体图片设置类型
        instance.displayImage(urlHeader, imager, DIsplay.getDisplay());

猜你喜欢

转载自blog.csdn.net/weixin_43566836/article/details/84255399