太原中企动力网站建设,wordpress淘宝模板,小公司网站建设需要多少钱,广州市城市建设档案馆网站本文主要包括以下内容
利用LruCache实现内存缓存 利用DiskLruCache实现磁盘缓存 LruCache与DiskLruCache结合实例 利用了缓存机制的瀑布流实例
内存缓存的实现
public class PhotoWallAdapter extends ArrayAdapterString implements OnScrollListener {/*** 记录…本文主要包括以下内容
利用LruCache实现内存缓存 利用DiskLruCache实现磁盘缓存 LruCache与DiskLruCache结合实例 利用了缓存机制的瀑布流实例
内存缓存的实现
public class PhotoWallAdapter extends ArrayAdapterString implements OnScrollListener {/*** 记录所有正在下载或等待下载的任务。*/private SetBitmapWorkerTask taskCollection;/*** 图片缓存技术的核心类用于缓存所有下载好的图片在程序内存达到设定值时会将最少最近使用的图片移除掉。*/private LruCacheString, Bitmap mMemoryCache;/*** GridView的实例*/private GridView mPhotoWall;/*** 第一张可见图片的下标*/private int mFirstVisibleItem;/*** 一屏有多少张图片可见*/private int mVisibleItemCount;/*** 记录是否刚打开程序用于解决进入程序不滚动屏幕不会下载图片的问题。*/private boolean isFirstEnter true;public PhotoWallAdapter(Context context, int textViewResourceId, String[] objects,GridView photoWall) {super(context, textViewResourceId, objects);mPhotoWall photoWall;taskCollection new HashSetBitmapWorkerTask();// 获取应用程序最大可用内存int maxMemory (int) Runtime.getRuntime().maxMemory();int cacheSize maxMemory / 8;// 设置图片缓存大小为程序最大可用内存的1/8mMemoryCache new LruCacheString, Bitmap(cacheSize) {Overrideprotected int sizeOf(String key, Bitmap bitmap) {return bitmap.getByteCount();}};mPhotoWall.setOnScrollListener(this);}Overridepublic View getView(int position, View convertView, ViewGroup parent) {final String url getItem(position);View view;if (convertView null) {view LayoutInflater.from(getContext()).inflate(R.layout.photo_layout, null);} else {view convertView;}final ImageView photo (ImageView) view.findViewById(R.id.photo);// 给ImageView设置一个Tag保证异步加载图片时不会乱序photo.setTag(url);setImageView(url, photo);return view;}/*** 给ImageView设置图片。首先从LruCache中取出图片的缓存设置到ImageView上。如果LruCache中没有该图片的缓存* 就给ImageView设置一张默认图片。* * param imageUrl* 图片的URL地址用于作为LruCache的键。* param imageView* 用于显示图片的控件。*/private void setImageView(String imageUrl, ImageView imageView) {Bitmap bitmap getBitmapFromMemoryCache(imageUrl);if (bitmap ! null) {imageView.setImageBitmap(bitmap);} else {imageView.setImageResource(R.drawable.empty_photo);}}/*** 将一张图片存储到LruCache中。* * param key* LruCache的键这里传入图片的URL地址。* param bitmap* LruCache的键这里传入从网络上下载的Bitmap对象。*/public void addBitmapToMemoryCache(String key, Bitmap bitmap) {if (getBitmapFromMemoryCache(key) null) {mMemoryCache.put(key, bitmap);}}/*** 从LruCache中获取一张图片如果不存在就返回null。* * param key* LruCache的键这里传入图片的URL地址。* return 对应传入键的Bitmap对象或者null。*/public Bitmap getBitmapFromMemoryCache(String key) {return mMemoryCache.get(key);}Overridepublic void onScrollStateChanged(AbsListView view, int scrollState) {// 仅当GridView静止时才去下载图片GridView滑动时取消所有正在下载的任务if (scrollState SCROLL_STATE_IDLE) {loadBitmaps(mFirstVisibleItem, mVisibleItemCount);} else {cancelAllTasks();}}Overridepublic void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,int totalItemCount) {mFirstVisibleItem firstVisibleItem;mVisibleItemCount visibleItemCount;// 下载的任务应该由onScrollStateChanged里调用但首次进入程序时onScrollStateChanged并不会调用// 因此在这里为首次进入程序开启下载任务。if (isFirstEnter visibleItemCount 0) {loadBitmaps(firstVisibleItem, visibleItemCount);isFirstEnter false;}}/*** 加载Bitmap对象。此方法会在LruCache中检查所有屏幕中可见的ImageView的Bitmap对象* 如果发现任何一个ImageView的Bitmap对象不在缓存中就会开启异步线程去下载图片。* * param firstVisibleItem* 第一个可见的ImageView的下标* param visibleItemCount* 屏幕中总共可见的元素数*/private void loadBitmaps(int firstVisibleItem, int visibleItemCount) {try {for (int i firstVisibleItem; i firstVisibleItem visibleItemCount; i) {String imageUrl Images.imageThumbUrls[i];Bitmap bitmap getBitmapFromMemoryCache(imageUrl);if (bitmap null) {BitmapWorkerTask task new BitmapWorkerTask();taskCollection.add(task);task.execute(imageUrl);} else {ImageView imageView (ImageView) mPhotoWall.findViewWithTag(imageUrl);if (imageView ! null bitmap ! null) {imageView.setImageBitmap(bitmap);}}}} catch (Exception e) {e.printStackTrace();}}/*** 取消所有正在下载或等待下载的任务。*/public void cancelAllTasks() {if (taskCollection ! null) {for (BitmapWorkerTask task : taskCollection) {task.cancel(false);}}}/*** 异步下载图片的任务。* * author guolin*/class BitmapWorkerTask extends AsyncTaskString, Void, Bitmap {/*** 图片的URL地址*/private String imageUrl;Overrideprotected Bitmap doInBackground(String... params) {imageUrl params[0];// 在后台开始下载图片Bitmap bitmap downloadBitmap(params[0]);if (bitmap ! null) {// 图片下载完成后缓存到LrcCache中addBitmapToMemoryCache(params[0], bitmap);}return bitmap;}Overrideprotected void onPostExecute(Bitmap bitmap) {super.onPostExecute(bitmap);// 根据Tag找到相应的ImageView控件将下载好的图片显示出来。ImageView imageView (ImageView) mPhotoWall.findViewWithTag(imageUrl);if (imageView ! null bitmap ! null) {imageView.setImageBitmap(bitmap);}taskCollection.remove(this);}/*** 建立HTTP请求并获取Bitmap对象。* * param imageUrl* 图片的URL地址* return 解析后的Bitmap对象*/private Bitmap downloadBitmap(String imageUrl) {Bitmap bitmap null;HttpURLConnection con null;try {URL url new URL(imageUrl);con (HttpURLConnection) url.openConnection();con.setConnectTimeout(5 * 1000);con.setReadTimeout(10 * 1000);bitmap BitmapFactory.decodeStream(con.getInputStream());} catch (Exception e) {e.printStackTrace();} finally {if (con ! null) {con.disconnect();}}return bitmap;}}}
PhotoWallAdapter是整个照片墙程序中最关键的一个类了这里我来重点给大家讲解一下。首先在PhotoWallAdapter的构造函数中我们初始化了LruCache类并设置了最大缓存容量为程序最大可用内存的1/8接下来又为GridView注册了一个滚动监听器。然后在getView()方法中我们为每个ImageView设置了一个唯一的Tag这个Tag的作用是为了后面能够准确地找回这个ImageView不然异步加载图片会出现乱序的情况。之后调用了setImageView()方法为ImageView设置一张图片这个方法首先会从LruCache缓存中查找是否已经缓存了这张图片如果成功找到则将缓存中的图片显示在ImageView上否则就显示一张默认的空图片。 看了半天那到底是在哪里下载图片的呢这是在GridView的滚动监听器中进行的在onScrollStateChanged()方法中我们对GridView的滚动状态进行了判断如果当前GridView是静止的则调用loadBitmaps()方法去下载图片如果GridView正在滚动则取消掉所有下载任务这样可以保证GridView滚动的流畅性。在loadBitmaps()方法中我们为屏幕上所有可见的GridView子元素开启了一个线程去执行下载任务下载成功后将图片存储到LruCache当中然后通过Tag找到相应的ImageView控件把下载好的图片显示出来。 由于我们使用了LruCache来缓存图片所以不需要担心内存溢出的情况当LruCache中存储图片的总大小达到容量上限的时候会自动把最近最少使用的图片从缓存中移除。
硬盘缓存实现
主要包括以下几步
1、 下载DiskLruCache的源码。下载好了源码之后只需要在项目中新建一个libcore.io包然后将DiskLruCache.java文件复制到这个包中即可。
2、 打开缓存
DiskLruCache mDiskLruCache null;
try {File cacheDir getDiskCacheDir(context, bitmap);if (!cacheDir.exists()) {cacheDir.mkdirs();}mDiskLruCache DiskLruCache.open(cacheDir, getAppVersion(context), 1, 10 * 1024 * 1024);
} catch (IOException e) {e.printStackTrace();
}
3、写入缓存
new Thread(new Runnable() {Overridepublic void run() {try {String imageUrl https://img-my.csdn.net/uploads/201309/01/1378037235_7476.jpg;String key hashKeyForDisk(imageUrl);DiskLruCache.Editor editor mDiskLruCache.edit(key);if (editor ! null) {OutputStream outputStream editor.newOutputStream(0);if (downloadUrlToStream(imageUrl, outputStream)) {editor.commit();} else {editor.abort();}}mDiskLruCache.flush();} catch (IOException e) {e.printStackTrace();}}
}).start();private boolean downloadUrlToStream(String urlString, OutputStream outputStream) {HttpURLConnection urlConnection null;BufferedOutputStream out null;BufferedInputStream in null;try {final URL url new URL(urlString);urlConnection (HttpURLConnection) url.openConnection();in new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024);out new BufferedOutputStream(outputStream, 8 * 1024);int b;while ((b in.read()) ! -1) {out.write(b);}return true;} catch (final IOException e) {e.printStackTrace();} finally {if (urlConnection ! null) {urlConnection.disconnect();}try {if (out ! null) {out.close();}if (in ! null) {in.close();}} catch (final IOException e) {e.printStackTrace();}}return false;
}4、读取缓存
try {String imageUrl https://img-my.csdn.net/uploads/201309/01/1378037235_7476.jpg;String key hashKeyForDisk(imageUrl);DiskLruCache.Snapshot snapShot mDiskLruCache.get(key);if (snapShot ! null) {InputStream is snapShot.getInputStream(0);Bitmap bitmap BitmapFactory.decodeStream(is);mImage.setImageBitmap(bitmap);}
} catch (IOException e) {e.printStackTrace();
}
移除缓存
try {String imageUrl https://img-my.csdn.net/uploads/201309/01/1378037235_7476.jpg; String key hashKeyForDisk(imageUrl); mDiskLruCache.remove(key);
} catch (IOException e) {e.printStackTrace();
}
LruCache与DiskLruCache结合实例
public class PhotoWallAdapter extends ArrayAdapterString {/*** 记录所有正在下载或等待下载的任务。*/private SetBitmapWorkerTask taskCollection;/*** 图片缓存技术的核心类用于缓存所有下载好的图片在程序内存达到设定值时会将最少最近使用的图片移除掉。*/private LruCacheString, Bitmap mMemoryCache;/*** 图片硬盘缓存核心类。*/private DiskLruCache mDiskLruCache;/*** GridView的实例*/private GridView mPhotoWall;/*** 记录每个子项的高度。*/private int mItemHeight 0;public PhotoWallAdapter(Context context, int textViewResourceId, String[] objects,GridView photoWall) {super(context, textViewResourceId, objects);mPhotoWall photoWall;taskCollection new HashSetBitmapWorkerTask();// 获取应用程序最大可用内存int maxMemory (int) Runtime.getRuntime().maxMemory();int cacheSize maxMemory / 8;// 设置图片缓存大小为程序最大可用内存的1/8mMemoryCache new LruCacheString, Bitmap(cacheSize) {Overrideprotected int sizeOf(String key, Bitmap bitmap) {return bitmap.getByteCount();}};try {// 获取图片缓存路径File cacheDir getDiskCacheDir(context, thumb);if (!cacheDir.exists()) {cacheDir.mkdirs();}// 创建DiskLruCache实例初始化缓存数据mDiskLruCache DiskLruCache.open(cacheDir, getAppVersion(context), 1, 10 * 1024 * 1024);} catch (IOException e) {e.printStackTrace();}}Overridepublic View getView(int position, View convertView, ViewGroup parent) {final String url getItem(position);View view;if (convertView null) {view LayoutInflater.from(getContext()).inflate(R.layout.photo_layout, null);} else {view convertView;}final ImageView imageView (ImageView) view.findViewById(R.id.photo);if (imageView.getLayoutParams().height ! mItemHeight) {imageView.getLayoutParams().height mItemHeight;}// 给ImageView设置一个Tag保证异步加载图片时不会乱序imageView.setTag(url);imageView.setImageResource(R.drawable.empty_photo);loadBitmaps(imageView, url);return view;}/*** 将一张图片存储到LruCache中。* * param key* LruCache的键这里传入图片的URL地址。* param bitmap* LruCache的键这里传入从网络上下载的Bitmap对象。*/public void addBitmapToMemoryCache(String key, Bitmap bitmap) {if (getBitmapFromMemoryCache(key) null) {mMemoryCache.put(key, bitmap);}}/*** 从LruCache中获取一张图片如果不存在就返回null。* * param key* LruCache的键这里传入图片的URL地址。* return 对应传入键的Bitmap对象或者null。*/public Bitmap getBitmapFromMemoryCache(String key) {return mMemoryCache.get(key);}/*** 加载Bitmap对象。此方法会在LruCache中检查所有屏幕中可见的ImageView的Bitmap对象* 如果发现任何一个ImageView的Bitmap对象不在缓存中就会开启异步线程去下载图片。*/public void loadBitmaps(ImageView imageView, String imageUrl) {try {Bitmap bitmap getBitmapFromMemoryCache(imageUrl);if (bitmap null) {BitmapWorkerTask task new BitmapWorkerTask();taskCollection.add(task);task.execute(imageUrl);} else {if (imageView ! null bitmap ! null) {imageView.setImageBitmap(bitmap);}}} catch (Exception e) {e.printStackTrace();}}/*** 取消所有正在下载或等待下载的任务。*/public void cancelAllTasks() {if (taskCollection ! null) {for (BitmapWorkerTask task : taskCollection) {task.cancel(false);}}}/*** 根据传入的uniqueName获取硬盘缓存的路径地址。*/public File getDiskCacheDir(Context context, String uniqueName) {String cachePath;if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())|| !Environment.isExternalStorageRemovable()) {cachePath context.getExternalCacheDir().getPath();} else {cachePath context.getCacheDir().getPath();}return new File(cachePath File.separator uniqueName);}/*** 获取当前应用程序的版本号。*/public int getAppVersion(Context context) {try {PackageInfo info context.getPackageManager().getPackageInfo(context.getPackageName(),0);return info.versionCode;} catch (NameNotFoundException e) {e.printStackTrace();}return 1;}/*** 设置item子项的高度。*/public void setItemHeight(int height) {if (height mItemHeight) {return;}mItemHeight height;notifyDataSetChanged();}/*** 使用MD5算法对传入的key进行加密并返回。*/public String hashKeyForDisk(String key) {String cacheKey;try {final MessageDigest mDigest MessageDigest.getInstance(MD5);mDigest.update(key.getBytes());cacheKey bytesToHexString(mDigest.digest());} catch (NoSuchAlgorithmException e) {cacheKey String.valueOf(key.hashCode());}return cacheKey;}/*** 将缓存记录同步到journal文件中。*/public void fluchCache() {if (mDiskLruCache ! null) {try {mDiskLruCache.flush();} catch (IOException e) {e.printStackTrace();}}}private String bytesToHexString(byte[] bytes) {StringBuilder sb new StringBuilder();for (int i 0; i bytes.length; i) {String hex Integer.toHexString(0xFF bytes[i]);if (hex.length() 1) {sb.append(0);}sb.append(hex);}return sb.toString();}/*** 异步下载图片的任务。* * author guolin*/class BitmapWorkerTask extends AsyncTaskString, Void, Bitmap {/*** 图片的URL地址*/private String imageUrl;Overrideprotected Bitmap doInBackground(String... params) {imageUrl params[0];FileDescriptor fileDescriptor null;FileInputStream fileInputStream null;Snapshot snapShot null;try {// 生成图片URL对应的keyfinal String key hashKeyForDisk(imageUrl);// 查找key对应的缓存snapShot mDiskLruCache.get(key);if (snapShot null) {// 如果没有找到对应的缓存则准备从网络上请求数据并写入缓存DiskLruCache.Editor editor mDiskLruCache.edit(key);if (editor ! null) {OutputStream outputStream editor.newOutputStream(0);if (downloadUrlToStream(imageUrl, outputStream)) {editor.commit();} else {editor.abort();}}// 缓存被写入后再次查找key对应的缓存snapShot mDiskLruCache.get(key);}if (snapShot ! null) {fileInputStream (FileInputStream) snapShot.getInputStream(0);fileDescriptor fileInputStream.getFD();}// 将缓存数据解析成Bitmap对象Bitmap bitmap null;if (fileDescriptor ! null) {bitmap BitmapFactory.decodeFileDescriptor(fileDescriptor);}if (bitmap ! null) {// 将Bitmap对象添加到内存缓存当中addBitmapToMemoryCache(params[0], bitmap);}return bitmap;} catch (IOException e) {e.printStackTrace();} finally {if (fileDescriptor null fileInputStream ! null) {try {fileInputStream.close();} catch (IOException e) {}}}return null;}Overrideprotected void onPostExecute(Bitmap bitmap) {super.onPostExecute(bitmap);// 根据Tag找到相应的ImageView控件将下载好的图片显示出来。ImageView imageView (ImageView) mPhotoWall.findViewWithTag(imageUrl);if (imageView ! null bitmap ! null) {imageView.setImageBitmap(bitmap);}taskCollection.remove(this);}/*** 建立HTTP请求并获取Bitmap对象。* * param imageUrl* 图片的URL地址* return 解析后的Bitmap对象*/private boolean downloadUrlToStream(String urlString, OutputStream outputStream) {HttpURLConnection urlConnection null;BufferedOutputStream out null;BufferedInputStream in null;try {final URL url new URL(urlString);urlConnection (HttpURLConnection) url.openConnection();in new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024);out new BufferedOutputStream(outputStream, 8 * 1024);int b;while ((b in.read()) ! -1) {out.write(b);}return true;} catch (final IOException e) {e.printStackTrace();} finally {if (urlConnection ! null) {urlConnection.disconnect();}try {if (out ! null) {out.close();}if (in ! null) {in.close();}} catch (final IOException e) {e.printStackTrace();}}return false;}}}
代码有点长我们一点点进行分析。首先在PhotoWallAdapter的构造函数中我们初始化了LruCache类并设置了内存缓存容量为程序最大可用内存的1/8紧接着调用了DiskLruCache的open()方法来创建实例并设置了硬盘缓存容量为10M这样我们就把LruCache和DiskLruCache的初始化工作完成了。 接着在getView()方法中我们为每个ImageView设置了一个唯一的Tag这个Tag的作用是为了后面能够准确地找回这个ImageView不然异步加载图片会出现乱序的情况。然后在getView()方法的最后调用了loadBitmaps()方法加载图片的具体逻辑也就是在这里执行的了。
进入到loadBitmaps()方法中可以看到实现是调用了getBitmapFromMemoryCache()方法来从内存中获取缓存如果获取到了则直接调用ImageView的setImageBitmap()方法将图片显示到界面上。如果内存中没有获取到则开启一个BitmapWorkerTask任务来去异步加载图片。 那么在BitmapWorkerTask的doInBackground()方法中我们就灵活运用了上篇文章中学习的DiskLruCache的各种用法。首先根据图片的URL生成对应的MD5 key然后调用DiskLruCache的get()方法来获取硬盘缓存如果没有获取到的话则从网络上请求图片并写入硬盘缓存接着将Bitmap对象解析出来并添加到内存缓存当中最后将这个Bitmap对象显示到界面上这样一个完整的流程就执行完了。 那么我们再来分析一下上述流程每次加载图片的时候都优先去内存缓存当中读取当读取不到的时候则回去硬盘缓存中读取而如果硬盘缓存仍然读取不到的话就从网络上请求原始数据。不管是从硬盘缓存还是从网络获取读取到了数据之后都应该添加到内存缓存当中这样的话我们下次再去读取图片的时候就能迅速从内存当中读取到而如果该图片从内存中被移除了的话那就重复再执行一遍上述流程就可以了。 这样我们就把LruCache和DiskLruCache完美结合到一起了。
效果如下 瀑布流效果 源码下载
内存缓存
内存与硬盘结合
瀑布流
参考链接
Android照片墙应用实现再多的图片也不怕崩溃 - 郭霖的专栏 - 博客频道 - CSDN.NET
Android DiskLruCache完全解析硬盘缓存的最佳方案 - 郭霖的专栏 - 博客频道 - CSDN.NET
Android照片墙完整版完美结合LruCache和DiskLruCache - 郭霖的专栏 - 博客频道 - CSDN.NET
Android瀑布流照片墙实现体验不规则排列的美感 - 郭霖的专栏 - 博客频道 - CSDN.NET