Volley获取网络图片使用总结

参考资料

http://www.3fwork.com/b600/001956MYM002697/

    //缓存
    int cacheSize= 10 * 1024 * 1024;
    //声明一个新的RequestQueue对象
    RequestQueue mQueue;
    ImageLoader imageLoader;


        //初始化RequsetQueue和ImageLoader
        mQueue = Volley.newRequestQueue(this);

        imageLoader= new ImageLoader(mQueue, new BitmapLruCache(cacheSize));
        mImageView = (ImageView) findViewById(R.id.myImage);

        String url = "http://i.imgur.com/7spzG.png";
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(mImageView,R.drawable.happy7, R.drawable.happy7);
//后两个参数分别是加载的默认图和加载出错后要显示的图片
        imageLoader.get(url, listener,200,200);

缓存代码

package com.firefly.volleytest;

import android.graphics.Bitmap;
import android.util.LruCache;

import com.android.volley.toolbox.ImageLoader;

public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageLoader.ImageCache {
    public BitmapLruCache(int maxSize) {
        super(maxSize);
    }

    protected int sizeOf(String key, Bitmap bitmap) {
        return (bitmap.getRowBytes() * bitmap.getHeight());
    }

    public Bitmap getBitmap(String url) {
        return ((Bitmap) get(url));
    }

    public void putBitmap(String url, Bitmap bitmap) {
        put(url, bitmap);
    }
}

 

posted @ 2016-06-16 20:48  春风自是人间客  阅读(428)  评论(0编辑  收藏  举报