Android googlemap Out of memory
I made an android app with googlemap api and drew roughly 16x16 png (about 200 count) on overlay. When I move or zoom in to / in the mapview, the out of memory error occurs very often.
I have also used googlemap app in my htc itself. The stitches that he uses are about 14 + MB memmory, and are never "out of memory".
How to keep memmory usage in googlemap api or how to increase android api memmory limit.
Thank you very much!
+2
a source to share
2 answers
My own solution: Raising an OutOfMemoryError when scaling / scaling will prevent the api from being destroyed by the VM. Because it usually dies when doing map scaling after 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;
}
+2
a source to share
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 the onZoomListener, I added my own CustomMapView that extends the MapView. This solved the memory exception in my application.
0
a source to share