android開發使用ImageLoad插件

直接上代碼緩存

public class ImageLoad {
	private Context context;
	private ImageLoaderConfiguration config;
	private ImageLoader loader = ImageLoader.getInstance();
	private DisplayImageOptions options;
	public ImageLoad(Context context) {
		this.context = context;
		initImageLoader(this.context, false);
	}
	
	public ImageLoad(Context context, boolean isAvater) {
		this.context = context;
		initImageLoader(this.context, isAvater);
	}
	/**
	 * 加載原圖
	 * 
	 * @param iv
	 * @param url
	 */
	public void loadImage(final ImageView iv, final String url) {
		try {
			// iv.setTag(url);
			loader.displayImage(url, iv, options);
			// loader.loadImage(url, options, new SimpleImageLoadingListener() {
			// 
                        // 	@Override
			// 	public void onLoadingComplete(String imageUri, View view,
			// 			Bitmap loadedImage) {
			// 		super.onLoadingComplete(imageUri, view, loadedImage);
			// 		if (url.equals(iv.getTag())) {
			// 			if (iv != null) {
			// 				iv.setImageBitmap(loadedImage);
			// 			}
			// 		}
			// 	}
			// });
		} catch (Exception e) {
			LogUtil.e(getClass(), "loadImage()", e);
		}
	}
	/**
	 * 加載指定大小圖片
	 * 
	 * @param iv
	 * @param url
	 * @param width
	 * @param height
	 */
	public void loadImage(final ImageView iv, final String url, int width,
			int height) {
		try {
                      // iv.setTag(url);
			// ImageSize imageSize = new ImageSize(width, height);
			// loader.displayImage(url, iv, options);
			// loader.loadImage(url, imageSize, options,
			// new SimpleImageLoadingListener() {
			//
			// @Override
			// public void onLoadingComplete(String imageUri,
			// View view, Bitmap loadedImage) {
			// super.onLoadingComplete(imageUri, view, loadedImage);
			// if (url.equals(iv.getTag())) {
			// iv.setImageBitmap(loadedImage);
			// }
			// }
			// });
			loader.displayImage(url, iv, options);
		} catch (Exception e) {
			LogUtil.e(
					getClass(),
					"loadImage(final ImageView iv, final String url, int width,int height)",
					e);
		}
	}
        public void clearCache() {
		try {
			if (loader != null) {
				// loader.clearDiskCache();
				loader.clearMemoryCache();
			}
		} catch (Exception e) {
			LogUtil.e(getClass(), "clearCache()", e);
		}
	}
	/**
	 * 初始化ImageLoader數據
	 */
	private void initImageLoader(Context context, boolean isAvater) {
		try {
                      options = new DisplayImageOptions.Builder()
					.bitmapConfig(Bitmap.Config.RGB_565)
					.imageScaleType(ImageScaleType.EXACTLY)
					.showImageOnLoading(isAvater ? R.drawable.default_avater : R.drawable.home_page_pic)
					.showImageForEmptyUri(isAvater ? R.drawable.default_avater : R.drawable.home_page_pic)
					.showImageOnFail(isAvater ? R.drawable.default_avater : R.drawable.home_page_pic)
					.cacheInMemory(true).cacheOnDisk(true).build();
			File cacheDir = StorageUtils.getCacheDirectory(context);
			// 設置緩存路徑和緩存文件的數量,超過數量後,old將被刪除
			config = new ImageLoaderConfiguration.Builder(context)
					.threadPoolSize(5)
					// 設定線程池的大小
					.threadPriority(Thread.NORM_PRIORITY - 1)
					.denyCacheImageMultipleSizesInMemory()
					.diskCacheSize(50 * 1024 * 1024).diskCacheFileCount(100)
					.diskCache(new UnlimitedDiskCache(cacheDir))
					.defaultDisplayImageOptions(options)
					.memoryCache(new WeakMemoryCache())
					.memoryCacheSize(2 * 1024 * 1024)
					.diskCacheFileNameGenerator(new Md5FileNameGenerator())
					.tasksProcessingOrder(QueueProcessingType.LIFO).build();
			ImageLoader.getInstance().init(config);
		} catch (Exception e) {
			LogUtil.e(getClass(), "initImageLoader(Context context)", e);
		}
            }
}
相關文章
相關標籤/搜索