Volley图片加载并加入缓存处理(转自http://blog.csdn.net/viewhandkownhealth/article/details/50957024)

直接上代码  两种方式 ImageView 和NetworkImageView

如有问题或者好的建议、意见 欢迎大家加入技术群(群号: 387648673 )

先自定义全局Application 获取

public classMyApplication extends Application {
      private static RequestQueuerequestQueue;
      private static MyApplicationapplication;
      
      @Override
      public void onCreate() {
           // TODO Auto-generated methodstub
           super.onCreate();
           application = this;
           requestQueue =Volley.newRequestQueue(this);
      }
      public static MyApplicationgetInstance() {
           // TODO Auto-generated methodstub
           return application;
      }
      public static RequestQueuegetRequestQueue(){
           
           return requestQueue;
      }
}

先上布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<ImageView
        android:id="@+id/iv_test1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginTop="10dp"/>

<com.android.volley.toolbox.NetworkImageView
        android:id="@+id/iv_test2"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp" >
    </com.android.volley.toolbox.NetworkImageView>

</LinearLayout>

方式一用ImageView加载

需要封装一下自定义的BitmapCache类实现图片缓存接口ImageCache

public class BitmapCache implements ImageCache{

public LruCache<String, Bitmap> cache;

public BitmapCache(){
int maxMermorySize = (int)(Runtime.getRuntime().maxMemory()/1024);
cache = new LruCache<String, Bitmap>(maxMermorySize/8){
@Override
protected int sizeOf(String key, Bitmap value) {
// TODO Auto-generated method stub
return value.getRowBytes()*value.getHeight()/1024;

}
};

}

@Override
public Bitmap getBitmap(String key) {
// TODO Auto-generated method stub
return cache.get(key);
}

@Override
public void putBitmap(String key, Bitmap value) {
// TODO Auto-generated method stub
cache.put(key, value);
}

}

方式二 用NetworkImageView记载

封装一个VolleyUtils图片加载并处理缓存的类,使用起来就比较方便了

public class VolleyUtils {

private static ImageLoader imageLoader;
private static LruCache<String,Bitmap> lruCache;
private static HashMap<String,SoftReference<Bitmap>> softmap;

private static void initCache() {
// TODO Auto-generated method stub
//实例化二级缓存
softmap = new HashMap<String, SoftReference<Bitmap>>();
//实例化一级缓存  字节单位 KB
int maxMermorySize = (int)(Runtime.getRuntime().maxMemory()/1024);
lruCache = new LruCache<String, Bitmap>(maxMermorySize/8){
@Override
protected int sizeOf(String key, Bitmap value) {
// TODO Auto-generated method stub
//return lruCache.size()
return value.getRowBytes()*value.getHeight()/1024;//返回KB的大小
}
@Override
protected void entryRemoved(boolean evicted, String key,
Bitmap oldValue, Bitmap newValue) {
//当oldValue被一级缓存逐出时,放入二级缓存中
if (evicted) {
softmap.put(key, new SoftReference<Bitmap>(oldValue));
}
}
};

}
public static ImageLoader getImageLoader(Context context){

if (imageLoader==null) {
initCache();
imageLoader = new ImageLoader(MyApplication.getRequestQueue(), new ImageCache() {

@Override
public void putBitmap(String url, Bitmap bitmap) {
// 存入一级缓存
lruCache.put(url, bitmap);
}

@Override
public Bitmap getBitmap(String url) {

//从一级缓存中获取
Bitmap bitmap = lruCache.get(url);
//判断一级缓存是否存在
if ( bitmap== null) {//一级缓存不存在
//从二级缓存中获取
SoftReference<Bitmap> softRef = softmap.get(url);
if (softRef!=null) {
bitmap = softRef.get();
//获取软引用中图片
//为了便于使用将二级缓存中的图片存放一级缓存中
     lruCache.put(url, bitmap);
     //将此图片从二级缓存中移除
     softmap.remove(url);
}
}
return null;
}
});
}
return imageLoader;

}

}

在Activity或者Fragment或者适配中可以直接调取用,下边是以Activity为实例的代码

private ImageView test1;//用ImageView加载网略图片

private NetworkImageView test2;//用NetworkImageView记载网略图片

@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
test1 = (ImageView)findViewById(R.id.iv_test1);

test2 = (NetworkImageView) findViewById(R.id.iv_test2);

initImage();
}

private void initImage(){

//本人随便找了一张美女的图片

String url = "http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1205/25/c2/11755122_1337938898582.jpg";

//方式一用ImageView加载
ImageLoader loader = new ImageLoader(MyApplication.getRequestQueue(), new BitmapCache());
ImageListener listener = ImageLoader.getImageListener(test1, 
R.drawable.ic_launcher, R.drawable.ic_launcher);
loader.get(url, listener);

//方式二用NetworkImageView加载

ImageLoader imageLoader = VolleyUtils.getImageLoader(this);
test2.setDefaultImageResId(R.drawable.ic_launcher);
test2.setErrorImageResId(R.drawable.ic_launcher);
test2.setImageUrl(url, imageLoader);

}

上一篇:echarts通过ajax向服务器发送post请求,servlet从数据库读取数据并返回前端


下一篇:【LOJ】#2511. 「BJOI2018」双人猜数游戏