package com.example.administrator.rcarouselfigure;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.widget.ImageView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
/**
* Created by Administrator on 2016/9/20 0020.
*/
public class VolleyRequestUtil {
public LruCache<String,Bitmap> mLruCache;
VolleyRequestListener mListener;
public String[] mRequestAddress;
RequestQueue mRequestQueue;
int a = 0;
int b = 0;
public VolleyRequestUtil(Context context){
setCache();
mRequestQueue = Volley.newRequestQueue(context);
}
public VolleyRequestUtil(Context context, String[] address) {
mRequestAddress = address;
mRequestQueue = Volley.newRequestQueue(context);
setCache();
}
public void startGroupRequest() {
for (; a < mRequestAddress.length; a++) {
mRequestQueue.add(new ImageRequest(mRequestAddress[a], new Response.Listener<Bitmap>() {
String address = mRequestAddress[a];
@Override
public void onResponse(Bitmap response) {
mLruCache.put(address,response);
b++;
if (b == mRequestAddress.length) {
mListener.onRequestFinish();
a = 0;
b = 0;
}
}
}, 720, 0, ImageView.ScaleType.CENTER_CROP, null, null));
}
}
public void startVolleyRequest(final String address, final ImageView imageView) {
if(address == null||imageView==null){
return;
}
mRequestQueue.add(new ImageRequest(address, new Response.Listener<Bitmap>() {
String pic_addresss = address;
@Override
public void onResponse(Bitmap response) {
imageView.setImageBitmap(response);
mLruCache.put(pic_addresss,response);
}
}, 720, 0, ImageView.ScaleType.CENTER_CROP, null, null));
}
private void setCache() {
int maxCacheSize = (int)Runtime.getRuntime().maxMemory();
int cacheSize = maxCacheSize/7;
mLruCache = new LruCache<String,Bitmap>(cacheSize){
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
}
public interface VolleyRequestListener {
void onRequestFinish();
}
public void setVolleyRequestListener(VolleyRequestListener listener) {
mListener = listener;
}
public void setmRequestAddress(String[] address){
mRequestAddress = address;
}
}
Volley请求模板
最新推荐文章于 2017-09-21 16:30:27 发布