我的应用中有一个mapview,每当用户做太多放大或者mapview崩溃时。以下是我在崩溃发生时从DDMS获得的数据分配:
486 17971 byte[] 1 com.google.common.io.android.AndroidPersistentStore readBlock
422 17966 byte[] 1 com.google.common.io.android.AndroidPersistentStore readBlock
349 17963 byte[] 1 com.google.common.io.android.AndroidPersistentStore readBlock
244 17958 byte[] 1 com.google.common.io.android.AndroidPersistentStore readBlock
172 17935 byte[] 1 com.google.common.io.android.AndroidPersistentStore readBlock
102 17886 byte[] 1 com.google.common.io.android.AndroidPersistentStore readBlock
96 9526 byte[] 1 com.google.googlenav.map.MapTile read
64 9526 byte[] 1 com.google.googlenav.map.MapTile read
481 9268 byte[] 1 com.google.googlenav.map.MapTile read
462 9268 byte[] 1 com.google.googlenav.map.MapTile read
345 9058 byte[] 1 com.google.googlenav.map.MapTile read
289 9058 byte[] 1 com.google.googlenav.map.MapTile read
418 8994 byte[] 1 com.google.googlenav.map.MapTile read
400 8994 byte[] 1 com.google.googlenav.map.MapTile read
236 8808 byte[] 1 com.google.googlenav.map.MapTile read
164 8421 byte[] 1 com.google.googlenav.map.MapTile read
92 8351 byte[] 1 com.google.googlenav.map.MapTile read
479 8076 byte[] 1 com.google.googlenav.map.MapTile read
416 7779 byte[] 1 com.google.googlenav.map.MapTile read
240 7682 byte[] 1 com.google.googlenav.map.MapTile read
219 7682 byte[] 1 com.google.googlenav.map.MapTile read
166 7468 byte[] 1 com.google.googlenav.map.MapTile read
147 7468 byte[] 1 com.google.googlenav.map.MapTile read
342 7415 byte[] 1 com.google.googlenav.map.MapTile read
15 4920 long[] 22 com.google.googlenav.map.MapService
我只有一个叠加层,即自定义叠加层
公共类ProfilesOverlay扩展了Overlay {
private static final String TAG="ProfilesOverlay";
private Paint paintCircle;
private Paint paintText;
private Bitmap heartIcon;
boolean multitouchGesture;
boolean showCounts;
public interface ProfilesOverlayDelegate {
public List<OverlayInfo> getOverlayInfo();
public void onOverlayTouched();
public boolean onOverlayTapped(GeoPoint p);
}
ProfilesOverlayDelegate delegate;
public ProfilesOverlay(ProfilesOverlayDelegate delegate, Resources res, boolean showCounts) {
this.delegate = delegate;
this.showCounts = showCounts;
paintCircle = new Paint();
paintCircle.setColor(res.getColor(R.color.mapProfileClusterCircle));
paintCircle.setAntiAlias(true);
heartIcon = BitmapFactory.decodeResource(res, R.drawable.flirtplace_white);
paintText = new Paint();
paintText.setColor(Color.WHITE);
paintText.setTypeface(Typeface.DEFAULT_BOLD);
paintText.setTextSize((float)(heartIcon.getHeight() * 0.7));
paintText.setAntiAlias(true);
}
@Override
public void draw(Canvas canvas, MapView map, boolean shadow) {
Projection projection = map.getProjection();
Point point = new Point();
int heartWidth = heartIcon.getWidth();
int heartHeight = heartIcon.getHeight();
for (OverlayInfo overlay : delegate.getOverlayInfo()) {
projection.toPixels(overlay.getGeoPoint(), point);
if(isLocationVisible(point,map)){
paintCircle.setAlpha(overlay.getAlphs());
canvas.drawCircle(point.x, point.y, (int) overlay.getRadius(), paintCircle);
canvas.drawBitmap(heartIcon, point.x - heartWidth/2, point.y - heartHeight / 2, null);
if(showCounts) {
canvas.drawText(Integer.toString(overlay.getCount()), point.x + heartWidth - App.dp(6), point.y + App.dp(4), paintText);
}
}
}
super.draw(canvas, map, shadow);
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView map) {
multitouchGesture = event.getPointerCount()>1;
delegate.onOverlayTouched();
return super.onTouchEvent(event, map);
}
@Override
public boolean onTap(GeoPoint p, MapView map) {
if (multitouchGesture) {
return false; //
}
return delegate.onOverlayTapped(p);
}
//check if given location is visible on the screen
private boolean isLocationVisible(Point point, MapView mapView)
{
Rect currentMapBoundsRect = new Rect();
mapView.getDrawingRect(currentMapBoundsRect);
return currentMapBoundsRect.contains(point.x, point.y);
}
}
我在我的活动中使用它:
map = (MapView) findViewById(R.id.map);
map.getOverlays().add(new ProfilesOverlay(this, activity.getResources(), true));
Stack Trace非常常见:
06-13 17:27:49.479: ERROR/dalvikvm-heap(20267): 584640-byte external allocation too large for this process.
06-13 17:27:49.479: ERROR/dalvikvm(20267): Out of memory: Heap Size=10339KB, Allocated=9989KB, Bitmap Size=14057KB, Limit=21884KB
06-13 17:27:49.479: ERROR/dalvikvm(20267): Trim info: Footprint=11335KB, Allowed Footprint=11335KB, Trimmed=996KB
06-13 17:27:49.479: ERROR/GraphicsJNI(20267): VM won't let us allocate 584640 bytes
06-13 17:27:49.479: WARN/System.err(20267): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-13 17:27:49.479: WARN/System.err(20267): at android.graphics.Bitmap.nativeCreate(Native Method)
06-13 17:27:49.479: WARN/System.err(20267): at android.graphics.Bitmap.createBitmap(Bitmap.java:677)
06-13 17:27:49.479: WARN/System.err(20267): at com.google.android.maps.ZoomHelper.createSnapshot(ZoomHelper.java:444)
06-13 17:27:49.479: WARN/System.err(20267): at com.google.android.maps.ZoomHelper.beginZoom(ZoomHelper.java:194)
06-13 17:27:49.479: WARN/System.err(20267): at com.google.android.maps.MapView$2.onScaleBegin(MapView.java:371)
06-13 17:27:49.489: WARN/System.err(20267): at android.view.ScaleGestureDetector.onTouchEvent(ScaleGestureDetector.java:248)
06-13 17:27:49.489: WARN/System.err(20267): at com.google.android.maps.MapView.onTouchEvent(MapView.java:646)
06-13 17:27:49.489: WARN/System.err(20267): at android.view.View.dispatchTouchEvent(View.java:3778)
06-13 17:27:49.489: WARN/System.err(20267): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:919)
06-13 17:27:49.489: WARN/System.err(20267): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
06-13 17:27:49.489: WARN/System.err(20267): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
06-13 17:27:49.489: WARN/System.err(20267): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
06-13 17:27:49.489: WARN/System.err(20267): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
06-13 17:27:49.489: WARN/System.err(20267): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
06-13 17:27:49.489: WARN/System.err(20267): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
06-13 17:27:49.489: WARN/System.err(20267): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
06-13 17:27:49.489: WARN/System.err(20267): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
06-13 17:27:49.489: WARN/System.err(20267): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
06-13 17:27:49.489: WARN/System.err(20267): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:958)
06-13 17:27:49.489: WARN/System.err(20267): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1716)
06-13 17:27:49.489: WARN/System.err(20267): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1124)
06-13 17:27:49.489: WARN/System.err(20267): at android.app.Activity.dispatchTouchEvent(Activity.java:2125)
06-13 17:27:49.499: WARN/System.err(20267): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1700)
06-13 17:27:49.499: WARN/System.err(20267): at android.view.ViewRoot.handleMessage(ViewRoot.java:1802)
06-13 17:27:49.499: WARN/System.err(20267): at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 17:27:49.499: WARN/System.err(20267): at android.os.Looper.loop(Looper.java:143)
06-13 17:27:49.499: WARN/System.err(20267): at android.app.ActivityThread.main(ActivityThread.java:5068)
06-13 17:27:49.499: WARN/System.err(20267): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 17:27:49.499: WARN/System.err(20267): at java.lang.reflect.Method.invoke(Method.java:521)
06-13 17:27:49.499: WARN/System.err(20267): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-13 17:27:49.499: WARN/System.err(20267): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-13 17:27:49.499: WARN/System.err(20267): at dalvik.system.NativeStart.main(Native Method)