LruCache为GridView异步加载大量网络图片
MainActivity如下:
- import android.os.Bundle;
- import android.widget.GridView;
- import android.app.Activity;
- /**
- * Demo描述:
- * 在GridView中采用LruCache异步加载大量图片,避免OOM
- *
- * 学习资料:
- * http://blog.csdn.net/guolin_blog/article/details/9526203
- * Thank you very much
- */
- public class MainActivity extends Activity {
- private GridView mGridView;
- private GridViewAdapter mGridViewAdapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
- private void init(){
- mGridView = (GridView) findViewById(R.id.gridView);
- mGridViewAdapter = new GridViewAdapter(this, 0, ImagesUrl.Urls, mGridView);
- mGridView.setAdapter(mGridViewAdapter);
- }
- //取消所有的下载任务
- @Override
- protected void onDestroy() {
- super.onDestroy();
- mGridViewAdapter.cancelAllTasks();
- }
GridViewAdapter如下:
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.HashSet;
- import android.annotation.SuppressLint;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.AsyncTask;
- import android.support.v4.util.LruCache;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.AbsListView.OnScrollListener;
- import android.widget.ArrayAdapter;
- import android.widget.GridView;
- import android.widget.ImageView;
- /**
- * LruCache的流程分析:
- * 我们从第一次进入应用的情况下开始
- * 1 依据图片的Url从LruCache缓存中取图片.
- * 若图片存在缓存中,则显示该图片;否则显示默认图片
- * 2 因为是第一次进入该界面所以会执行:
- * loadBitmaps(firstVisibleItem, visibleItemCount);
- * 我们从loadBitmaps()方法作为切入点,继续往下梳理
- * 3 尝试从LruCache缓存中取图片.如果在显示即可,否则进入4
- * 4 开启一个异步任务下载图片.下载完成后显示图片,并且将
- * 该图片存入LruCache缓存中
- *
- * 在停止滑动时,会调用loadBitmaps(firstVisibleItem, visibleItemCount)
- * 情况与上类似
- */
- @SuppressLint("NewApi")
- public class GridViewAdapter extends ArrayAdapter<String> {
- private GridView mGridView;
- //图片缓存类
- private LruCache<String, Bitmap> mLruCache;
- //记录所有正在下载或等待下载的任务
- private HashSet<DownloadBitmapAsyncTask> mDownloadBitmapAsyncTaskHashSet;
- //GridView中可见的第一张图片的下标
- private int mFirstVisibleItem;
- //GridView中可见的图片的数量
- private int mVisibleItemCount;
- //记录是否是第一次进入该界面
- private boolean isFirstEnterThisActivity = true;
- public GridViewAdapter(Context context, int textViewResourceId,String[] objects, GridView gridView) {
- super(context, textViewResourceId, objects);
- mGridView = gridView;
- mGridView.setOnScrollListener(new ScrollListenerImpl());
- mDownloadBitmapAsyncTaskHashSet = new HashSet<DownloadBitmapAsyncTask>();
- // 获取应用程序最大可用内存
- int maxMemory = (int) Runtime.getRuntime().maxMemory();
- // 设置图片缓存大小为maxMemory的1/6
- int cacheSize = maxMemory/6;
- mLruCache = new LruCache<String, Bitmap>(cacheSize) {
- @Override
- protected int sizeOf(String key, Bitmap bitmap) {
- return bitmap.getByteCount();
- }
- };
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- String url = getItem(position);
- View view;
- if (convertView == null) {
- view = LayoutInflater.from(getContext()).inflate(R.layout.gridview_item, null);
- } else {
- view = convertView;
- }
- ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
- //为该ImageView设置一个Tag,防止图片错位
- imageView.setTag(url);
- //为该ImageView设置显示的图片
- setImageForImageView(url, imageView);
- return view;
- }
- /**
- * 为ImageView设置图片(Image)
- * 1 从缓存中获取图片
- * 2 若图片不在缓存中则为其设置默认图片
- */
- private void setImageForImageView(String imageUrl, ImageView imageView) {
- Bitmap bitmap = getBitmapFromLruCache(imageUrl);
- if (bitmap != null) {
- imageView.setImageBitmap(bitmap);
- } else {
- imageView.setImageResource(R.drawable.default_image);
- }
- }
- /**
- * 将图片存储到LruCache
- */
- public void addBitmapToLruCache(String key, Bitmap bitmap) {
- if (getBitmapFromLruCache(key) == null) {
- mLruCache.put(key, bitmap);
- }
- }
- /**
- * 从LruCache缓存获取图片
- */
- public Bitmap getBitmapFromLruCache(String key) {
- return mLruCache.get(key);
- }
- /**
- * 为GridView的item加载图片
- *
- * @param firstVisibleItem
- * GridView中可见的第一张图片的下标
- *
- * @param visibleItemCount
- * GridView中可见的图片的数量
- *
- */
- private void loadBitmaps(int firstVisibleItem, int visibleItemCount) {
- try {
- for (int i = firstVisibleItem; i < firstVisibleItem + visibleItemCount; i++) {
- String imageUrl = ImagesUrl.Urls[i];
- Bitmap bitmap = getBitmapFromLruCache(imageUrl);
- if (bitmap == null) {
- DownloadBitmapAsyncTask downloadBitmapAsyncTask = new DownloadBitmapAsyncTask();
- mDownloadBitmapAsyncTaskHashSet.add(downloadBitmapAsyncTask);
- downloadBitmapAsyncTask.execute(imageUrl);
- } else {
- //依据Tag找到对应的ImageView显示图片
- ImageView imageView = (ImageView) mGridView.findViewWithTag(imageUrl);
- if (imageView != null && bitmap != null) {
- imageView.setImageBitmap(bitmap);
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 取消所有正在下载或等待下载的任务
- */
- public void cancelAllTasks() {
- if (mDownloadBitmapAsyncTaskHashSet != null) {
- for (DownloadBitmapAsyncTask task : mDownloadBitmapAsyncTaskHashSet) {
- task.cancel(false);
- }
- }
- }
- private class ScrollListenerImpl implements OnScrollListener{
- /**
- *
- * 我们的本意是通过onScrollStateChanged获知:每次GridView停止滑动时加载图片
- * 但是存在一个特殊情况:
- * 当第一次入应用的时候,此时并没有滑动屏幕的操作即不会调用onScrollStateChanged,但应该加载图片.
- * 所以在此处做一个特殊的处理.
- * 即代码:
- * if (isFirstEnterThisActivity && visibleItemCount > 0) {
- * loadBitmaps(firstVisibleItem, visibleItemCount);
- * isFirstEnterThisActivity = false;
- * }
- *
- * ------------------------------------------------------------
- *
- * 其余的都是正常情况.
- * 所以我们需要不断保存:firstVisibleItem和visibleItemCount
- * 从而便于中在onScrollStateChanged()判断当停止滑动时加载图片
- *
- */
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
- mFirstVisibleItem = firstVisibleItem;
- mVisibleItemCount = visibleItemCount;
- if (isFirstEnterThisActivity && visibleItemCount > 0) {
- loadBitmaps(firstVisibleItem, visibleItemCount);
- isFirstEnterThisActivity = false;
- }
- }
- /**
- * GridView停止滑动时下载图片
- * 其余情况下取消所有正在下载或者等待下载的任务
- */
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- if (scrollState == SCROLL_STATE_IDLE) {
- loadBitmaps(mFirstVisibleItem, mVisibleItemCount);
- } else {
- cancelAllTasks();
- }
- }
- }
- /**
- * 下载图片的异步任务
- */
- class DownloadBitmapAsyncTask extends AsyncTask<String, Void, Bitmap> {
- private String imageUrl;
- @Override
- protected Bitmap doInBackground(String... params) {
- imageUrl = params[0];
- Bitmap bitmap = downloadBitmap(params[0]);
- if (bitmap != null) {
- //下载完后,将其缓存到LrcCache
- addBitmapToLruCache(params[0], bitmap);
- }
- return bitmap;
- }
- @Override
- protected void onPostExecute(Bitmap bitmap) {
- super.onPostExecute(bitmap);
- //下载完成后,找到其对应的ImageView显示图片
- ImageView imageView = (ImageView) mGridView.findViewWithTag(imageUrl);
- if (imageView != null && bitmap != null) {
- imageView.setImageBitmap(bitmap);
- }
- mDownloadBitmapAsyncTaskHashSet.remove(this);
- }
- }
- // 获取Bitmap
- private Bitmap downloadBitmap(String imageUrl) {
- Bitmap bitmap = null;
- HttpURLConnection httpURLConnection = null;
- try {
- URL url = new URL(imageUrl);
- httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setConnectTimeout(5 * 1000);
- httpURLConnection.setReadTimeout(10 * 1000);
- httpURLConnection.setDoInput(true);
- httpURLConnection.setDoOutput(true);
- bitmap = BitmapFactory.decodeStream(httpURLConnection.getInputStream());
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (httpURLConnection != null) {
- httpURLConnection.disconnect();
- }
- }
- return bitmap;
- }
- }
ImagesUrl如下:
- public class ImagesUrl {
- public final static String[] Urls = new String[] {
- "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s160-c/A%252520Photographer.jpg",
- "https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s160-c/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
- "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s160-c/Another%252520Rockaway%252520Sunset.jpg",
- "https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s160-c/Antelope%252520Butte.jpg",
- "https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s160-c/Antelope%252520Hallway.jpg",
- "https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s160-c/Antelope%252520Walls.jpg",
- "https://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s160-c/Apre%2525CC%252580s%252520la%252520Pluie.jpg"};
}
你必须非常努力,才能看起来毫不费力。。。