Android简易实战教程--第三十二话《使用Lrucache和NetworkImageView加载图片》

转载本专栏每一篇博客请注明转载出处地址,尊重原创。此博客转载链接地址:小杨的博客    http://blog.csdn.net/qq_32059827/article/details/52791311


本博客是所用vooley框架完成的一个小案例,如果想详细学习该框架的使用。可以关注本人专栏《Android进阶》下的volley框架详解使用。

好了,看到这里说明,你对该框架使用应该算是入门了,那就开始实战之旅吧!

首先,定义一个布局,只用一个ListView就好了了。

然后,自定义一个adapter适配器

package 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加载、item数据设置的时候。不再使用ImageView,而是使用了NetworkImageView

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);
	}
}

可以把程序跑起来了,如下:(由于服务器崩掉了,暂时使用默认图片吧,见谅)



原文地址:https://www.cnblogs.com/wanghang/p/6299570.html