/** * Generates key for memory cache for incoming image (URI + size).<br> * Pattern for cache key - <b>[imageUri]_[width]x[height]</b>. */ public static String generateKey(String imageUri, ImageSize targetSize) { return new StringBuilder(imageUri) .append(URI_AND_SIZE_SEPARATOR) .append(targetSize.getWidth()) .append(WIDTH_AND_HEIGHT_SEPARATOR) .append(targetSize.getHeight()) .toString(); }
@Override public int compareTo(ImageSize other) { long thisImageSize = this.getWidth() * this.getHeight(); long otherImageSize = other.getWidth() * other.getHeight(); if (thisImageSize < otherImageSize) { return -1; } else if (thisImageSize == otherImageSize) { if (this.getWidth() < other.getWidth()) { return -1; } else if (this.getWidth() == other.getWidth()) { return 0; } else { return 1; } } else { return 1; } }
/** * Adds load image task to execution pool. Image will be returned with {@link * ImageLoadingListener#onLoadingComplete(String, android.view.View, android.graphics.Bitmap)} * callback}.<br> * <b>NOTE:</b> {@link #init(ImageLoaderConfiguration)} method must be called before this method * call * * @param uri Image URI (i.e. "http://site.com/image.png", "file:///mnt/sdcard/image.png") * @param targetImageSize Minimal size for {@link Bitmap} which will be returned in {@linkplain * ImageLoadingListener#onLoadingComplete(String, android.view.View, android.graphics.Bitmap)} * callback}. Downloaded image will be decoded and scaled to {@link Bitmap} of the size which * is <b>equal or larger</b> (usually a bit larger) than incoming minImageSize . * @param options {@linkplain DisplayImageOptions Display image options} for image displaying. If * <b>null</b> - default display image options {@linkplain * ImageLoaderConfiguration.Builder#defaultDisplayImageOptions(DisplayImageOptions) from * configuration} will be used.<br> * Incoming options should contain {@link FakeBitmapDisplayer} as displayer. * @param listener {@linkplain ImageLoadingListener Listener} for image loading process. Listener * fires events on UI thread. * @throws IllegalStateException if {@link #init(ImageLoaderConfiguration)} method wasn't called * before */ public void loadImage( String uri, ImageSize targetImageSize, DisplayImageOptions options, ImageLoadingListener listener) { checkConfiguration(); if (targetImageSize == null) { targetImageSize = new ImageSize( configuration.maxImageWidthForMemoryCache, configuration.maxImageHeightForMemoryCache); } if (options == null) { options = configuration.defaultDisplayImageOptions; } DisplayImageOptions optionsWithFakeDisplayer; if (options.getDisplayer() instanceof FakeBitmapDisplayer) { optionsWithFakeDisplayer = options; } else { optionsWithFakeDisplayer = new DisplayImageOptions.Builder() .cloneFrom(options) .displayer(fakeBitmapDisplayer) .build(); } ImageView fakeImage = new ImageView(configuration.context); fakeImage.setLayoutParams( new LayoutParams(targetImageSize.getWidth(), targetImageSize.getHeight())); fakeImage.setScaleType(ScaleType.CENTER_CROP); displayImage(uri, fakeImage, optionsWithFakeDisplayer, listener); }