网友通过本文主要向大家介绍了imageloader,android imageloader,imageloader使用,universalimageloader,imageloader缓存等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
ImageLoader配合ImageSwitcher的使用,imageloader使用
先在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 path="+path+";hashCode="+path.hashCode()); mLoadedImages.append(path.hashCode(), new SoftReference<Bitmap>(loadedImage)); } };
以及一个软引用的SparseArray
//SparseArray替代HashMap,SoftReference软连接 private SparseArray<SoftReference<Bitmap>> mLoadedImages = new SparseArray<SoftReference<Bitmap>>();
还有就是MyImageLoader 的类
/** * Copyright(c) 2003-2013 Shenzhen SDMC Technology Co.,LTD * All Rights Reserved. * * Filename : MyImageLoader.java * Author : wuxiaodi * Creation time : 下午2:01:58 - 2013-7-17 * Description : */ package com.sdmc.hotel.util; import java.io.File; import android.content.Context; import android.widget.ImageView; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.assist.ImageLoadingListener; import com.nostra13.universalimageloader.core.assist.ImageSize; /** * Just to cache images in our archive, so to use it when off-line. */ public class MyImageLoader extends ImageLoader { private static MyImageLoader instance; public static MyImageLoader getInstance() { if (instance == null) { synchronized (MyImageLoader.class) { if (instance == null) { instance = new MyImageLoader(); } } } return instance; } protected MyImageLoader() { } public void displayImage(Context context, String path, ImageView imageView) { String furi = getLocalImage(context, path); if (furi == null) { super.displayImage(Configs.getServerAddress(context) + path, imageView); } else { super.displayImage(furi, imageView); } } public void displayImage(Context contex