Android簡易實戰教程--第三十二話《使用Lrucache和NetworkImageView載入圖片》
阿新 • • 發佈:2019-01-31
轉載本專欄每一篇部落格請註明轉載出處地址,尊重原創。此部落格轉載連結地址:小楊的部落格 http://blog.csdn.net/qq_32059827/article/details/52791311
本部落格是所用vooley框架完成的一個小案例,如果想詳細學習該框架的使用。可以關注本人專欄《Android進階》下的volley框架詳解使用。
好了,看到這裡說明,你對該框架使用應該算是入門了,那就開始實戰之旅吧!
首先,定義一個佈局,只用一個ListView就好了了。
然後,自定義一個adapter介面卡
介面卡沒什麼可說的,主要是在item載入、item資料設定的時候。不再使用ImageView,而是使用了NetworkImageViewpackage com.leslie.volleylistviewdemo; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import com.android.volley.RequestQueue; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.NetworkImageView; import com.android.volley.toolbox.Volley; public class MyAdapter extends BaseAdapter { private String[] list; private Context context; private RequestQueue queue; private ImageLoader imageLoader; public MyAdapter(Context context, String[] list) { this.context = context; this.list = list; queue = Volley.newRequestQueue(context); imageLoader = new ImageLoader(queue, new MyImageCache()); } @Override public int getCount() { return list.length; } @Override public Object getItem(int position) { return list[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null); holder.img = (NetworkImageView) convertView.findViewById(R.id.userimage); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } final String imgUrl = list[position]; if (imgUrl != null && !imgUrl.equals("")) { holder.img.setDefaultImageResId(R.drawable.ic_launcher); holder.img.setErrorImageResId(R.drawable.ic_launcher); holder.img.setImageUrl(imgUrl, imageLoader); } return convertView; } class ViewHolder { NetworkImageView img; } }
item的佈局如下:
<?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" > <com.android.volley.toolbox.NetworkImageView android:id="@+id/userimage" android:layout_width="100dp" android:layout_height="100dp" /> </LinearLayout>
然後就是它的使用,如果您看過《Android進階專欄》的volley使用,相信這已經很明瞭了。
在使用NetworkImageView的時候,需要傳入自定義的快取類物件LruCache:
package com.leslie.volleylistviewdemo; import android.annotation.SuppressLint; import android.graphics.Bitmap; import android.support.v4.util.LruCache; import com.android.volley.toolbox.ImageLoader.ImageCache; /** * 自定義一個記憶體快取,基於的LRU演算法的記憶體快取 * @author Administrator * */ @SuppressLint("NewApi") public class MyImageCache implements ImageCache { LruCache<String, Bitmap> caches; //1.定義快取的空間大小 int maxSize = 4 * 1024 * 1024;//單位是byte-->4194304byte // int maxSize = 4;//單位是m-->4M //快取的最大值 4m 4*1024*1024kb ,是空間大小.不是元素個數 public MyImageCache() { caches = new LruCache<String, Bitmap>(maxSize) { //2.過載sizeOf @Override protected int sizeOf(String key, Bitmap value) { // TODO //返回bitmap這個entry的大小,統一計算單位 // return value.getByteCount() / 1024 / 1024;//一張圖片,佔了多少M return value.getByteCount();//一張圖片,佔了多少bytes } }; } /** * 從快取裡面取圖片 */ @Override public Bitmap getBitmap(String url) { System.out.println("--------------從快取中載入--------------"); return caches.get(url); } /** * 放到快取裡面去 */ @Override public void putBitmap(String url, Bitmap bitmap) { System.out.println("--------------放置到快取--------------"); caches.put(url, bitmap); } }
載入圖片和移除快取圖片的時候,我加入了Log。待會再看列印怎麼輸出的。
最後,看看MainActivity程式碼:
package com.leslie.volleylistviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listView;
String[] images = new String[] { "http://192.168.1.100:8080/img/b7003af33a87e950bc07153d12385343fbf2b44b.jpg",
"http://192.168.1.100:8080/img/b7fd5266d01609244a0c6b55d50735fae6cd3457.jpg",
"http://192.168.1.100:8080/img/b86afbbf9f78ae5dddfaa4c72c3afe81.jpg",
"http://192.168.1.100:8080/img/b89230d85178eafa7513a8601ba01d6e.jpg",
"http://192.168.1.100:8080/img/bba1cd11728b4710fc4148cbc1cec3fdfc03234e.jpg",
"http://192.168.1.100:8080/img/bigimg.jpg",
"http://192.168.1.100:8080/img/c0a11a6489b8f14e1f02d0be7afc0cad(1).jpg",
"http://192.168.1.100:8080/img/c0a11a6489b8f14e1f02d0be7afc0cad.jpg",
"http://192.168.1.100:8080/img/c8529ee742fa8abb853b297fef5ad772.jpg",
"http://192.168.1.100:8080/img/c8a9338d3020ea03d66aca22c34cef3f.jpg",
"http://192.168.1.100:8080/img/c9fcc3cec3fdfc03da9554bfd63f8794a5c226c3.jpg",
"http://192.168.1.100:8080/img/ca1349540923dd54489f106fd309b3de9d8248cc.jpg",
"http://192.168.1.100:8080/img/cbdee99ceabdfc7c76a78803c06d3b5b.jpg",
"http://192.168.1.100:8080/img/cdbbf5d62e08e4466fdbd526031145ee.jpg",
"http://192.168.1.100:8080/img/ce9dfc60b7002d583ffd3277def6dec7.jpg",
"http://192.168.1.100:8080/img/d01373f082025aafa359acd3f9edab64034f1aba.jpg",
"http://192.168.1.100:8080/img/d043ad4bd11373f02e541a9ea60f4bfbfbed0443.jpg",
"http://192.168.1.100:8080/img/d1160924ab18972b8bd74acde4cd7b899f510ac3.jpg",
"http://192.168.1.100:8080/img/d439b6003af33a8760a4e74bc45c10385243b592.jpg",
"http://192.168.1.100:8080/img/d6680a5223ac83ab60720d3536b42363.jpg",
"http://192.168.1.100:8080/img/d833c895d143ad4b99b5d1c080025aafa40f0612.jpg",
"http://192.168.1.100:8080/img/db08ab97060da52c484569bf7113d8ea.jpg",
"http://192.168.1.100:8080/img/de2563343fededa0aea0bfe5a040840c.jpg",
"http://192.168.1.100:8080/img/e01491bfc1a6f9fdd273779b582d3f53.jpg",
"http://192.168.1.100:8080/img/e7c04293605ff797a8adbb9369dda2eb.jpg",
"http://192.168.1.100:8080/img/e7cd7b899e510fb36deec090db33c895d0430cc5.jpg",
"http://192.168.1.100:8080/img/eac4b74543a98226e5f5cd768882b9014a90ebaf.jpg",
"http://192.168.1.100:8080/img/f2deb48f8c5494ee7e883e112ff5e0fe99257e0b.jpg",
"http://192.168.1.100:8080/img/f6baf658a81853bcc3b383b1d886d16e.jpg",
"http://192.168.1.100:8080/img/f87228e52049993ea750a79e39f1e3b9.jpg",
"http://192.168.1.100:8080/img/fa7ac7801e26b0a01f99dfeab0e20e6e.jpg",
"http://192.168.1.100:8080/img/fcfaaf51f3deb48f9d86ebd9f21f3a292cf578cd.jpg",
"http://192.168.1.100:8080/img/1000x1500_803068byte.jpg",
"http://192.168.1.100:8080/img/1020x637_312320byte.jpg",
"http://192.168.1.100:8080/img/fda7c901b289af73f4b5e9d9cc21a069.jpg"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview);
MyAdapter adapter = new MyAdapter(this, null);
listView.setAdapter(adapter);
}
}
可以把程式跑起來了,如下:(由於伺服器崩掉了,暫時使用預設圖片吧,見諒)