使用ImageLoader时的基本配置和需要注意的点

来源:互联网 发布:女装淘宝店铺排名 编辑:程序博客网 时间:2024/06/05 00:35

环境Android studio2.1.1

dependencies配置
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
自定义一个Application
就像你已经知道的,首先,你需要使用ImageLoaderConfiguration对象来初始化ImageLoader。由于ImageLoader是单例,所以在程序开始的时候只需要初始化一次就好了。建议你在Activity的onCreate()方法中初始化。如果一个ImageLoader已经初始化过,再次初始化不会有任何效果。下面我们通过ImageLoaderConfiguration.Builder创建一个设置
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)        .memoryCacheExtraOptions(480,800)//这里是缓存图片允许的最大尺寸        .threadPoolSize(3)//线程池内加载数量        .threadPriority(Thread.NORM_PRIORITY - 2)//降低线程优先级,保证UI主线程不受太大影响        .denyCacheImageMultipleSizesInMemory()            .memoryCache(new WeakMemoryCache()) // 弱缓存,缓存bitmap的总大小没有限制,唯一不足的地方就是不稳定,缓存的图片容易被回收掉//      .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))//建议内存设在5-10M,可以有比较好的表现        // You can pass your own memory cache        // implementation/你可以通过自己的内存缓存实现        .memoryCacheSize(2 * 1024 * 1024)        .discCacheSize(50 * 1024 * 1024)        .discCacheFileNameGenerator(new Md5FileNameGenerator())// 将保存的时候的URI名称用MD5 加密        .tasksProcessingOrder(QueueProcessingType.LIFO)        .discCacheFileCount(100)<span style="font-family: 宋体;">// 缓存的文件数量</span>        .discCache(new UnlimitedDiskCache(new File(Environment                .getExternalStorageDirectory()                + "/myApp/imgCache")))        // 自定义缓存路径        .defaultDisplayImageOptions(getDisplayOptions())//图片加载默认配置方法自定义        .imageDownloader(                new BaseImageDownloader(this, 5 * 1000, 30 * 1000))        .writeDebugLogs() // Remove for release app        .build();// 开始构建ImageLoader.getInstance().init(config);
这里就是imageloader的基本配置,下面是图片加载的默认配置方法getDisplayOptions()
 public DisplayImageOptions getDisplayOptions() {        DisplayImageOptions options;        options = new DisplayImageOptions.Builder()                .showImageOnLoading(R.drawable.defult_good_img) // 设置图片在下载期间显示的图片                .showImageForEmptyUri(R.drawable.defult_good_img)// 设置图片Uri为空或是错误的时候显示的图片                .showImageOnFail(R.drawable.defult_good_img) // 设置图片加载/解码过程中错误时候显示的图片                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中                .cacheOnDisc(true)// 设置下载的图片是否缓存在SD卡中                .considerExifParams(true) // 是否考虑JPEG图像EXIF参数(旋转,翻转)                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)// 设置图片以如何的编码方式显示,推荐这种方式,省内存//                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型这种方式最省内存                        // .delayBeforeLoading(int delayInMillis)//int                        // delayInMillis为你设置的下载前的延迟时间                        // 设置图片加入缓存前,对bitmap进行设置                        // .preProcessor(BitmapProcessor preProcessor)                .resetViewBeforeLoading(true)// 设置图片在下载前是否重置,复位                .displayer(new RoundedBitmapDisplayer(20))// 是否设置为圆角,弧度为多少                .displayer(new FadeInBitmapDisplayer(100)) // 是否图片加载好后渐入的动画时间                .build();// 构建完成        return options;    }

imageloader的配置完成,即使使用imageloader了
ImageLoader mImageLoader=ImageLoader.getInstance();
得到imageloader尽量使用displayImage()方法去加载图片
如果app还是出现了oom的话那就在manifest文件的application中加
android:largeHeap="true"
来增大系统分配的内存,暂时就这些以后留着用

0 0
原创粉丝点击