Volley请求模板

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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值