/**
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.volley.toolbox;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import java.util.HashMap;
import java.util.LinkedList;
/**
* Helper that handles loading and caching images from remote URLs.
*
* The simple way to use this class is to call {@link ImageLoader#get(String, ImageListener)}
* and to pass in the default image listener provided by
* {@link ImageLoader#getImageListener(ImageView, int, int)}. Note that all function calls to
* this class must be made from the main thead, and all responses will be delivered to the main
* thread as well.
*/
public class ImageLoader {
/** RequestQueue for dispatching ImageRequests onto. */
private final RequestQueue mRequestQueue;
/** Amount of time to wait after first response arrives before delivering all responses. */
private int mBatchResponseDelayMs = 100;
/** The cache implementation to be used as an L1 cache before calling into volley. */
private final ImageCache mCache;
/**
* HashMap of Cache keys -> BatchedImageRequest used to track in-flight requests so
* that we can coalesce multiple requests to the same URL into a single network request.
*/
private final HashMap<String, BatchedImageRequest> mInFlightRequests =
new HashMap<String, BatchedImageRequest>();
/** HashMap of the currently pending responses (waiting to be delivered). */
private final HashMap<String, BatchedImageRequest> mBatchedResponses =
new HashMap<String, BatchedImageRequest>();
/** Handler to the main thread. */
private final Handler mHandler = new Handler(Looper.getMainLooper());
/** Runnable for in-flight response delivery. */
private Runnable mRunnable;
/**
* Simple cache adapter interface. If provided to the ImageLoader, it
* will be used as an L1 cache before dispatch to Volley. Implementations
* must not block. Implementation with an LruCache is recommended.
*/
public interface ImageCache {
public Bitmap getBitmap(String url);
public void putBitmap(String url, Bitmap bitmap);
}
/**
* Constructs a new ImageLoader.
* @param queue The RequestQueue to use for making image requests.
* @param imageCache The cache to use as an L1 cache.
*/
public ImageLoader(RequestQueue queue, ImageCache imageCache) {
mRequestQueue = queue;
mCache = imageCache;
}
/**
* The default implementation of ImageListener which handles basic functionality
* of showing a default image until the network response is received, at which point
* it will switch to either the actual image or the error image.
* @param view The imageView that the listener is associated with.
* @param defaultImageResId Default image resource ID to use, or 0 if it doesn't exist.
* @param errorImageResId Error image resource ID to use, or 0 if it doesn't exist.
*/
public static ImageListener getImageListener(final ImageView view,
final int defaultImageResId, final int errorImageResId) {
return new ImageListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (errorImageResId != 0) {
view.setImageResource(errorImageResId);
}
}
@Override
public void onResponse(ImageContainer response, boolean isImmediate) {
if (response.getBitmap() != null) {
view.setImageBitmap(response.getBitmap());
} else if (defaultImageResId != 0) {
view.setImageResource(defaultImageResId);
}
}
};
}
/**
* Interface for the response handlers on image requests.
*
* The call flow is this:
* 1. Upon being attached to a request, onResponse(response, true) will
* be invoked to reflect any cached data that was already available. If the
* data was available, response.getBitmap() will be non-null.
*
* 2. After a network response returns, only one of the following cases will happen:
* - onResponse(response, false) will be called if the image was loaded.
* or
* - onErrorResponse will be called if there was an error loading the image.
*/
public interface ImageListener extends ErrorListener {
/**
* Listens for non-error changes to the loading of the image request.
*
* @param response Holds all information pertaining to the request, as well
* as the bitmap (if it is loaded).
* @param isImmediate True if this was called during ImageLoader.get() variants.
* This can be used to differentiate between a cached image loading and a network
* image loading in order to, for example, run an animation to fade in network loaded
* images.
*/
public void onResponse(ImageContainer response, boolean isImmediate);
}
/**
* Checks if the item is available in the cache.
* @param requestUrl The url of the remote image
* @param maxWidth The maximum width of the returned image.
* @param maxHeight The maximum height of the returned image.
* @return True if the item exists in cache, false otherwise.
*/
public boolean isCached(String requestUrl, int maxWidth, int maxHeight) {
return isCached(requestUrl, maxWidth, maxHeight, ScaleType.CENTER_INSIDE);
}
/**
* Checks if the item is available in the cache.
*
* @param requestUrl The url of the remote image
* @param maxWidth The maximum width of the returned image.
* @param maxHeight The maximum height of the returned image.
* @param scaleType The scaleType of the imageView.
* @return True if the item exists in cache, false otherwise.
*/
public boolean isCached(String requestUrl, int maxWidth, int maxHeight, ScaleType scaleType) {
throwIfNotOnMainThread();
String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight, scaleType);
return mCache.getBitmap(cacheKey) != null;
}
/**
* Returns an ImageContainer for the requested URL.
*
* The ImageContainer will contain either the specified default bitmap or the loaded bitmap.
* If the default was returned, the {@link ImageLoader} will be invoked when the
* request is fulfilled.
*
* @param requestUrl The URL of the image to be loaded.
*/
public ImageContainer get(String requestUrl, final ImageListener listener) {
return get(requestUrl, listener, 0, 0);
}
/**
* Equivalent to calling {@link #get(String, ImageListener, int, int, ScaleType)} with
* {@code Scaletype == ScaleType.CENTER_INSIDE}.
*/
public ImageContainer get(String requestUrl, ImageListener imageListener,
int maxWidth, int maxHeight) {
return get(requestUrl, imageListener, maxWidth, maxHeight, ScaleType.CENTER_INSIDE);
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
volley网络请求源码,添加gzip数据格式 (133个子文件)
jarlist.cache 120B
Request.class 13KB
DiskBasedCache.class 12KB
BasicNetwork.class 10KB
ImageLoader.class 8KB
RequestQueue.class 8KB
HurlStack.class 8KB
ImageRequest.class 7KB
GzipJsonObjectRequest.class 6KB
HttpClientStack.class 6KB
ExtendedRequest.class 6KB
JsonArrayRequest.class 5KB
NetworkDispatcher.class 5KB
JsonObjectRequest.class 4KB
NetworkImageView.class 4KB
CacheDispatcher.class 4KB
HttpHeaderParser.class 4KB
RequestFuture.class 4KB
JsonRequest.class 3KB
AndroidAuthenticator.class 3KB
VolleyLog.class 3KB
Volley.class 3KB
DiskBasedCache$CacheHeader.class 3KB
StringRequest.class 3KB
VolleyLog$MarkerLog.class 3KB
ImageLoader$BatchedImageRequest.class 3KB
ImageLoader$4.class 2KB
ExecutorDelivery.class 2KB
ImageLoader$ImageContainer.class 2KB
GzipStringRequest.class 2KB
ClearCacheRequest.class 2KB
Response.class 2KB
NetworkImageView$1.class 2KB
ByteArrayPool.class 2KB
PoolingByteArrayOutputStream.class 2KB
ExecutorDelivery$ResponseDeliveryRunnable.class 2KB
ImageLoader$1.class 2KB
InternalUtils.class 2KB
DefaultRetryPolicy.class 2KB
NetworkResponse.class 1KB
ImageLoader$2.class 1KB
AuthFailureError.class 1KB
ImageLoader$3.class 1KB
DiskBasedCache$CountingInputStream.class 1KB
Request$Priority.class 1KB
VolleyError.class 1KB
NoCache.class 1KB
RequestQueue$1.class 1KB
NetworkImageView$1$1.class 1KB
Request$1.class 1KB
CacheDispatcher$1.class 997B
Cache$Entry.class 996B
HttpClientStack$HttpPatch.class 987B
ExecutorDelivery$1.class 898B
ByteArrayPool$1.class 759B
ResponseDelivery.class 658B
VolleyLog$MarkerLog$Marker.class 652B
NetworkError.class 647B
RedirectError.class 643B
ParseError.class 637B
ImageLoader$ImageListener.class 592B
ServerError.class 499B
Request$Method.class 488B
NoConnectionError.class 479B
HttpStack.class 459B
Cache.class 454B
RequestQueue$RequestFinishedListener.class 414B
ImageLoader$ImageCache.class 366B
Network.class 359B
BuildConfig.class 345B
RequestQueue$RequestFilter.class 329B
Response$Listener.class 325B
TimeoutError.class 313B
RetryPolicy.class 300B
Authenticator.class 298B
HurlStack$UrlRewriter.class 287B
Response$ErrorListener.class 274B
.classpath 475B
tsb_netword_volley.jar 98KB
netword_volley.jar 98KB
main.jar 92KB
ImageLoader.java 20KB
Request.java 20KB
DiskBasedCache.java 18KB
BasicNetwork.java 12KB
RequestQueue.java 11KB
HurlStack.java 10KB
ImageRequest.java 10KB
NetworkImageView.java 8KB
HttpClientStack.java 7KB
VolleyLog.java 6KB
NetworkDispatcher.java 6KB
CacheDispatcher.java 6KB
HttpHeaderParser.java 6KB
GzipJsonObjectRequest.java 6KB
JsonArrayRequest.java 6KB
ByteArrayPool.java 5KB
ExtendedRequest.java 5KB
JsonObjectRequest.java 4KB
Volley.java 4KB
共 133 条
- 1
- 2
资源评论
- issaclam2017-03-17谢谢分享!~
_SmileZ
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功