Universal_Image_loader

今天  在 app的引导界面   添加几张图片图片不大每个  20k左右   15张左右。

imageView1.setImageResource(R.drawable.radio_icon);结果直接OOM了!

网上  说用是 弱引用  等等 那些都是费力不讨好   实际开发中还是直接用    主流的框架比较好

1、BitmapFactory.Options   要自己来 设置 但是不一定稳定

2、然后我就想到了  使用BitmapUtils,  但是 他没有直接 添加  drawable文件夹中的  图片   (所以这里用不合适)

3、那就使用 Universal Image Loader    它可以 直接加载  来自很多 路径的图片

使用 :如下

"http://site.com/image.png"// from Web

"file:///mnt/sdcard/image.png"// from SD card

"file:///mnt/sdcard/video.mp4"// from SD card (video thumbnail)

"content://media/external/images/media/13"// from content provider

"content://media/external/video/media/13"// from content provider (video thumbnail)

"assets://image.png"// from assets

"drawable://"+ R.drawable.img// fromdrawables (non-9patch images)

但是使用之前进行  一堆的配置  直接copy过去 稍作修改就可以了

所以  综上所述 我选用 universal-image-loader

1、导入universal-image-loader     jar包

2、配置  Image loader 的设置,  只有保证在使用    实例前调用就可以了

//初始化  ImageLoader
public void initImageLoader(){
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
        .memoryCacheExtraOptions(480, 800)// max width, max height,即保存的每个缓存文件的最大长宽
        .discCacheExtraOptions(480, 800, null)
        // Can slow ImageLoader, use it carefully (Better don't use it)//设置缓存的详细信息,最好不要设置这个
        .threadPoolSize(3)// 线程池内加载的数量
        .threadPriority(Thread.NORM_PRIORITY - 2)
        .denyCacheImageMultipleSizesInMemory()
        .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
        // You can pass your own memory cache
        // implementation/你可以通过自己的内存缓存实现
        .memoryCacheSize(2 * 1024 * 1024)
        .discCacheSize(50 * 1024 * 1024)
        .discCacheFileNameGenerator(new Md5FileNameGenerator())//将缓存的名字用URLMD5命名
        .tasksProcessingOrder(QueueProcessingType.LIFO)
        .discCacheFileCount(100)// 缓存的文件数量
        .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);  //设置生效配置
}

//图片 参数设置
private DisplayImageOptions getDisplayOptions() {
    DisplayImageOptions options;
    options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.android) // 设置图片在下载期间显示的图片
            .showImageForEmptyUri(R.drawable.android)// 设置图片Uri为空或是错误的时候显示的图片
            .showImageOnFail(R.drawable.android) // 设置图片加载/解码过程中错误时候显示的图片
            .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
            .cacheOnDisc(true)// 设置下载的图片是否缓存在SD卡中
            .considerExifParams(true) // 是否考虑JPEG图像EXIF参数(旋转,翻转)
            .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;
}

3、实例化并添加图片

imageView1 = (ImageView) view.findViewById(R.id.iv_image1);
imageView2 = (ImageView) view.findViewById(R.id.iv_image2);
imageView3 = (ImageView) view.findViewById(R.id.iv_image3);
imageView4 = (ImageView) view.findViewById(R.id.iv_image4);
imageLoader = ImageLoader.getInstance();
imageLoader.displayImage("drawable://"+R.drawable.disc_vinyl_icon,imageView1);
imageLoader.displayImage("drawable://"+R.drawable.device_tv_icon,imageView2);
imageLoader.displayImage("drawable://"+R.drawable.headphone_icon,imageView3);
imageLoader.displayImage("drawable://"+R.drawable.radio_icon,imageView4);





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值