优化网格布局的内存使用情况 [英] Optimizing GridLayout's memory usage

查看:265
本文介绍了优化网格布局的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的 Android的支持-V7-网格布局

我在调试时间顺利和快速的实现它,直到我测试它在一个庞大的数据规模。实际数据将大约〜700x400 行列的),我只是测试它在 400x100 ,但应用刚刚坠毁并引发 OutOfMemoryException异常。然后,我减少了数据,直到它实际上是在的300x100 运行。这不是落后或者我没有任何CPU性能的问题,唯一的问题是,<一个href=\"http://stackoverflow.com/questions/18675557/what-is-the-maximum-amount-of-ram-an-app-can-use\">inadequate内存。

I implement it smoothly and fast during the debugging times until I test it on a huge data scale. The actual data would be about ~700x400 (row-column) and I just tested it in 400x100 but the application has just crashed and throws an OutOfMemoryException. I then reduced the data until it actually runs on 300x100. It's not lagging or I don't have any CPU performance issue, the only issue is inadequate memory.

这是我怎么加的ImageView ■在网格布局:

This is how I add ImageViews in the grid layout:

public boolean genMap(GridLayout gl) {

    gl.removeAllViews();
    gl.setRowCount( mapFile.getRowCount() );
    gl.setColumnCount( mapFile.getColCount() );

    try {

    for (int row = 0; row < mapFile.getRowCount(); ++row) {
        for (int col = 0; col < mapFile.getColCount(); ++col) {

            com.gridlayout.GridLayout.Spec rowspan = GridLayout.spec(row, 1); 
            com.gridlayout.GridLayout.Spec colspan = GridLayout.spec(col, 1);
            GridLayout.LayoutParams lp = new GridLayout.LayoutParams(rowspan, colspan);
            lp.width = Cell.SIZE;
            lp.height = Cell.SIZE;

            Cell cell = genNewCell( ctx.getApplicationContext(), row, col );

            gl.addView( cell, lp );
        }
    }

        return true;

    } catch (Exception e) {
        return false;
    }

}

其中,细胞是ImageView的子类

Where Cell is a subclass of ImageView

或许,我也觉得那里的唯一可见的视图将被载入了lazyload格局,但是,我想知道,如果网​​格布局已经实现了。

Perhaps, I also think of lazyload pattern where the only visible view will be loaded out but, I'd like to know if GridLayout already implements it.

GridView控件好像是不是我要找的。它不能对角滚动加上它不能有一个滚动条水平和垂直两个同时。我想实现的是类似的GoogleMaps'的ViewGroup布局,其中你可以在一个二维的方式滚动屏幕,每个细胞的内存分配/释放的自动管理。我觉得网​​格布局是我最后的希望,因为我看不到任何的ViewGroup W / C实现自己想做的。

GridView seems is not what I'm looking for. It cannot scroll diagonally plus it can't have a scrollbar both horizontal and vertical at the same time. What I want to achieve is something like GoogleMaps' ViewGroup layout wherein you can scroll in a 2 dimensional way and each cells memory allocation/deallocation are managed automatically. I think GridLayout is my last hope as I cannot see any ViewGroup w/c implements what I wanted to.

所以我的问题是,我该怎么能够循环利用的细胞的不可见但在屏幕上,同时保持版面,因为它们是present?

So my question is, how can I able to recycle those cells that aren't visible yet in the screen while keeping layout as they are present?

推荐答案

网格布局没有做任何的动态内存管理和简单地创建一切,无论其是否实际在屏幕上与否。你需要使用类似的GridView。有了一个GridView,理论上可以支持项目的无限量。现在,因为你想做水平滚动,你将需要自定义GridView控件的实现。像 https://github.com/jess-anders/two-way-gridview

GridLayout does not do any dynamic memory management and simply creates everything regardless if it is actually on the screen or not. You need to use something like the GridView. With a GridView, you could theoretically support an infinite amount of items. Now because you want to do horizontal scrolling you will need a custom GridView implementation. Something like https://github.com/jess-anders/two-way-gridview.

如果你从我的后拿走什么,不要做这跟网格布局。你可以测试所有的一天,但每个电话不同,有的有更多或更少的内存。

If you take away nothing from my post, DO NOT DO THIS WITH A GRID LAYOUT. You could test all day but every phones different and some have more or less memory.

编辑1:

这也可能与Android中L新RecyclerView的释放容易,但我还没有考虑那么远。

This also might be easier with the release of the new RecyclerView in android L, but I haven’t look into it that much.

这篇关于优化网格布局的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆