Android中的Listview延迟负载平滑

Android中的Listview延迟负载平滑,listview,scroll,bitmapimage,bitmapfactory,lazylist,Listview,Scroll,Bitmapimage,Bitmapfactory,Lazylist,我有一个显示产品的列表视图。 每个产品都有产品详细信息 还有图像视图,但我的问题是 对的图像进行延迟加载 产品。而且图像很高 决议滚动时,它会变为 粘乎乎的(不光滑的)。因为下载 形象需要时间。和facebook一样 他们的图片,但滚动是非常重要的 更平滑的任何解决方案请帮助 public class BitmapCacheManager { private static LruCache<Object, Bitmap> cache = null; private f

我有一个显示产品的列表视图。 每个产品都有产品详细信息 还有图像视图,但我的问题是 对的图像进行延迟加载 产品。而且图像很高 决议滚动时,它会变为 粘乎乎的(不光滑的)。因为下载 形象需要时间。和facebook一样 他们的图片,但滚动是非常重要的 更平滑的任何解决方案请帮助

public class BitmapCacheManager {
    private static LruCache<Object, Bitmap> 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<Object, Bitmap>(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<Object, Void, Bitmap> {
        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);
        }
    }

}
  • 将虚拟图像添加到所有占位符中,以便滚动顺畅
  • 使用异步任务根据需要获取图像,并在图像准备好后用正确的图像替换虚拟图像
  • 使用正确的命名约定缓存图像,并根据图像大小正确选择缓存大小

  • 使用LruCache并解码具有首选大小的位图
    bitmapCacheManager.loadBitmap(somekey, someImageView);