我使用 googlemap api 制作了一个 android 应用程序,并在叠加层上绘制了一些 16x16 png(约 200 个)。当我在地图视图中移动或缩放时,经常出现“内存不足”错误。
我还在我的 htc 本身中使用了 googlemap 应用程序。它使用大约 14 + MB 内存的接缝,并且永远不会发生“内存不足”。
如何在 googlemap api 中保存内存使用,或如何扩大 android api 内存限制。
非常感谢!
我使用 googlemap api 制作了一个 android 应用程序,并在叠加层上绘制了一些 16x16 png(约 200 个)。当我在地图视图中移动或缩放时,经常出现“内存不足”错误。
我还在我的 htc 本身中使用了 googlemap 应用程序。它使用大约 14 + MB 内存的接缝,并且永远不会发生“内存不足”。
如何在 googlemap api 中保存内存使用,或如何扩大 android api 内存限制。
非常感谢!
我自己的解决方案:在放大/缩小时捕获 OutOfMemoryError,将防止 api 被 VM 杀死。因为它通常在翻译后进行地图缩放时死亡。
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。这解决了我的应用程序中的内存异常。