开发者

Android googlemap Out of memory

I made an android application with googlemap api, and draw some 16x16 png (about 200 count) on overlay. When I move or zoom on/in mapview, "out of memory" error occurs very often.

I also 开发者_运维百科used the googlemap appication in my htc itself. Seams that it uses about 14+MB memmory, and never happens "out of memory".

How to save memmory usage in a googlemap api, or how to enlarge android api memmory limit.

Thanks a lot!


My own solution: Catching OutOfMemoryError when zoomin/zoomout, will prevent api to be killed by VM. Because it dies usually when doing map zoom after a translation.

    mapView.setBuiltInZoomControls(true);  
    ZoomButtonsController zoomctrl = mapView.getZoomButtonsController(); 
    zoomctrl.setAutoDismissed(false);//自动隐藏关闭
    zoomctrl.setVisible(true);
    zoomctrl.setOnZoomListener(new ZoomButtonsController.OnZoomListener() {

        public void onZoom(boolean zoomIn) {
            // TODO Auto-generated method stub
            try{
                Log.i(TAG, "OnZoomListener");
                System.gc();
                if(zoomIn)
                {                       
                    mc.zoomIn();
                }
                else
                {
                    mc.zoomOut();
                }
                System.gc();
            }
            catch(OutOfMemoryError e)
            {
                e.printStackTrace();
                Log.e(TAG, e.toString());
                Toast.makeText(GoogleMap.this, e.toString(), Toast.LENGTH_LONG);
            }
            catch (Exception e)
            {
                Log.w(TAG, e.toString());
                Toast.makeText(GoogleMap.this, e.toString(), Toast.LENGTH_LONG);
            }               
        }

        public void onVisibilityChanged(boolean visible) {
            // TODO Auto-generated method stub

        }
    });

    private boolean myDoubleTouch(float x, float y, MapView mapView)
    {
    Log.i(mParent.TAG, "myDoubleTouch: " + x +","+y);
    try
    {
        mapView.getController().zoomInFixing((int)x, (int)y);
    }
    catch(OutOfMemoryError e)
    {
        System.gc();
        e.printStackTrace();
        Log.e(mParent.TAG, e.toString());
        Toast.makeText(m_mapview.getContext(), e.toString(), Toast.LENGTH_LONG);
    }
    catch (Exception e)
    {
        Log.w(mParent.TAG, e.toString());
        Toast.makeText(m_mapview.getContext(), e.toString(), Toast.LENGTH_LONG);
    }           

    return true;
}


public class CustomMapView extends MapView {
    // private Context context;
    public CustomMapView(Context context, String apiKey) {

    super(context, apiKey);
    // this.context = context;
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    try {
        return super.dispatchTouchEvent(ev);

    } catch (Exception ex) {

            //     Log.e("CustomMapView", "Caught the exception");
        int zoomLevel = getZoomLevel();
        getController().setZoom(zoomLevel-1);
        super.setVisibility(View.GONE);
        super.setVisibility(View.VISIBLE);
    }

    return true;
}

@Override
public void draw(Canvas canvas) {
    try {
        super.draw(canvas);
    } catch (Exception e) {

             //     Log.e("CustomMapView", "Caught the exception");
        int zoomLevel = getZoomLevel();
        getController().setZoom(zoomLevel-1);
        super.setVisibility(View.GONE);
        super.setVisibility(View.VISIBLE);
    }
}

public CustomMapView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // this.context = context;
}

public CustomMapView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // this.context = context;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    System.gc();
    return super.onTouchEvent(ev);
}

}

Along with onZoomListener, I had added my own CustomMapView which extends MapView. This solved the memory exception in my application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜