ImageLoader 基本介绍及使用

来源:互联网 发布:大数据的定义是什么 编辑:程序博客网 时间:2024/04/29 00:12

前几天做项目的时候因为暂时没有服务器,所以图片全部存在了工程里面,因为屏幕适配的原因,图片全部都是高清大图,一个图片就有1.8MB,下面有浏览区,上面是个大图



 小图是提供了,但是大图太大 放在imageView中 一张还好,点击第二张小图,一点就OOM了,开始想的特别好,用BitmapFactory 把图片进行缩放,缩放后OOM出现的频率降低了,但是效率特别慢,连跳转到该页面都需要3秒,身为技术的我都不能忍受这么慢的速度,点击下面的小图也是要等2-3秒,上面的大图才会相应的改变。后来一个朋友给我介绍了 ImageLoader 这个神级的东西,给我截图,一步一步的步骤都给我说了,但是套在自己的项目上就是有问题,然后问的朋友都无语了,只好自己在网上查关于imageLoader的文章来学习,看了好几篇都不是我想要的,因为步骤和朋友给我发的一模一样,我真找不出什么不同,后来自己摸索着改好了,不多说了,上代码了

首先要导入imageLoader的jar包到你的libs目录下         jia包下载地址:http://pan.baidu.com/s/1kTxMOQj



然后 要建一个继承自Appliaction的类,在里面配置ImageLoader的参数

public class FFApplication extends Application{        private static FFApplication mApplication;    public void onCreate()    {        // TODO Auto-generated method stub        super.onCreate();        mApplication = this;        // 创建默认的ImageLoader配置参数        initImageLoader(this);}public static FFApplication getInstance()    {        return mApplication;    }

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

/***     *      * @Title: initImageLoader     * @Description: 初始化ImageLoader     * @param @param context 设定文件     * @return void 返回类型     * @throws     */    public void initImageLoader(Context context)    {        ImageLoaderConfiguration config =            new ImageLoaderConfiguration.Builder(context).threadPriority(Thread.NORM_PRIORITY - 2)                .denyCacheImageMultipleSizesInMemory()                .discCacheFileNameGenerator(new Md5FileNameGenerator())                .tasksProcessingOrder(QueueProcessingType.LIFO)                .writeDebugLogs()                .threadPoolSize(2)                .memoryCache(null)                .build();        ImageLoader.getInstance().init(config);    }

之前我以为showImageForEmptyUri(sexyclothings[resId]) 就是加载图片了,我直接把大图放在了这些括号里,加载2次后就报OOM

    public DisplayImageOptions getOptions(int resId)    {     // 使用DisplayImageOptions.Builder()创建DisplayImageOptions         DisplayImageOptions myOptions = new DisplayImageOptions.Builder().showStubImage(sexyclothings[resId]) // 设置图片下载期间显示的图片            .showImageForEmptyUri(sexyclothings[resId])// 设置图片Uri为空或是错误的时候显示的图片            .showImageOnFail(sexyclothings[resId])// 设置图片加载或解码过程中发生错误显示的图片            .cacheInMemory(true)// 设置下载的图片是否缓存在内存中            .cacheOnDisc(true)// 设置下载的图片是否缓存在SD卡中            .bitmapConfig(Bitmap.Config.RGB_565)// 设置下载的图片是否缓存在SD卡中            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)            .build();   // 创建配置过得DisplayImageOption对象        return myOptions;    }
displayImage()加载图片


现在要加载图片了,我查了几个博文后 觉的最方便的还是displayimage(),因为我是读取的本地图片,所以 要生成一个本地图片的url

  /** 大图片源数组 */    private final Integer clothings[] = new Integer[] {R.drawable.sexy_img_big0, R.drawable.sexy_img_big1,        R.drawable.sexy_img_big2, R.drawable.sexy_img_big3};        /** 大图片对应的缩略图源数组 */    private final int sexyclothings[] = new int[] {R.drawable.sexy_img_s0, R.drawable.sexy_img_s1,        R.drawable.sexy_img_s2, R.drawable.sexy_img_s3};    String drawableUrl = Scheme.DRAWABLE.wrap(clothings[pieceNumberNow] + "");<span><span class="comment">String imageUrl = "http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg";</span><span>  //也可以直接加载网络图片</span></span> ImageLoader.getInstance().displayImage(drawableUrl,            iv_belle_game_picture,            maApplication.getInstance().getOptions(clothings[pieceNumberNow]));

 千万不要忘记在清单文件中配置你的Application哦

    <application        android:name="com.imatrixmob.ffime.common.base.FFApplication"        android:icon="@drawable/fflogo"        android:label="@string/app_name_IME"        android:windowSoftInputMode="adjustResize"        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >



0 0
原创粉丝点击