gpt4 book ai didi

android - Google API MapView OutOfMemoryError 问题

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:44 24 4
gpt4 key购买 nike

我在我的 Android 应用程序中使用 Google API MapView,我发现当我四处滚动和缩放 map 时它经常遇到这个错误。它们往往发生得很快(使用 map 20 秒),所以我相信这将是一个问题。下面是我的堆栈跟踪。我正在 HTC Desire 上开发,它有 24MB 内存余量。

我可以做些什么来减少这些错误的发生频率?

java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:574)
at com.google.android.maps.ZoomHelper.createSnapshot(ZoomHelper.java:444)
at com.google.android.maps.ZoomHelper.doZoom(ZoomHelper.java:151)
at com.google.android.maps.ZoomHelper.doZoom(ZoomHelper.java:140)
at com.google.android.maps.MapView.doZoom(MapView.java:1478)
at com.google.android.maps.MapView.doZoom(MapView.java:1487)
at com.google.android.maps.MapView$6.onZoom(MapView.java:1442)
at android.widget.ZoomButtonsController$3.onClick(ZoomButtonsController.java:268)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8817)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)


debug.heap native: allocated 10.63MB of 11.01MB (385.98KB free)
debug.memory: allocated: 17.95MB of 24.00MB (13.50MB free)

这是我的叠加层。我有大约 50 个项目显示 96x96 的 png 图像,具有一定的透明度(文件大小为 6KB)。

public class StationItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private ArrayList<Location> locations = new ArrayList<Location>();


private Context mContext;

public StationItemizedOverlay(Drawable defaultMarker, Context context)
{
super(boundCenterBottom(defaultMarker));
this.mContext = context;
}

/**
* Add a range of locations to this overlay
*
* @param locations The locations to add
*/
public void addRange(List<Location> locations)
{
for (Location l : locations)
{
addOne(l);
}
populate();
}

/**
* Add a location to this overlay
*
* @param location The location
*/
public void add(Location location)
{
addOne(location);
populate();
}

/**
* Remove a location from this overlay
* @param location The location to remove
*/
public void remove(Location location)
{
int position = locations.indexOf(location);

if (position >= 0)
{
mOverlays.remove(position);
locations.remove(position);
populate();
}
}

private void addOne(Location location)
{
locations.add(location);
int lat = location.getMicroLatitude();
int lon = location.getMicroLongitude();
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem item = new OverlayItem(point, location.getRealName(), location.getRealName());
mOverlays.add(item);
}

@Override
protected boolean onTap(int index)
{
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}

@Override
protected OverlayItem createItem(int i)
{
return mOverlays.get(i);
}

@Override
public int size()
{
return mOverlays.size();
}

}

这是我的 MapActivityClass

public class StationFinder extends MapActivity
{
IUIInterface dataInterface = UIInterfaceFactory.getInterface();

MapView mapView;

List<Overlay> mapOverlays;
Drawable drawable;
StationItemizedOverlay trainItemizedOverlay;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ui_station_finder);

mapView = (MapView) findViewById(R.id.stationFinderUI_mapview);
mapView.setBuiltInZoomControls(true);

mapOverlays = mapView.getOverlays();
drawable = getResources().getDrawable(R.drawable.dash_train_btn_default);
trainItemizedOverlay = new StationItemizedOverlay(drawable, this);

myMapController = mapView.getController();
myMapController.setZoom(8); // zoom level selected from google map .com

// center this location on northern ireland
centerLocation(Settings.getNorthernIrelandCenter().getGeoPoint());

mapOverlays.add(trainItemizedOverlay);

// add data to the adapters only whenever the data is loaded
if (!dataInterface.isDataLoaded())
{
// data is not loaded
// so make a listener for the data loaded event
dataLoadedListener = new IEventListener()
{
public void action(Object optionalData)
{
// the event has been triggered
// register this function to run in the UI thread of the application
runOnUiThread(new Runnable()
{
public void run()
{
dataLoaded();
}
});
}
};
// register the listener for data loaded events
dataInterface.registerDataLoaded(dataLoadedListener);
// call the data loaded function now incase the data has since been loaded by
// the application between registering for the event
if (dataInterface.isDataLoaded())
{
dataLoaded();
}
}
else
{
// data already loaded so run immediately
dataLoaded();
}



}

IEventListener dataLoadedListener = null;
boolean isDataLoaded = false;

private void dataLoaded()
{
// make sure this function is only called once
if (isDataLoaded)
return;
isDataLoaded = true;

// IMPORTANT STUFF GOES HERE



trainItemizedOverlay.addRange(dataInterface.getAllLocations());


// clean up the data load listener
if (dataLoadedListener != null)
{
dataInterface.removeDataLoaded(dataLoadedListener);
dataLoadedListener = null;
}
}

private MapController myMapController;

private void centerLocation(GeoPoint center)
{
myMapController.animateTo(center);

myMapController.setCenter(center);
};


@Override
protected boolean isRouteDisplayed()
{
return false;
}

最佳答案

奇怪的是这个问题已经停止了。目前不确定为什么,但我最好的猜测是我所做的其他一些内存优化使更大的内存块可用于 map 绘制

关于android - Google API MapView OutOfMemoryError 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5066102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com