Android - Volley的ImageLoader下载图片
来源:互联网 发布:天梭手表怎么样 知乎 编辑:程序博客网 时间:2024/04/27 15:43
使用Volley的ImageLoader下载图片时, onResponse会调用两次, 第一次Bitmap是空, 设置默认图片; 第二次是下载的网络图片.
源码中:
// Update the caller to let them know that they should use the default bitmap.imageListener.onResponse(imageContainer, true);
使用时, 在加载图片之前, 需要判断Bitmap是否为空, 再加载.
代码
/** * 从服务器下载图片 * onResponse会调用两次, 第一次Bitmap是null, 设置默认图片; 第二次是网络图片. * * @param listener 接收监听 */ protected void parseImage(final ImageListener<Bitmap> listener) { WebScheduler.getInstance().getImageLoader().get(NetworkHelper.getRealUrl(mImgUrl), new ImageLoader.ImageListener() { @Override public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) { mImgBitmap = imageContainer.getBitmap(); if (mImgBitmap != null) listener.onResponse(mImgBitmap); } @Override public void onErrorResponse(VolleyError volleyError) { ToastHelper.getInstance().showToast(R.string.network_error); } }, 0, 0 ); }
源码
/** * Issues a bitmap request with the given URL if that image is not available * in the cache, and returns a bitmap container that contains all of the data * relating to the request (as well as the default image if the requested * image is not available). * @param requestUrl The url of the remote image * @param imageListener The listener to call when the remote image is loaded * @param maxWidth The maximum width of the returned image. * @param maxHeight The maximum height of the returned image. * @param scaleType The ImageViews ScaleType used to calculate the needed image size. * @return A container object that contains all of the properties of the request, as well as * the currently available image (default if remote is not loaded). */ public ImageContainer get(String requestUrl, ImageListener imageListener, int maxWidth, int maxHeight, ScaleType scaleType) { // only fulfill requests that were initiated from the main thread. throwIfNotOnMainThread(); final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight, scaleType); // Try to look up the request in the cache of remote images. Bitmap cachedBitmap = mCache.getBitmap(cacheKey); if (cachedBitmap != null) { // Return the cached bitmap. ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null); imageListener.onResponse(container, true); return container; } // The bitmap did not exist in the cache, fetch it! ImageContainer imageContainer = new ImageContainer(null, requestUrl, cacheKey, imageListener); // Update the caller to let them know that they should use the default bitmap. imageListener.onResponse(imageContainer, true); // Check to see if a request is already in-flight. BatchedImageRequest request = mInFlightRequests.get(cacheKey); if (request != null) { // If it is, add this request to the list of listeners. request.addContainer(imageContainer); return imageContainer; } // The request is not already in flight. Send the new request to the network and // track it. Request<Bitmap> newRequest = makeImageRequest(requestUrl, maxWidth, maxHeight, scaleType, cacheKey); mRequestQueue.add(newRequest); mInFlightRequests.put(cacheKey, new BatchedImageRequest(newRequest, imageContainer)); return imageContainer; }
2 0
- Android - Volley的ImageLoader下载图片
- Android图片加载框架:ImageLoader,Volley,Picasso,Glide,Fresco
- android-----Volley框架使用ImageLoader加载图片源码分析
- Android图片加载框架:ImageLoader,Volley,Picasso,Glide,Fresco
- android ImageLoader 缓存下载显示网络图片
- Volley的ImageLoader用法
- 使用Volley框架的ImageLoader加载网络图片
- 使用Volley框架的ImageLoader加载网络图片
- 使用volley的imageloader加载网络图片实例
- 使用Volley框架的ImageLoader加载网络图片
- Volley的ImageLoader加载图片错位处理方式
- volley中ImageLoader的二次封装(图片缓存)
- 使用Volley的ImageLoader优化listview加载图片
- 使用Volley框架的ImageLoader加载网络图片
- ImageLoader下载图片
- Volley框架ImageLoader的使用
- Android中ImageLoader的使用,下载图片绑定到listview上
- Volley(二) ImageRequest、Imageloader、NetworkImageView加载图片
- UIKit 视图 UIView
- Mat类
- 10003---SpringMVC 使用@RequestMapping映射请求
- android 跳转
- 基于有序数组的二分查找
- Android - Volley的ImageLoader下载图片
- 消息队列再整理
- ibooks打开含iframe的epub乱码
- android 即时通讯开发总结
- 241-Different Ways to Add Parentheses
- Android 旋转动画
- 消息队列再整理二
- Mybatis学习笔记-mybatis对时间的处理
- poj2431 Expedition (优先队列) 挑战程序设计竞赛