DevUtils Github
- ImageLoader 工具类 -> ImageLoaderUtils.java
方法 |
注释 |
init |
初始化 ImageLoader 加载配置 |
defaultOptions |
获取 DisplayImageOptions 图片加载配置 |
getDefaultImageOptions |
获取图片默认加载配置 |
getNoCacheImageOptions |
获取不使用缓存的图片加载配置 |
getCacheImageOptions |
获取 ImageLoader 图片缓存加载配置 |
cloneImageOptions |
克隆图片加载配置 |
getFadeInBitmapDisplayer |
获取图片渐变动画加载配置 |
getRoundedBitmapDisplayer |
获取圆角图片加载配置 |
getBitmapDisplayerOptions |
获取图片效果加载配置 |
displayImage |
图片显示 |
loadImage |
图片加载 |
loadImageSync |
图片同步加载 |
clearDiskCache |
清除磁盘缓存 |
clearMemoryCache |
清除内存缓存 |
getDiskCache |
获取 SDCard 缓存空间 |
getMemoryCache |
获取 Memory 缓存空间 |
handleSlowNetwork |
设置是否处理弱网情况 |
denyNetworkDownloads |
设置是否禁止网络下载 |
cancelDisplayTask |
取消图片显示任务 |
getLoadingUriForView |
通过 ImageView 获取图片加载地址 |
setDefaultLoadingListener |
设置全局加载监听事件 |
destroy |
销毁操作 |
pause |
暂停图片加载 |
resume |
恢复图片加载 |
stop |
停止图片加载 |
package dev.other;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.DrawableRes;
import android.widget.ImageView;
import com.nostra13.universalimageloader.cache.disc.DiskCache;
import com.nostra13.universalimageloader.cache.memory.MemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
public final class ImageLoaderUtils {
private ImageLoaderUtils() {
}
private static int sImageLoadingRes = 0;
private static int sImageUriErrorRes = 0;
private static int sImageFailRes = 0;
private static final DisplayImageOptions DF_OPTIONS = defaultOptions();
public static void init(final Context context) {
DisplayImageOptions options = DF_OPTIONS;
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.defaultDisplayImageOptions(options)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.memoryCacheSize(2 * 1024 * 1024)
.memoryCacheSizePercentage(13)
.imageDownloader(new BaseImageDownloader(context))
.tasksProcessingOrder(QueueProcessingType.LIFO).build();
ImageLoader.getInstance().init(config);
}
public static DisplayImageOptions defaultOptions() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(sImageLoadingRes)
.showImageForEmptyUri(sImageUriErrorRes)
.showImageOnFail(sImageFailRes)
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.cacheInMemory(true)
.considerExifParams(false)
.resetViewBeforeLoading(false)
.cacheOnDisk(true).build();
return options;
}
public static DisplayImageOptions getDefaultImageOptions(@DrawableRes final int loadingRes) {
DisplayImageOptions.Builder optionsBuilder = cloneImageOptions(DF_OPTIONS);
optionsBuilder.showImageOnLoading(loadingRes)
.showImageForEmptyUri(loadingRes)
.showImageOnFail(loadingRes);
return optionsBuilder.build();
}
public static DisplayImageOptions getNoCacheImageOptions() {
return getCacheImageOptions(false, false, -1);
}
public static DisplayImageOptions getNoCacheImageOptions(@DrawableRes final int loadingRes) {
return getCacheImageOptions(false, false, loadingRes);
}
public static DisplayImageOptions getCacheImageOptions(final boolean isCache, final boolean isCacheDisk) {
return getCacheImageOptions(isCache, isCacheDisk, -1);
}
public static DisplayImageOptions getCacheImageOptions(final boolean isCache,
final boolean isCacheDisk,
@DrawableRes final int loadingRes) {
DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder()
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.cacheInMemory(isCache)
.displayer(new RoundedBitmapDisplayer(20))
.cacheOnDisk(isCacheDisk);
if (loadingRes != 0) {
builder.showImageOnLoading(loadingRes)
.showImageForEmptyUri(loadingRes)
.showImageOnFail(loadingRes);
}
return builder.build();
}
public static DisplayImageOptions.Builder cloneImageOptions(final DisplayImageOptions options) {
return (options != null) ? new DisplayImageOptions.Builder().cloneFrom(options) : null;
}
public static DisplayImageOptions getFadeInBitmapDisplayer(final int durationMillis) {
return getBitmapDisplayerOptions(cloneImageOptions(DF_OPTIONS).build(), new FadeInBitmapDisplayer(durationMillis));
}
public static DisplayImageOptions getFadeInBitmapDisplayer(final DisplayImageOptions options, final int durationMillis) {
if (options != null) {
return getBitmapDisplayerOptions(options, new FadeInBitmapDisplayer(durationMillis));
}
return options;
}
public static DisplayImageOptions getRoundedBitmapDisplayer(final int cornerRadiusPixels) {
return getBitmapDisplayerOptions(cloneImageOptions(DF_OPTIONS).build(), new RoundedBitmapDisplayer(cornerRadiusPixels));
}
public static DisplayImageOptions getRoundedBitmapDisplayer(final DisplayImageOptions options, final int cornerRadiusPixels) {
if (options != null) {
return getBitmapDisplayerOptions(options, new RoundedBitmapDisplayer(cornerRadiusPixels));
}
return options;
}
public static DisplayImageOptions getBitmapDisplayerOptions(final DisplayImageOptions options, final BitmapDisplayer displayer) {
if (options != null && displayer != null) {
return cloneImageOptions(options).displayer(displayer).build();
}
return null;
}
public static void displayImage(final String uri, final ImageView imageView) {
if (imageView != null) ImageLoader.getInstance().displayImage(uri, imageView);
}
public static void displayImage(final String uri, final ImageView imageView, final DisplayImageOptions options) {
if (imageView != null) {
if (options != null) {
ImageLoader.getInstance().displayImage(uri, imageView, options);
} else {
ImageLoader.getInstance().displayImage(uri, imageView);
}
}
}
public static void displayImage(final String uri, final ImageView imageView, final ImageSize imageSize) {
if (imageView != null && imageSize != null) {
ImageLoader.getInstance().displayImage(uri, imageView, imageSize);
}
}
public static void displayImage(final String uri, final ImageAware imageAware, final DisplayImageOptions options) {
if (imageAware != null) {
if (options != null) {
ImageLoader.getInstance().displayImage(uri, imageAware, options);
} else {
ImageLoader.getInstance().displayImage(uri, imageAware);
}
}
}
public static void displayImage(final String uri, final ImageView imageView, final ImageLoadingListener listener) {
displayImage(uri, imageView, null, listener);
}
public static void displayImage(final String uri, final ImageView imageView,
final DisplayImageOptions options, final ImageLoadingListener listener) {
if (imageView != null) {
if (options != null) {
ImageLoader.getInstance().displayImage(uri, imageView, options, listener);
} else {
ImageLoader.getInstance().displayImage(uri, imageView, listener);
}
}
}
public static void displayImage(final String uri, final ImageView imageView,
final ImageLoadingListener listener,
final ImageLoadingProgressListener progressListener) {
displayImage(uri, imageView, null, listener, progressListener);
}
public static void displayImage(final String uri, final ImageView imageView,
final DisplayImageOptions options, final ImageLoadingListener listener,
final ImageLoadingProgressListener progressListener) {
if (imageView != null) {
ImageLoader.getInstance().displayImage(uri, imageView, options, listener, progressListener);
}
}
public static void loadImage(final String uri, final ImageLoadingListener listener) {
loadImage(uri, null, null, listener, null);
}
public static void loadImage(final String uri, final ImageSize imageSize, final ImageLoadingListener listener) {
loadImage(uri, imageSize, null, listener, null);
}
public static void loadImage(final String uri, final ImageSize imageSize,
final ImageLoadingListener listener,
final ImageLoadingProgressListener progressListener) {
loadImage(uri, imageSize, null, listener, progressListener);
}
public static void loadImage(final String uri, final ImageLoadingListener listener,
final ImageLoadingProgressListener progressListener) {
loadImage(uri, null, null, listener, progressListener);
}
public static void loadImage(final String uri, final DisplayImageOptions options,
final ImageLoadingListener listener, final ImageLoadingProgressListener progressListener) {
loadImage(uri, null, options, listener, progressListener);
}
public static void loadImage(final String uri, final ImageSize imageSize, final DisplayImageOptions options,
final ImageLoadingListener listener, final ImageLoadingProgressListener progressListener) {
ImageLoader.getInstance().loadImage(uri, imageSize, options, listener, progressListener);
}
public static Bitmap loadImageSync(final String uri) {
return loadImageSync(uri, null, null);
}
public static Bitmap loadImageSync(final String uri, final ImageSize imageSize) {
return loadImageSync(uri, imageSize, null);
}
public static Bitmap loadImageSync(final String uri, final DisplayImageOptions options) {
return loadImageSync(uri, null, options);
}
public static Bitmap loadImageSync(final String uri, final ImageSize imageSize, final DisplayImageOptions options) {
return ImageLoader.getInstance().loadImageSync(uri, imageSize, options);
}
public static void clearDiskCache() {
ImageLoader.getInstance().clearDiskCache();
}
public static void clearMemoryCache() {
ImageLoader.getInstance().clearMemoryCache();
}
public static DiskCache getDiskCache() {
return ImageLoader.getInstance().getDiskCache();
}
public static MemoryCache getMemoryCache() {
return ImageLoader.getInstance().getMemoryCache();
}
public static void handleSlowNetwork(final boolean handleSlowNetwork) {
ImageLoader.getInstance().handleSlowNetwork(handleSlowNetwork);
}
public static void denyNetworkDownloads(final boolean denyNetworkDownloads) {
ImageLoader.getInstance().denyNetworkDownloads(denyNetworkDownloads);
}
public static void cancelDisplayTask(final ImageView imageView) {
if (imageView != null) {
ImageLoader.getInstance().cancelDisplayTask(imageView);
}
}
public static String getLoadingUriForView(final ImageView imageView) {
if (imageView != null) {
return ImageLoader.getInstance().getLoadingUriForView(imageView);
}
return null;
}
public static void setDefaultLoadingListener(final ImageLoadingListener listener) {
ImageLoader.getInstance().setDefaultLoadingListener(listener);
}
public static void destroy() {
ImageLoader.getInstance().destroy();
}
public static void pause() {
ImageLoader.getInstance().pause();
}
public static void resume() {
ImageLoader.getInstance().resume();
}
public static void stop() {
ImageLoader.getInstance().stop();
}
}