设为首页 加入收藏

TOP

ImageLoader配合ImageSwitcher的使用(一)
2017-10-12 17:57:28 】 浏览:2494
Tags:ImageLoader 配合 ImageSwitcher 使用

先在MyApplication中初始化ImageLoader

initImageLoader(getApplicationContext());
   /**
    * 初始化ImageLoader
    * 如果你经常出现oom
    * 减少配置的线程池的大小(.threadPoolSize(...)),建议1~5
    * 配置中使用.diskCacheExtraOptions(480, 320, null)
    * @param context
    */
    public static void initImageLoader(Context context) {
        // This configuration tuning is custom. You can tune every option, you may tune some of them, 
        // or you can create default configuration by
        // ImageLoaderConfiguration.createDefault(this);
        // method.
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .threadPriority(Thread.NORM_PRIORITY - 1)
                .threadPoolSize(5)
                .denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .discCacheSize(100 * 1024 * 1024)
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                //.enableLogging() // Not necessary in common
                .build();
        // Initialize ImageLoader with configuration.
        MyImageLoader.getInstance().init(config);
    }

 

 

再在BaseActivity中新建一个imageLoader 和设置参数

protected MyImageLoader imageLoader = MyImageLoader.getInstance();

 

/**
     * 如果你经常出现oom
     * 禁用在内存中缓存cacheInMemory(false),
     * 在显示选项中使用 .bitmapConfig(Bitmap.Config.RGB_565) . RGB_565模式消耗的内存比ARGB_8888模式少两倍.
     * 配置中使用 .memoryCache(newWeakMemoryCache()) 或者完全禁用在内存中缓存(don't call .cacheInMemory()).
     * 在显示选项中使用.imageScaleType(ImageScaleType.EXACTLY) 或 .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
     * 一定要对ImageLoaderConfiguration进行初始化,否则会报错
     * 开启缓存后默认会缓存到外置SD卡如下地址(/sdcard/Android/data/[package_name]/cache).如果外置SD卡不存在,会缓存到手机. 缓存到Sd卡需要在AndroidManifest.xml文件中进行配置WRITE_EXTERNAL_STORAGE
     */
    protected DisplayImageOptions options = new DisplayImageOptions.Builder()
    //.resetViewBeforeLoading()
    //.showImageOnLoading(R.drawable.ic_stub) //加载中显示的图片 
    //.showImageOnFail(R.drawable.ic_error)  //加载失败显示的图片
    //.cacheInMemory()
    .cacheOnDisc()    // 设置下载的图片是否缓存在SD卡中   //缓存在/mnt/shell/emulated/0/Android/data/com.sdmc.hotel.ollauncher/cache/uil-images
    .imageScaleType(ImageScaleType.IN_SAMPLE_INT)//设置图片以如何的编码方式显示(图像将被二次采样的整数倍)
    .bitmapConfig(Bitmap.Config.RGB_565)   //16 R占5位 G占6位 B占5位 没有透明度(A)
    //.displayer(new FadeInBitmapDisplayer(500))//设置图片渐显的时间
    .build();

 

 

在要使用的activity中使用方式

imageLoader.displayImage(this, path, (ImageView) mImageSwitcher.getCurrentView(), 
                        options, mImageLoadingListener);
imageLoader.loadImage(this, path, 
                        options, mImageLoadingListener);

还要加一个监听器

    /**
     * 把imageUri加载成bitmap之后就会触动监听,
     * 然后把加载出来的bitmap加入到SparseArray<SoftReference<Bitmap>>
     * before imageUri=file:///data/user/0/com.sdmc.hotel.ollauncher/files/cloud/public/upload/picture/1441178486.jpg
     * after path=/public/upload/picture/1441178486.jpg;
     * hashCode=-1524414597
     */
    private SimpleImageLoadingListener mImageLoadingListener = new SimpleImageLoadingListener() {
        
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            String path = imageUri.substring(imageUri.indexOf(Configs.getHotelProj(mContext)) 
                    + Configs.getHotelProj(mContext).length());
            log("before imageUri="+imageUri+"; getHotelProj"+Configs.getHotelProj(mContext));
            log("after pat
首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇回收ImageView占用的图像内存 下一篇12306火车票订票项目源码

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目