设为首页 加入收藏

TOP

universal image loader自己使用的一些感受(一)
2017-10-13 10:35:55 】 浏览:7596
Tags:universal image loader 自己 使用 一些 感受

1、全局入口的Application定义初始化:

ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(getApplicationContext())
        .threadPoolSize(3) //线程池内加载的数量
        .threadPriority(Thread.NORM_PRIORITY - 1) // default  
        .denyCacheImageMultipleSizesInMemory()
                //.memoryCache(new WeakMemoryCache())   //也可以用自己的内存缓存实
        .memoryCache(new LruMemoryCache(50 * 1024 * 1024))   //也可以用自己的内存缓存实现
        .memoryCacheSize(50*1024*1024)
        .diskCacheFileNameGenerator(new Md5FileNameGenerator()) //将保存的时候的URL名称用MD5加密
        .tasksProcessingOrder(QueueProcessingType.FIFO) //先进先出
        .diskCacheSize(200 * 1024 * 1024)
        .defaultDisplayImageOptions(DisplayImageOptions.createSimple())    
        .imageDownloader(new BaseImageDownloader(getApplicationContext()))  // default
        .imageDecoder(new BaseImageDecoder(true)) // default
        //.writeDebugLogs() // Remove for release app
        .build();
        //全局初始化此配置  
        ImageLoader.getInstance().init(configuration);

2、显示设置:

/**
     *用于显示图片的选项,没过渡时间
     * 用于圈子社区,解决列表图片过多时,出现刷新闪烁的情况
     */
    public static DisplayImageOptions OptionsWithCache  = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.icon_no_photo)
            .showImageOnFail(R.drawable.icon_no_photo)
            .showImageForEmptyUri(R.drawable.icon_no_photo)//设置图片Uri为空或是错误的时候显示的图片
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .considerExifParams(true)
            //EXACTLY_STRETCHED:图片会缩放到目标大小完全相同   EXACTLY :图像将完全按比例缩小的目标大小
            //IN_SAMPLE_INT:图像将被二次采样的整数倍
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .resetViewBeforeLoading(false)
            //    .delayBeforeLoading(50)  //载入图片前稍做延时可以提高整体滑动的流畅度
            .displayer(new FadeInBitmapDisplayer(200))  //是否图片加载好后渐入的动画时间
            .build();

3、如果ImageView设置了长宽大小:

建议用display,可以根据ImageView的大小来自动缩放图片,节省内存:

 ImageLoader.getInstance().displayImage(pic_url, imageView,OptionsWithCache); 

对于listView里面,图片可能因为滑动过快,导致错误重复,可以通过设置tag来处理:

public static void setImageWithTag(final String pic_url,final ImageView imageView,Context context) {
        if(pic_url != null) {
            String tag = (String) imageView.getTag();
            if(tag == null) {
                tag = "";
            }
            if(pic_url.equals(imageView.getTag())) {
                return;
            }
        }
        String picUrl = pic_url;
        if(!picUrl.contains("http://")) {
            picUrl = MyConfig.picFirst+picUrl;
        }
        //Log.i("main","loading pic:"+pic_url);
        ImageLoader.getInstance().displayImage(picUrl, imageView, OptionsWithCache);
    }

listView里面的设置:

 SetImageUtils.setImageWithTag(picUrl,holder.iv,context);

holder.iv.setTag(picUrl);

4、一些图片加载过程中的监听:

//加载自定义配置的一个图片的,网络加载监听,等待加载图片完成再初始化缩小放大
        ImageLoader.getInstance().displayImage(picurl,mImageView, SetImageUtils.OptionsWithCache, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                super.onLoadingStarted(imageUri, view);
                Utility.setLoadingProgressbar(null,parentView,true);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                super.onLoadingComplete(imageUri, view, loadedImage);
                Utility.setLoadingProgressbar(null,parentView,false);
            }
        }, new ImageLoadingProgressListener() {
            @Override
            public void onProgressUpdate(String s, View view, int i, int i1)
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Binder中的asInterface解析 下一篇动态注册广播和静态注册广播的区别

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目