【问题标题】:Android Bitmaps inside ListView -> out of MemoryListView中的Android位图->内存不足
【发布时间】:2011-09-07 08:49:29
【问题描述】:

我有一个 ListView,其中每个项目最多可以有 10 个图标。如果我上下滚动 ListView,我会强制关闭,说位图大小超过 VM 预算,并且我的内存不足。我在这里阅读了有关回收位图的信息,因此我编写了一个方法,该方法可以回收我的位图。问题是滚动速度很慢。这是我的功能:

 private void recycleBmpsFromConvertView(View convertView){
    if (convertView != null && convertView instanceof LinearLayout) {
        LinearLayout iconArea = (LinearLayout)convertView;
        for (int i = 0; i < iconArea.getChildCount(); i++) {
            View v = iconArea.getChildAt(i);
            if (v instanceof LinearLayout) {
                LinearLayout row = (LinearLayout) v;
                for (int j = 0; j < row.getChildCount(); j++) {
                    View candidate = row.getChildAt(j);
                    if (candidate instanceof ImageView) {
                        ImageView image = (ImageView) candidate;
                        Drawable d = image.getDrawable();
                        Bitmap bmp = null;
                        if (d instanceof SvgDrawable) {
                            SvgDrawable svg = (SvgDrawable) d;
                            bmp = svg.getBitmap();
                        } else if (d instanceof BitmapDrawable) {
                            BitmapDrawable bmpd = (BitmapDrawable) d;
                            bmp = bmpd.getBitmap();
                        }
                        if (bmp != null) {
                            bmp.recycle();
                            bmp = null;
                        }
                    }
                }
            }
        }
    }
    System.gc();
}

只要我的 convertView 不为空,我就会尝试删除位图。我的方法有更快的方法吗?

【问题讨论】:

    标签: android listview bitmap imageview


    【解决方案1】:

    删除回收

    1. 放一个较低分辨率的图标或
    2. 下采样,如果您在设置图像之前对其进行解码。这可以通过在位图解码之前使用 insample 设置来完成。

    【讨论】:

    • 谢谢你的答案。问题是我使用外部 SVG-lib 来创建我的图像。它们也非常小,上面的文字很小,可以阅读。如果我对它们进行下采样,我就无法再阅读上面的文字了。
    • 下采样不会减小图像大小,而是允许以更少的内存消耗进行解码
    猜你喜欢
    • 2014-02-08
    • 2013-05-08
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 2012-07-21
    相关资源
    最近更新 更多