adapter里面用BitmapFactory.decodeFile得到Bitmap,然后setImageBitmap提供给ImageView,可是这样加载listview的时候很慢,并且上下滑动的时候出现一个item的时候会卡一下,这个怎么解决呢?
另外,还想问一下,这些bitmap该在什么怎么回收呢?不知道把回收代码写在哪里……
求教,谢谢
------解决方案--------------------
1. decode成小图
2. 缓存(内存和磁盘结合)
3. decode等费时的操作不要放到主线程里
------解决方案--------------------
------解决方案--------------------
应该是在给setAdapter之前将图片读进来吧,getView会被频繁执行的,当然后卡
------解决方案--------------------
------解决方案--------------------
以下代码是异步加载图片的独立类:
- Java code
/** * 先从内存缓存中查找,若没有再开线程, * 从文件缓存中查找都没有,则从指定的url中查找,并对bitmap进行处理,最后对UI进行更新操作 * @author Treasure * */public class ImageLoader { MemoryCache memoryCache = new MemoryCache(); FileCache fileCache; private Map<ImageView, String> imageViews = Collections .synchronizedMap(new WeakHashMap<ImageView, String>()); // 线程池 ExecutorService executorService; public ImageLoader(Context context) { fileCache = new FileCache(context); executorService = Executors.newFixedThreadPool(5); } // 当进入listview时默认的图片,可换成你自己的默认图片 final int stub_id = R.drawable.none_logo; // 最主要的方法 public void displayImage(String url, ImageView imageView) { imageViews.put(imageView, url); // 先从内存缓存中查找 Bitmap bitmap = memoryCache.get(url); if (bitmap != null) imageView.setImageBitmap(bitmap); else { // 若没有的话,则开启新线程加载图片 queuePhoto(url, imageView); imageView.setImageResource(stub_id); } } private void queuePhoto(String url, ImageView imageView) { PhotoToLoad p = new PhotoToLoad(url, imageView); executorService.submit(new PhotosLoader(p)); } private Bitmap getBitmap(String url) { File f = fileCache.getFile(url); // 先从文件缓存中查找是否有 Bitmap b = decodeFile(f); if (b != null) { return b; } // 最后从指定的url中下载图片 try { Bitmap bitmap = null; URL imageUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection)imageUrl .openConnection(); conn.setConnectTimeout(30 * 1000); conn.setReadTimeout(30 * 1000); conn.setInstanceFollowRedirects(true); InputStream is = conn.getInputStream(); OutputStream os = new FileOutputStream(f); copyStream(is, os); os.close(); bitmap = decodeFile(f); return bitmap; } catch (Exception e) { e.printStackTrace(); return null; } } /** * decode 这个图片并且按比例缩放以减少内存消耗,虚拟机对每张图片的缓存大小也是有限制的 * @param f * @return */ private Bitmap decodeFile(File f) { try { // decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(f), null, o); // Find the correct scale value. It should be the power of 2. final int REQUIRED_SIZE = 70; int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 1; while (true) { if (width_tmp / 2 < REQUIRED_SIZE || height_tmp /2 < REQUIRED_SIZE) break; width_tmp /= 2; height_tmp /= 2; scale *= 2; } // decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); } catch (FileNotFoundException e) { } return null; } /** * Task for the queue * @author Treasure * */ private class PhotoToLoad { public String url; public ImageView imageView; public PhotoToLoad(String u, ImageView i) { url = u; imageView = i; } } class PhotosLoader implements Runnable { PhotoToLoad photoToLoad; PhotosLoader(PhotoToLoad photoToLoad) { this.photoToLoad = photoToLoad; } @Override public void run() { if (imageViewReused(photoToLoad)) return; Bitmap bmp = getBitmap(photoToLoad.url); memoryCache.put(photoToLoad.url, bmp); if (imageViewReused(photoToLoad)) return; BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad); // 更新的操作放在UI线程中 Activity a = (Activity)photoToLoad.imageView.getContext(); a.runOnUiThread(bd); } } /** * 防止图片错位 * @param photoToLoad * @return */ boolean imageViewReused(PhotoToLoad photoToLoad) { String tag = imageViews.get(photoToLoad.imageView); if (tag == null || !tag.equals(photoToLoad.url)) return true; return false; } class BitmapDisplayer implements Runnable { Bitmap bitmap; PhotoToLoad photoToLoad; public BitmapDisplayer(Bitmap b, PhotoToLoad p) { bitmap = b; photoToLoad = p; } @Override public void run() { if (imageViewReused(photoToLoad)) return; if (bitmap != null) photoToLoad.imageView.setImageBitmap(bitmap); else photoToLoad.imageView.setImageResource(stub_id); } } public void clearCache() { memoryCache.clear(); fileCache.clear(); } public static void copyStream(InputStream is, OutputStream os) { final int buffer_size = 1024; try { byte[] bytes = new byte[buffer_size]; for (; ;) { int count = is.read(bytes, 0, buffer_size); if (count == -1) break; os.write(bytes, 0, count); } } catch (Exception e) { } }}