您的位置:首页 > 理论基础 > 计算机网络

Android ImageLoader网络加载图片

2016-08-05 16:34 162 查看
public class XImageLoader {

static {
long maxMamory = Runtime.getRuntime().maxMemory();// 应用程序程序运行时所使用最大内存数
maxMamory = (long) (maxMamory * 0.7);
int v = Runtime.getRuntime().availableProcessors();
int availableProcessors = v < 4 ?3 : v-1;

// System.out.println("maxMamory : " + maxMamory + "  availableProcessors: " + availableProcessors);

// DebugToast.show("maxMamory : " + (maxMamory/1024.0f/1024 )+ "  availableProcessors: " + availableProcessors, 1);

DisplayImageOptions options = new DisplayImageOptions.Builder()
// 设置图片下载期间显示的图片
.showImageOnLoading(R.drawable.homepage_off).showImageForEmptyUri(R.drawable.homepage_off)// 设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(R.drawable.homepage_off) // 设置图片加载或解码过程中发生错误显示的图片
.cacheInMemory(true)// 设置下载的图片是否缓存在内存中
.cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
.bitmapConfig(Config.RGB_565)
// .displayer(new RoundedBitmapDisplayer(20))// 设置成圆角图片
.build(); // 创建配置过得DisplayImageOption对象

// File cacheDir = StorageUtils.getOwnCacheDirectory(App.ctx,
// "sunsun/Cache");
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(App.ctx)
.memoryCacheExtraOptions(480, 800) // max width, max
// height,即保存的每个缓存文件的最大长宽
// .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75,
// null) // Can slow ImageLoader, use it carefully (Better don't
// use it)/设置缓存的详细信息,最好不要设置这个
.threadPoolSize(availableProcessors)// 线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY-2).denyCacheImageMultipleSizesInMemory()
// .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 *
// 1024)) // You can pass your own memory cache
// implementation/你可以通过自己的内存缓存实现
// .memoryCache(new WeakMemoryCache())

.memoryCacheSize((int) maxMamory)
// .memoryCacheSize(15 * 1024 * 1024)

.diskCacheSize(80 * 1024 * 1024).diskCacheFileNameGenerator(new Md5FileNameGenerator())// 将保存的时候的URI名称用MD5
// 加密

.tasksProcessingOrder(QueueProcessingType.LIFO) // 后进先出
// .diskCacheFileCount(100) //缓存的文件数量
// .diskCache(new UnlimitedDiscCache(cacheDir))//自定义缓存路径
// .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
.defaultDisplayImageOptions(options)
// .imageDownloader(new BaseImageDownloader(App.ctx, 5 * 1000,
// 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
.imageDownloader(new DownloadImageFromNetWork(App.ctx, 5 * 1000, 30 * 1000)).writeDebugLogs() // Remove
// for
// release
// app
.build();// 开始构建
// Initialize ImageLoader with configuration.

com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config);// 全局初始化此配置

}

private static class DownloadImageFromNetWork extends BaseImageDownloader {

public DownloadImageFromNetWork(Context context, int connectTimeout, int readTimeout) {
super(context, connectTimeout, readTimeout);
}

@Override
protected InputStream getStreamFromNetwork(String arg0, Object arg1) throws IOException {

try {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(connectTimeout, TimeUnit.MILLISECONDS);
client.setReadTimeout(readTimeout, TimeUnit.MILLISECONDS);
Request request = new Request.Builder().url(arg0).build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) {
return null;
} else {
return response.body().byteStream();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

}

/**
* 加载网络图片
* @param uri
* @param imageView
*/
public static void load(String uri, ImageView imageView) {

ImageLoader.getInstance().displayImage(uri, imageView);

DebugLog.v("downloadImg", "" + uri);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android