美文网首页
An_ImageLoader_二次采样

An_ImageLoader_二次采样

作者: android_en | 来源:发表于2017-10-24 08:33 被阅读34次

    使用的网址:
    http://www.jianshu.com/p/1490965760c9

    ImageLoad是现在在Android开发过程中广泛使用的开源图片加载框架,该项目的Git源码:https://github.com/nostra13/Android-Universal-Image-Loader,ImageLoad的具有以下特点:
    1.多线程下载图片,图片可以来源于网络,文件系统,项目文件夹assets中以及drawable中等;
    2.支持随意的配置ImageLoader,例如线程池,图片下载器,内存缓存策略,硬盘缓存策略,图片显示选项以及其他的一些配置;
    3.支持图片的内存缓存,文件系统缓存或者SD卡缓存;
    4.支持图片下载过程的监听;
    5.根据控件(ImageView)的大小对Bitmap进行裁剪,减少Bitmap占用过多的内存;
    6.较好的控制图片的加载过程,例如暂停图片加载,重新开始加载图片,一般使用在ListView,GridView中,滑动过程中暂停加载图片,停止滑动的时候去加载图片;
    7.提供在较慢的网络下对图片进行加载
    
    

    ImageLoad的具体使用方法:

    1.新建一个Android项目,下载JAR包添加到工程libs目录下
    2.新建一个MyApplication继承Application,并在onCreate()中创建ImageLoader的配置参数,并初始化到ImageLoader中代码如下:

    package com.example.uil;  
    
    import com.nostra13.universalimageloader.core.ImageLoader;  
    import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;  
    
    import android.app.Application;  
    
    public class MyApplication extends Application {  
    
        @Override  
        public void onCreate() {  
            super.onCreate();  
    
            //创建默认的ImageLoader配置参数  
            ImageLoaderConfiguration configuration = ImageLoaderConfiguration  
                    .createDefault(this);  
    
            //Initialize ImageLoader with configuration.  
            ImageLoader.getInstance().init(configuration);  
        }  
    
    }
    
    

    ImageLoaderConfiguration是图片加载器ImageLoader的配置参数,使用了建造者模式,这里是直接使用了createDefault()方法创建一个默认的ImageLoaderConfiguration,当然我们还可以自己设置ImageLoaderConfiguration,设置如下:

    File cacheDir = StorageUtils.getCacheDirectory(context);  
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)  
            .memoryCacheExtraOptions(480, 800) // 内存缓存图片的最大宽高,默认为屏幕宽高
            .diskCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)  // 磁盘缓存图片的设置
            .taskExecutor(...)  // 缓存线程池
            .taskExecutorForCachedImages(...)  // 下载缓存图片的线程池
            .threadPoolSize(3) // 线程池数,默认为3
            .threadPriority(Thread.NORM_PRIORITY - 1) // 线程优先级 
            .tasksProcessingOrder(QueueProcessingType.FIFO) // 下载和显示的工作队列排序  
            .denyCacheImageMultipleSizesInMemory()  // 缓存显示不同大小的同一张图片
            .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) // 指定内存缓存的实现方式
            .memoryCacheSize(2 * 1024 * 1024)  // 设置内存缓存的最大字节
            .memoryCacheSizePercentage(13) // 设置内存缓存最大大小占当前应用可用内存的百分比
            .diskCache(new UnlimitedDiscCache(cacheDir)) // 指定磁盘缓存的实现方式
            .diskCacheSize(50 * 1024 * 1024)  // 设置磁盘缓存的最大字节
            .diskCacheFileCount(100)  // 磁盘缓存文件数
            .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // 磁盘缓存文件的命名规则
            .imageDownloader(new BaseImageDownloader(context)) // 设置图片下载器,负责从图片的各个来源获取输入流 
            .imageDecoder(new BaseImageDecoder()) // 设置图片解码器,负责将图片输入流InputStream转换为Bitmap对象
            .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // 缺省的图片显示的配置项  
            .writeDebugLogs()  // 打印debug log
            .build();
    

    上面的这些就是所有的选项配置,我们在项目中不需要每一个都自己设置,一般使用createDefault()创建的ImageLoaderConfiguration就能使用,然后调用ImageLoader的init()方法将ImageLoaderConfiguration参数传递进去,ImageLoader使用单例模式。
    3.配置Android Manifest文件

    <manifest>
     <uses-permission android:name="android.permission.INTERNET" /> 
    <!-- Include next permission if you want to allow UIL to cache images on SD card -->
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    ... 
    <application android:name="MyApplication"> ... </application> </manifest>
    

    4.加载图片
    ImageLader提供了几个图片加载的方法,主要是这几个displayImage(), loadImage(),loadImageSync(),loadImageSync()方法是同步的,android4.0有个特性,网络操作不能在主线程,所以loadImageSync()方法我们就不去使用

    1.loadImage()方法来加载网络图片
    final ImageView mImageView = (ImageView) findViewById(R.id.image);  
            String imageUrl = "http://img.showcdn.cn/uploads/allimg/160307/20-16030G15J1435.jpg";  
    
            ImageLoader.getInstance().loadImage(imageUrl, new ImageLoadingListener() {  
    
                @Override  
                public void onLoadingStarted(String imageUri, View view) {  
                      //开始加载的时候回调
                }  
    
                @Override  
                public void onLoadingFailed(String imageUri, View view,  
                        FailReason failReason) {  
                      //加载失败的时候回调
                }  
    
                @Override  
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {  
                    //加载完成的时候回调
                    mImageView.setImageBitmap(loadedImage);  
                }  
    
                @Override  
                public void onLoadingCancelled(String imageUri, View view) {  
                      //取消加载的时候回调
                }  
            });
    

    传入图片的url和ImageLoaderListener, 在回调方法onLoadingComplete()中将loadedImage设置到ImageView上面就行了,如果你觉得传入ImageLoaderListener太复杂了,我们可以使用SimpleImageLoadingListener类,该类提供了ImageLoaderListener接口方法的空实现,使用的是缺省适配器模式

    final ImageView mImageView = (ImageView) findViewById(R.id.image); 
    String imageUrl = "http://img.showcdn.cn/uploads/allimg/160307/20-16030G15J1435.jpg";
     ImageLoader.getInstance().loadImage(imageUrl, new SimpleImageLoadingListener(){ 
                  @Override 
                   public void onLoadingComplete(String imageUri, View view,
                          Bitmap loadedImage) {
                       super.onLoadingComplete(imageUri, view, loadedImage); 
                       mImageView.setImageBitmap(loadedImage); 
    } 
    });
    

    上面只是很简单的使用ImageLoader来加载网络图片,在实际的开发中,我们并不会这么使用,那我们平常会怎么使用呢?我们会用到DisplayImageOptions,他可以配置一些图片显示的选项,比如图片在加载中ImageView显示的图片,是否需要使用内存缓存,是否需要使用文件缓存等等,可供我们选择的配置如下

    DisplayImageOptions options = new DisplayImageOptions.Builder()  
            .showImageOnLoading(R.drawable.ic_stub) // 加载未完成时显示的自定义图片  
            .showImageForEmptyUri(R.drawable.ic_empty) // 链接为空时的占位图  
            .showImageOnFail(R.drawable.ic_error) // 加载失败时显示自定义图片
            .resetViewBeforeLoading(false)  // 在加载前是否重置 view ,默认为false 
            .delayBeforeLoading(1000)  //设置在开始加载前的延迟时间,单位为毫秒
            .cacheInMemory(false) // 是否启用内存缓存,默认为false  
            .cacheOnDisk(false) // 是否启用磁盘缓存,默认为false
            .preProcessor(...)  // 缓存在内存之前的处理程序
            .postProcessor(...)  // 缓存在内存之后的处理程序
            .extraForDownloader(...)  // 下载器需要的辅助信息
            .considerExifParams(false) // 是否考虑图片的 EXIF 信息,默认为false
            .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // 图片的缩放类型  
            .bitmapConfig(Bitmap.Config.ARGB_8888) // 图片的色彩格式
            .decodingOptions(...)  // 为 BitmapFactory.Options,用于得到图片尺寸等信息
            .displayer(new SimpleBitmapDisplayer()) // 图片的显示方式
            .handler(new Handler()) // handler 对象,消息处理
            .build();
    

    Demo--------------------

    package com.example.liuentong20171024recyclerview_catcherror;
    
    import android.app.Application;
    import android.graphics.Bitmap;
    import android.os.Handler;
    
    import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
    import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
    import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
    import com.nostra13.universalimageloader.core.DisplayImageOptions;
    import com.nostra13.universalimageloader.core.ImageLoader;
    import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
    import com.nostra13.universalimageloader.core.assist.ImageScaleType;
    import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
    
    import java.io.File;
    
    /**
     * 作者:author
     * 时间 :2017/10/24:8:16
     * 说明:
     */
    
    public class MyApp extends Application {
        ImageLoader imageLoader;
        @Override
        public void onCreate() {
            super.onCreate();
            File file = this.getCacheDir();
    
            DisplayImageOptions options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.mipmap.ic_launcher) // 加载未完成时显示的自定义图片
                    .showImageForEmptyUri(R.mipmap.ic_launcher) // 链接为空时的占位图
                    .showImageOnFail(R.mipmap.ic_launcher_round) // 加载失败时显示自定义图片
                    .resetViewBeforeLoading(false)  // 在加载前是否重置 view ,默认为false
                    .delayBeforeLoading(1000)  //设置在开始加载前的延迟时间,单位为毫秒
                    .cacheInMemory(true) // 是否启用内存缓存,默认为false
                    .cacheOnDisk(true) // 是否启用磁盘缓存,默认为false
                    .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // 图片的缩放类型
                    .bitmapConfig(Bitmap.Config.ARGB_8888) // 图片的色彩格式
                    .handler(new Handler()) // handler 对象,消息处理
                    .build();
            ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                    this)
                    // max width, max height,即保存的每个缓存文件的最大长宽
                    .memoryCacheExtraOptions(480, 800)
                    // 线程池内加载的数量
                    .threadPoolSize(3)
                    // 线程优先级
                    .threadPriority(Thread.NORM_PRIORITY - 2)
                    .defaultDisplayImageOptions(options)
                    // You can pass your own memory cache implementation你可以通过自己的内存缓存实现
                    // .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
                    // .memoryCacheSize(2 * 1024 * 1024)
                    //硬盘缓存50MB
                    .diskCacheSize(50 * 1024 * 1024)
                    //将保存的时候的URI名称用MD5
                    .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                    // 加密
                    .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())//将保存的时候的URI名称用HASHCODE加密
                    .tasksProcessingOrder(QueueProcessingType.LIFO)
                    .diskCacheFileCount(100) //缓存的File数量
                    .diskCache(new UnlimitedDiscCache(file))// 自定义缓存路径
                    // .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                    // .imageDownloader(new BaseImageDownloader(context, 5 * 1000,
                    // 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
                    .writeDebugLogs() // Remove for release app
                    .build();
    
            ImageLoader.getInstance().init(config);
        }
    
    }
    
    

    图片的二次采样----

    package code.feihu.com.shangfeihu_yuekao.utils;
    
    import android.graphics.BitmapFactory;
    
    /**
     * Created by
     * Emali: 210666592@qq.com
     * 类作用:
     */
    
    public class BitmapUtils {
        /**
         * @param filePath   要加载的图片路径
         * @param destWidth  显示图片的控件宽度
         * @param destHeight 显示图片的控件的高度
         * @return
         */
        public static android.graphics.Bitmap getBitmap(String filePath, int destWidth, int destHeight) {
            //第一次采样
            BitmapFactory.Options options = new BitmapFactory.Options();
            //该属性设置为true只会加载图片的边框进来,并不会加载图片具体的像素点
            options.inJustDecodeBounds = true;
            //第一次加载图片,这时只会加载图片的边框进来,并不会加载图片中的像素点
            BitmapFactory.decodeFile(filePath, options);
            //获得原图的宽和高
            int outWidth = options.outWidth;
            int outHeight = options.outHeight;
            //定义缩放比例
            int sampleSize = 1;
            while (outHeight / sampleSize > destHeight || outWidth / sampleSize > destWidth) {
                //如果宽高的任意一方的缩放比例没有达到要求,都继续增大缩放比例
                //sampleSize应该为2的n次幂,如果给sampleSize设置的数字不是2的n次幂,那么系统会就近取值
                sampleSize *= 2;
            }
            /********************************************************************************************/
            //至此,第一次采样已经结束,我们已经成功的计算出了sampleSize的大小
            /********************************************************************************************/
            //二次采样开始
            //二次采样时我需要将图片加载出来显示,不能只加载图片的框架,因此inJustDecodeBounds属性要设置为false
            options.inJustDecodeBounds = false;
            //设置缩放比例
            options.inSampleSize = sampleSize;
            //加载图片并返回
            return BitmapFactory.decodeFile(filePath, options);
        }
    
        public static android.graphics.Bitmap getBitmap(byte[] filePath, int destWidth, int destHeight) {
            //第一次采样
            BitmapFactory.Options options = new BitmapFactory.Options();
            //该属性设置为true只会加载图片的边框进来,并不会加载图片具体的像素点
            options.inJustDecodeBounds = true;
            //第一次加载图片,这时只会加载图片的边框进来,并不会加载图片中的像素点
            BitmapFactory.decodeByteArray(filePath, 0, filePath.length, options);
            //获得原图的宽和高
            int outWidth = options.outWidth;
            int outHeight = options.outHeight;
            //定义缩放比例
            int sampleSize = 1;
            while (outHeight / sampleSize > destHeight || outWidth / sampleSize > destWidth) {
                //如果宽高的任意一方的缩放比例没有达到要求,都继续增大缩放比例
                //sampleSize应该为2的n次幂,如果给sampleSize设置的数字不是2的n次幂,那么系统会就近取值
                sampleSize *= 2;
            }
            /********************************************************************************************/
            //至此,第一次采样已经结束,我们已经成功的计算出了sampleSize的大小
            /********************************************************************************************/
            //二次采样开始
            //二次采样时我需要将图片加载出来显示,不能只加载图片的框架,因此inJustDecodeBounds属性要设置为false
            options.inJustDecodeBounds = false;
            //设置缩放比例
            options.inSampleSize = sampleSize;
            //加载图片并返回
            return BitmapFactory.decodeByteArray(filePath, 0, filePath.length, options);
        }
    }
    

    demo---

    OkHttpUtils.getInstance().doGet(irb.getStories().get(position).getImages().get(0), new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
    
                }
    
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    final Bitmap bitmap = BitmapUtils.getBitmap(response.body().bytes(), 100, 200);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            holder.iv.setImageBitmap(bitmap);
                        }
                    });
                }
            });
    

    相关文章

      网友评论

          本文标题:An_ImageLoader_二次采样

          本文链接:https://www.haomeiwen.com/subject/kiwqrxtx.html