//整理了网上的一些资料,运用了内存缓存和文件缓存两种技术,希望对大家有用 public class BitmapManager { private static HashMap<String, SoftReference<Bitmap>> cache; //图片缓存是当有加载过相同的图片的时候,可以快速重复使用,比如同一个人的头像。 private static ExecutorService pool; //固定线程池里的并发线程数,可以防止用户在快速滑动列表的时候,不执行已经滑过去的加载线程。 private static Map<ImageView, String> imageViews; private Bitmap defaultBmp; static { cache = new HashMap<String, SoftReference<Bitmap>>(); pool = Executors.newFixedThreadPool(5); //固定线程池 imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>()); } public BitmapManager(){} public BitmapManager(Bitmap def) { this.defaultBmp = def; } /** * 设置默认图片 * @param bmp */ public void setDefaultBmp(Bitmap bmp) { defaultBmp = bmp; } /** * 加载图片 * @param url * @param imageView */ public void loadBitmap(String url, ImageView imageView) { loadBitmap(url, imageView, this.defaultBmp, 0, 0); } /** * 加载图片-可设置加载失败后显示的默认图片 * @param url * @param imageView * @param defaultBmp */ public void loadBitmap(String url, ImageView imageView, Bitmap defaultBmp) { loadBitmap(url, imageView, defaultBmp, 0, 0); } /** * 加载图片-可指定显示图片的高宽 * 根据图片的url地址,先从图片缓存里面查找是否已缓存过,如果没有,再从SD卡的图片缓存文件中查找,如果再没有,最后才是加载网络图片 * @param url * @param imageView * @param width * @param height */ public void loadBitmap(String url, ImageView imageView, Bitmap defaultBmp, int width, int height) { imageViews.put(imageView, url); Bitmap bitmap = getBitmapFromCache(url); if (bitmap != null) { //显示缓存图片 imageView.setImageBitmap(bitmap); } else { //加载SD卡中的图片缓 String filename = FileUtils.getFileName(url); // String filepath = imageView.getContext().getFilesDir() + File.separator + filename; String CachePath = FileUtils.getExternalCacheDir("adv").getPath(); String filepath = CachePath+"/"+filename; File file = new File(filepath); if(file.exists()){ //显示SD卡中的图片缓 // Bitmap bmp = ImageUtils.getBitmap(imageView.getContext(), filename); Bitmap bmp = ImageUtils.getBitmapByPath(file.getPath()); imageView.setImageBitmap(bmp); }else{ //线程加载网络图片 imageView.setImageBitmap(defaultBmp); queueJob(url, imageView, width, height); } } } /** * 从缓存中获取图片 * @param url */ public Bitmap getBitmapFromCache(String url) { Bitmap bitmap = null; if (cache.containsKey(url)) { bitmap = cache.get(url).get(); } return bitmap; } /** * 从网络中加载图片 * @param url * @param imageView * @param width * @param height */ public void queueJob(final String url, final ImageView imageView, final int width, final int height) { /* Create handler in UI thread. */ final Handler handler = new Handler() { public void handleMessage(Message msg) { String tag = imageViews.get(imageView); if (tag != null && tag.equals(url)) { if (msg.obj != null) { imageView.setImageBitmap((Bitmap) msg.obj); try { //向SD卡中写入图片缓存 String path = FileUtils.getExternalCacheDir("adv").getPath(); // String path = FileUtils.getSDPath(); ImageUtils.saveImageToSD(path+"/"+FileUtils.getFileName(url), (Bitmap) msg.obj, 100); // ImageUtils.saveImage(imageView.getContext(), FileUtils.getFileName(url), (Bitmap) msg.obj); } catch (IOException e) { e.printStackTrace(); } } } } }; pool.execute(new Runnable() { public void run() { Message message = Message.obtain(); message.obj = downloadBitmap(url, width, height); handler.sendMessage(message); } }); } /** * 下载图片-可指定显示图片的高宽 * @param url * @param width * @param height */ private Bitmap downloadBitmap(String url, int width, int height) { Bitmap bitmap = null; try { //http加载图片 bitmap = ApiClient.getNetBitmap(url); if(width > 0 && height > 0) { bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true); } //放入缓存 cache.put(url, new SoftReference<Bitmap>(bitmap)); } catch (Exception e) { e.printStackTrace(); } return bitmap; } } |
listview中图片的异步加载,双缓存
最新推荐文章于 2021-05-27 09:20:15 发布