Android Volley框架使用(二)

时间:2022-08-16 18:31:44

Android Volley框架使用(一)

使用 Volley 提供的 ImageLoader 工具类,来实现图片的加载、缓存、
二次采样

在使用Volley来从网络获取图片的时候,我们前面介绍了ImageRequest的使用,而其实Volley还提供了一个NetworkImageView类。利用这个类,我们可以更有效率地去从网络去获取图片,因为它里面帮我们多设置了一个缓存,帮我们自己去处理请求的队列。

例:
在activity_main.xml中定义一个GridView

<GridView
android:id="@+id/gridview"
android:clickable="true"
android:numColumns="3"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

同时定义GridView中的子布局 item.xml:使用NetworkImageView

<com.android.volley.toolbox.NetworkImageView
android:id="@+id/networkimage"
android:layout_width="300dp"
android:layout_height="300dp"/>

MainActivity代码

package com.example.hfs.volleyimageloaderdemo;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.LruCache;
import android.widget.GridView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.example.hfs.volleyimageloaderdemo.adapters.MyAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
// imageloader是批量加载图片
private ImageLoader mImageLoader;
private GridView mGridView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initImageLoader();

// 使用imageLoader ListView中使用的效果才能体现

initView();

initData();


}

private void initData() {

List<String> list=new ArrayList<>();

String[] imageurls = ImageUrl.imageurls;

for (int i = 0; i < imageurls.length; i++) {
list.add(imageurls[i]);
}


MyAdapter adapter=new MyAdapter(this,list,mImageLoader);

mGridView.setAdapter(adapter);
}

private void initView() {
mGridView = (GridView) findViewById(R.id.gridview);
}

private void initImageLoader() {
// 参数1 一个请求队列 实例化一个请求队列
RequestQueue queue= Volley.newRequestQueue(this);
ImageLoader.ImageCache imageCache=new ImageLoader.ImageCache() {

// 内存缓存
int maxSize= (int) (Runtime.getRuntime().totalMemory()/8);
LruCache<String,Bitmap> mLruCache=new LruCache<String,Bitmap>(maxSize){

// 告诉LruCache存入的数据多大
@Override
protected int sizeOf(String key, Bitmap value) {
// 存入每张图片的大小
return value.getRowBytes()*value.getHeight();
}
};

// 从缓存中获取数据
@Override
public Bitmap getBitmap(String url) {
return mLruCache.get(url);
}
// 第一次使用肯定没有数据,联网请求 ,将数据存起来,方便以后使用
@Override
public void putBitmap(String url, Bitmap bitmap) {
mLruCache.put(url,bitmap);
}
};
mImageLoader=new ImageLoader(queue,imageCache);
}
}

图片地址类

package com.example.hfs.volleyimageloaderdemo;

/**
* Created by HFS on 2016/4/26.
*/

public class ImageUrl {

public static String imageurls[] = new String[] {
"http://img.my.csdn.net/uploads/201407/26/1406383299_1976.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383291_6518.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383291_8239.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383290_9329.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383290_1042.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383275_3977.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383265_8550.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383264_3954.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383264_4787.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383264_8243.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383248_3693.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383243_5120.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383242_3127.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383242_9576.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383242_1721.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383219_5806.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383214_7794.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383213_4418.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383213_3557.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383210_8779.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383172_4577.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383166_3407.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383166_2224.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383166_7301.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383165_7197.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383150_8410.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383131_3736.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383130_5094.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383130_7393.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383129_8813.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383100_3554.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383093_7894.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383092_2432.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383092_3071.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383091_3119.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383059_6589.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383059_8814.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383059_2237.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383058_4330.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406383038_3602.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382942_3079.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382942_8125.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382942_4881.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382941_4559.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382941_3845.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382924_8955.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382923_2141.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382923_8437.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382922_6166.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382922_4843.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382905_5804.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382904_3362.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382904_2312.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382904_4960.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382900_2418.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382881_4490.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382881_5935.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382880_3865.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382880_4662.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382879_2553.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382862_5375.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382862_1748.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382861_7618.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382861_8606.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382861_8949.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382841_9821.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382840_6603.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382840_2405.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382840_6354.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382839_5779.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382810_7578.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382810_2436.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382809_3883.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382809_6269.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382808_4179.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382790_8326.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382789_7174.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382789_5170.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382789_4118.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382788_9532.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382767_3184.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382767_4772.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382766_4924.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382766_5762.jpg",
"http://img.my.csdn.net/uploads/201407/26/1406382765_7341.jpg"};
}

自定义的Adapter

package com.example.hfs.volleyimageloaderdemo.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.example.hfs.volleyimageloaderdemo.R;

import java.util.List;

/**
* Created by HFS on 2016/4/26.
*/

public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<String> mList;
private ImageLoader mImageLoader;


public MyAdapter(Context context, List<String> list, ImageLoader imageLoader) {
mContext = context;
mList = list;
mImageLoader = imageLoader;
}


@Override
public int getCount() {
int ret = 0;
if (mList != null) {
ret = mList.size();
}
return ret;
}

@Override
public Object getItem(int position) {
return mList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View ret = null;

if (convertView != null) {
ret = convertView;
} else {
ret = LayoutInflater.from(mContext).inflate(R.layout.item, null);
}
/*******************************ViewHolde*****************************/

ViewHolder holder = (ViewHolder) ret.getTag();

if (holder == null) {
holder = new ViewHolder();

holder.mImageView = ((ImageView) ret.findViewById(R.id.showImage));
holder.mNetworkImageView= (NetworkImageView) ret.findViewById(R.id.networkimage);

ret.setTag(holder);

}

// //holder.mImageView.setImageResource(R.mipmap.ic_launcher);
//// 创建一个空的Bitmap
// Bitmap bitmap=Bitmap.createBitmap(300,300, Bitmap.Config.ALPHA_8);
// holder.mImageView.setImageBitmap(bitmap);

/*******************************进行赋值**********************************/

// final ViewHolder finalHolder = holder;
// mImageLoader.get(mList.get(position), new ImageLoader.ImageListener() {
// @Override
// public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
//
// Bitmap bitmap = imageContainer.getBitmap();
//
// if (bitmap != null) {
// finalHolder.mImageView.setImageBitmap(bitmap);
// }
//
// }
//
// @Override
// public void onErrorResponse(VolleyError volleyError) {
// finalHolder.mImageView.setImageResource(R.mipmap.ic_launcher);
// }
// }, 300, 300);

/*******************使用NetworkImageView**************************/
holder.mNetworkImageView.setImageUrl(mList.get(position),mImageLoader);

return ret;
}

private class ViewHolder {
public ImageView mImageView;
public NetworkImageView mNetworkImageView;
}
}