我用googlemap api制作了一个android应用程序,并在覆盖图上绘制了一些16x16 png(大约200计数)。当我在mapview中移动或缩放时,经常会出现“内存不足”错误。Android googlemap内存不足
我也在我的htc中使用了googlemap appication。它使用大约14 + MB内存的接缝,从不发生“内存不足”。
如何在googlemap api中保存内存使用情况,或如何放大android api memmory限制。
非常感谢!
我用googlemap api制作了一个android应用程序,并在覆盖图上绘制了一些16x16 png(大约200计数)。当我在mapview中移动或缩放时,经常会出现“内存不足”错误。Android googlemap内存不足
我也在我的htc中使用了googlemap appication。它使用大约14 + MB内存的接缝,从不发生“内存不足”。
如何在googlemap api中保存内存使用情况,或如何放大android api memmory限制。
非常感谢!
我自己的解决方案: 当缩放/缩小时捕捉OutOfMemoryError,将阻止虚拟机杀死api。 因为在翻译后进行地图缩放时,通常会死亡。
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);
}
}
随着onZoomListener,我已经加了我自己的CustomMapView延伸的MapView。这解决了我的应用程序中的内存异常。
查看我对同一问题的另一个问题的回答:[android mapview总是在嵌套元素中导致OutOfMemoryError](http://stackoverflow.com/questions/5460650/android-mapview-always-causes-an-outofmemoryerror- in-nested-elements/6134375#6134375)[Google Groups:Issue 2181:使用MapView时系统内存泄漏](http://code.google.com/p/android/issues/detail?id=2181) – Muzikant 2011-05-29 04:48:49