Optimizing GridLayout's memory usage

落花浮王杯 提交于 2020-01-15 17:34:39

问题


I'm simulating a tile-based map in Android using android-support-v7-gridlayout.

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.

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;
    }

}

Where Cell is a subclass of ImageView

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.

Update 1

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.

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?


回答1:


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.

EDIT 1:

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




回答2:


In my opinion you should use Webview for this. Has scroll, zoom, memory issues already addressed.



来源:https://stackoverflow.com/questions/25213984/optimizing-gridlayouts-memory-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!