布局文件
actviity_main.xml
<?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"> <com.huewu.pla.lib.MultiColumnListView xmlns:pla="http://schemas.android.com/apk/res-auto" android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" pla:plaColumnNumber="2"/> </LinearLayout>image_item.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <com.android.volley.toolbox.NetworkImageView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/row_icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="1dp" android:adjustViewBounds="true"/> </FrameLayout>
package com.atguigu.platest; import java.util.ArrayList; import com.huewu.pla.lib.MultiColumnListView; import com.huewu.pla.sample.R; import android.app.Activity; import android.os.Bundle; public class SampleActivity extends Activity { //获取listview private MultiColumnListView listview; //获取图片集合 ArrayList<String> imageList = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.actviity_main); init(); } private void init() { listview = (MultiColumnListView) findViewById(R.id.listview); imageList.add("http://www.yjz9.com/uploadfile/2012/1219/20121219043531502.jpg"); imageList.add("http://www.yjz9.com/uploadfile/2012/1219/20121219043533581.jpg"); imageList.add("http://www.yjz9.com/uploadfile/2012/1219/20121219043533571.jpg"); imageList.add("http://www.yjz9.com/uploadfile/2012/1219/20121219043534672.jpg"); imageList.add("http://www.yjz9.com/uploadfile/2012/1219/20121219043534854.jpg"); imageList.add("http://www.yjz9.com/uploadfile/2012/1219/20121219043535929.jpg"); imageList.add("http://www.yjz9.com/uploadfile/2012/1219/20121219043535784.jpg"); imageList.add("http://www.yjz9.com/uploadfile/2012/1219/20121219043536626.jpg"); imageList.add("http://www.yjz9.com/uploadfile/2012/1219/20121219043536244.jpg"); imageList.add("http://g.hiphotos.baidu.com/image/pic/item/43a7d933c895d14323898b5970f082025baf07c8.jpg"); imageList.add("http://c.hiphotos.baidu.com/image/pic/item/b3b7d0a20cf431ad99736b5d4836acaf2edd9834.jpg"); //设置适配器 listview.setAdapter(new WaterfallAdapter(imageList, this)); } }WaterfallAdapter.java
package com.atguigu.platest; import java.util.ArrayList; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import com.android.volley.toolbox.NetworkImageView; import com.huewu.pla.sample.R; public class WaterfallAdapter extends BaseAdapter { ArrayList<String> list; Context context; public WaterfallAdapter(ArrayList<String> list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int arg0) { return 0; } @Override public View getView(final int position, View view, ViewGroup group) { final Holder holder; // 得到View if (view == null) { holder = new Holder(); LayoutInflater inflater = LayoutInflater.from(context); view = inflater.inflate(R.layout.image_item, null); holder.ivIcon = (NetworkImageView) view.findViewById(R.id.row_icon); view.setTag(holder); } else { holder = (Holder) view.getTag(); } String url = list.get(position); holder.ivIcon.setDefaultImageResId(R.drawable.load_default); holder.ivIcon.setImageUrl(url, VolleyUtils.getInstance(context) .getImageLoader()); return view; } } class Holder { public NetworkImageView ivIcon; }VolleyUtils.java
package com.atguigu.platest; import android.content.Context; import android.graphics.Bitmap; import android.support.v4.util.LruCache; import android.util.Log; import com.android.volley.RequestQueue; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.ImageLoader.ImageCache; import com.android.volley.toolbox.Volley; public class VolleyUtils { private RequestQueue queue; private ImageLoader imageLoader; //私有静态实例 private static VolleyUtils instance; //私有构造方法 private VolleyUtils(Context context) { this.queue = Volley.newRequestQueue(context); this.imageLoader = new ImageLoader(queue, new LruImageCache()); } //对外提供公共的静态方法 public static VolleyUtils getInstance(Context context) { if (instance == null) { instance = new VolleyUtils(context); } return instance; } /* * 提供获取RequestQueue imageLoader方法 */ public RequestQueue getQueue() { return queue; } public ImageLoader getImageLoader() { return imageLoader; } /** * 使用LRU回收算法的Bitmap缓存的类 */ class LruImageCache implements ImageCache { //缓存图片对象的容器(内部使用LinkedHashMap) private LruCache<String, Bitmap> cache; public LruImageCache() { //得到缓存图片的最大内存 int maxSize = (int) Runtime.getRuntime().maxMemory() / 8; cache = new LruCache<String, Bitmap>(maxSize) { @Override protected int sizeOf(String key, Bitmap value) { // 得到一个图片的大小(byte) int size = value.getRowBytes() * value.getHeight(); return size; } @Override protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) { Log.e("TAG", "移除 "+oldValue); } }; } /** * 从缓存中得到图片对象 */ @Override public Bitmap getBitmap(String url) { return cache.get(url); } /** * 缓存图片 * 如果超过了设置的最大缓存, 内部会移除使用最少的bitmap对象 */ @Override public void putBitmap(String url, Bitmap bitmap) { cache.put(url, bitmap); } } }