此问题已经有答案了: 如何在 Android 的 ListView 中使图片滑动流畅 如何在 Android 的 ListView 中使用懒加载
此问题已经有答案了:
如何在 Android 的 ListView 中使图片滑动流畅
如何在 Android 的 ListView 中使用懒加载
我有一个显示产品的 ListView。每个产品都有详细信息和 ImageView,但问题是我正在对产品的图片进行懒加载。而且这些图片都是高分辨率的。在滚动时会变得笨拙(不流畅)。因为下载图片需要时间。Facebook就是这样处理他们的图片,但滚动效果要平滑得多,请帮助找到解决方案。
\n
public class BitmapCacheManager { private static LruCache cache = null; private final Context context; private static final int KB = 1024; private final Drawable placeHolder; public BitmapCacheManager(Context context) { this.context = context; placeHolder = context.getResources().getDrawable(R.drawable.unknown); int maxMemory = (int) (Runtime.getRuntime().maxMemory() / KB); int cacheSize = maxMemory / 7; cache = new LruCache(cacheSize) { @Override protected int sizeOf(Object albumId, Bitmap bitmap) { return (bitmap.getRowBytes() * bitmap.getHeight() / KB); } protected void entryRemoved(boolean evicted, Object key, Bitmap oldValue, Bitmap newValue) { oldValue.recycle(); } }; } public void addBitmapToMemoryCache(Object key, Bitmap bitmap) { if (bitmap != null && key != null && cache.get(key) == null) cache.put(key, bitmap); } public Bitmap getBitmapFromMemCache(Object key) { return cache.get(key); } public void loadBitmap(final Object key, final ImageView imageView) { final Bitmap bitmap = getBitmapFromMemCache(key); if (bitmap != null) { imageView.setImageBitmap(bitmap); } else { imageView.setImageDrawable(placeHolder); BitmapWorker task = new BitmapWorker(imageView); task.execute(key); } } private class BitmapWorker extends AsyncTask { private final ImageView imageView; private Object key; public BitmapWorker(final ImageView imageView) { this.imageView = imageView; } @Implement protected Bitmap doInBackground(Object... params) { key = params[0]; final Bitmap b = SomeClass.GetSomeBitmap(context, key); addBitmapToMemoryCache(key, b); return b; } @Override protected void onPostExecute(final Bitmap bitmap) { if (bitmap == null) { imageView.setImageBitmap(SomeClass.DefaultBitmap); return; } if (imageView.getTag().toString().equalsIgnoreCase(key.toString()) && !bitmap.isRecycled()) imageView.setImageBitmap(bitmap); } } }
并呼叫:
bitmapCacheManager.loadBitmap(somekey, someImageView);
用户名或电子邮箱地址
密码