Android中的Universal-Image-Loader的

2017-03-18  本文已影响0人  Jackson杰

大家在做Android开发时,经常需要用到异步加载图片,在这里主要介绍最常用的一个工具Universal-Image-Loader,相信很多朋友都听过或者使用过这个强大的图片加载框架。有关Universal-Image-Loader的使用可以参考 https://github.com/nostra13/Android-Universal-Image-Loader,看一看到主要有以下特征:
Features
Multithread image loading (async or sync)
Wide customization of ImageLoader's configuration (thread executors, downloader, decoder, memory and disk cache, display image options, etc.)
Many customization options for every display image call (stub images, caching switch, decoding options, Bitmap processing and displaying, etc.)
Image caching in memory and/or on disk (device's file system or SD card)
Listening loading process (including downloading progress)

Android 2.0+ support
接下来看一下Universal-Image-Loader开源库的使用。在这里要说明一下我用到的开发工具是Android Studio。

1.创建一个Android工程目录,并把Universal-Image-Loader放在libs文件夹下。

20151231102826331.jpg

2.配置ImageLoaderConfiguration,这个是图片加载器ImageLoader的配置参数。可以选择在Application中配置,目的的每次用到的时候不用重复写代码。

新建一个MyApplication继承Application,并在onCreate()中创建ImageLoader的配置参数

public class MyApplication extends Application {  
    @Override  
    public void onCreate() {  
        super.onCreate();  
        initImageLoader(getApplicationContext());  
    }  
  
    private void initImageLoader(Context context){  
        /**定义缓存文件的目录**/  
        File cacheDir= StorageUtils.getOwnCacheDirectory(getApplicationContext(),"Cache/");  
        /**ImageLoader的配置**/  
        ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(context)  
                .threadPriority(Thread.NORM_PRIORITY-2) //设置同时运行的线程  
                .denyCacheImageMultipleSizesInMemory()  //缓存显示不同大小的同一张图片  
                .diskCacheSize(50*1024*1024)  //50MB SD卡本地缓存的最大值  
                .diskCache(new UnlimitedDiscCache(cacheDir)) //SD卡缓存  
                .memoryCache(new WeakMemoryCache()) //内存缓存  
                .tasksProcessingOrder(QueueProcessingType.LIFO).build();  
        //全局初始化配置  
        ImageLoader.getInstance().init(config);  
    }  
}  ```
后面附上网上比较全的配置信息,实际上不用每一个都用到。

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.diskCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
.taskExecutor(...)
.taskExecutorForCachedImages(...)
.threadPoolSize(3) // default
.threadPriority(Thread.NORM_PRIORITY - 1) // default
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.memoryCacheSizePercentage(13) // default
.diskCache(new UnlimitedDiscCache(cacheDir)) // default
.diskCacheSize(50 * 1024 * 1024)
.diskCacheFileCount(100)
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
.imageDownloader(new BaseImageDownloader(context)) // default
.imageDecoder(new BaseImageDecoder()) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs()
.build(); ```

不要忘了在AndroidManifest中注册MyApplication,加入权限。

3. ImageLoader加载图片

使用ImageLoader加载图片时,首先要实例化ImageLoader。ImageLoader的实例化采用的是单例模式。
ImageLoader loader=ImageLoader.getInstance();

ImageLoader加载图片分为两种情况,一种是默认的加载。

loader.displayImage(imgUrl,img);//参数1--要加载图片的url地址,参数2--要显示图片的控件,可以为ImageView

另一种是通过DisplayImageOptions 自定义要显示的图片。
加载图片的方法是:
loader.displayImage(imgUrl,img,options);
options在下面定义

DisplayImageOptions options=new DisplayImageOptions.Builder()  
               .showImageOnLoading(drawableID) //设置正在下载时显示的图片  
               .showImageForEmptyUri(drawableID) //设置Url为空时显示的图片  
               .showImageOnFail(drawableID)   //设置图片下载失败时显示的图片  
               .cacheInMemory(true)  //允许图片保存到手机内存  
               .cacheOnDisk(true)  //允许图片保存到SD卡中  
               .considerExifParams(true)  //是否考虑JPEG图像EXIF参数(旋转,翻转)  
               .bitmapConfig(Bitmap.Config.RGB_565)  
               .build();  //构建完成  ```

附上网上比较全的配置信息  

<pre name="code" class="java">DisplayImageOptions options;
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher) //设置图片在下载期间显示的图片
.showImageForEmptyUri(R.drawable.ic_launcher)//设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(R.drawable.ic_launcher) //设置图片加载/解码过程中错误时候显示的图片
.cacheInMemory(true)//设置下载的图片是否缓存在内存中
.cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中
.considerExifParams(true) //是否考虑JPEG图像EXIF参数(旋转,翻转)
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//设置图片以如何的编码方式显示
.bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型//
.decodingOptions(android.graphics.BitmapFactory.Options decodingOptions)//设置图片的解码配置
//.delayBeforeLoading(int delayInMillis)//int delayInMillis为你设置的下载前的延迟时间
//设置图片加入缓存前,对bitmap进行设置
//.preProcessor(BitmapProcessor preProcessor)
.resetViewBeforeLoading(true)//设置图片在下载前是否重置,复位
.displayer(new RoundedBitmapDisplayer(20))//是否设置为圆角,弧度为多少
.displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间
.build();//构建完成 ```

4.注意事项

5.从其他路径加载图片的url

多线程异步加载和显示图片(图片来源于网络、sd卡、assets文件夹,drawable文件夹(不能加载9patch),新增加载视频缩略图)

上一篇 下一篇

猜你喜欢

热点阅读