简体   繁体   English

Android ListView位图优化

[英]Android ListView Bitmap Optimization

I have a ListView that display Bitmap pictures. 我有一个ListView显示位图图片。 These Bitmaps get to be quite large. 这些位图会变得很大。 When i scroll on the ListView it seems to be very heavy. 当我在ListView上滚动时,它似乎很沉重。 What techniques can i use to optimize the ListView ? 我可以使用哪些技术来优化ListView? This may cover compressing the Bitmap in memory, or ways to enhance the List View memory management ? 这可能涉及压缩内存中的位图,还是增强列表视图内存管理的方法?

First, read this, 首先,请阅读

http://developer.android.com/training/improving-layouts/smooth-scrolling.html http://developer.android.com/training/improving-layouts/smooth-scrolling.html

It talks about the view holder pattern, and loading images in a threads. 它讨论了视图持有者模式,以及如何在线程中加载图像。 also, read this, 另外,读这篇

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

This talks about how to build an efficient memory cache for bitmaps. 这里讨论如何为位图建立有效的内存缓存。

If that's not enough, another technique you can employ is to avoid loading images until scrolling stops. 如果这还不够,您可以采用的另一种方法是避免在滚动停止之前加载图像。 This prevents the listview from loading all of the images if the user say flings to the bottom of the list. 如果用户说跳到列表的底部,这可以防止listview加载所有图像。 Basically, something like this, 基本上是这样的

    pagerList.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (scrollState != OnScrollListener.SCROLL_STATE_IDLE) {
                return;
            }
            // load images for adapter views between first and first+count.
            // depending on your memory requirements, you can pre-load additional
            // images before first and after first+count to give a better
            // user exp
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            first = firstVisibleItem;
            count = visibleItemCount;
        }
    });

This requires that you keep a handle to the ImageView for each item in your adapter, so you can find it later and set the appropriate bitmap into it. 这要求您为适配器中的每个项目保留一个ImageView的句柄,以便以后可以找到它并在其中设置适当的位图。 This could be as simple as keeping an array of image views in your adapter, where the index == the position in the list view. 这可以像在适配器中保留图像视图数组一样简单,其中索引==列表视图中的位置。

I mostly use LruCache to optimize list and load images from cache LruCache 我主要使用LruCache优化列表并从缓存LruCache加载图像

add this in getView in BaseAdapter class 在BaseAdapter类的getView中添加它

@Override
    public View getView(.....
   ..... . .  
     Bitmap image = getBitmapFromMemCache(name);
        if (image == null)
        {
         image = decodeSampledBitmapFromUri(image_path.get(position), 64,64);
        }
        else
        {
            Log.i("loding. ", "from cache "+name);
        }
        // Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(details), 64, 64);
         holder.img.setImageBitmap(image);
         addBitmapToMemoryCache(name, image);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM