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 框架练成 教你打造高效的图片加载框架
- JQ获取动态加载的图片大小的正确方法分享
- ListView异步加载图片,完美实现图文混排
- 【Cocos2d-x】新手自学(七)百分比读取进度加载图片资源到缓存(2.0.1版本)
- Android 中解决图片加载OOM 之 bitmap 压缩显示,压缩上传
- 加载网络图片
- 实现一个图片懒加载插件有多难? – 前端有多难? – SegmentFault
- 利用CSS、JavaScript及Ajax实现图片预加载的三大方法
- 图片加载框架Glide
- AsyncTask实现网络图片的异步加载
- 优化 Android ListView 异步加载图片
- 利用LruCache加载网络图片实现图片瀑布流效果(基础版)
- 小程序之『图片懒加载』终极解决方案
- uwp 如何加载本地图片更新image控件
- Google推荐的图片加载库Glide介绍
- Android网络图片加载内存溢出(OOM)解决方案
- ionic 图标以及启动页图片不能正确加载
- Bitmap_多级缓存实现图片加载
- UITables With Downloaded Images - Easy Asynchronous Code <UITable 异步加载图片>
- JavaScript实现图片预加载