0

I have a MapView, with various markers on it. On entering the MapActivity the first time, I set the bounds of drawables set on the markers, and everything appears fine. The markers and shadows all appear correctly. But when I click over to another activity, and return, the shadows, or the markers are no longer bound correctly. Sometimes its only some of the markers that are affected. Can anyone tell me what is going on here? Here is some code. Below is where I set the bounds on the marker drawable:

InputStream is = assetManager.open(imageName);
drawable = Drawable.createFromStream(is, null);
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
drawable.setBounds(-width / 2, -height, width - (width / 2), 0);

And here is how I create/add the overlay item to the map:

GeoPoint point = createGeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, character.get_id(), "");
Drawable image = drawable;
overlayItem.setMarker(image);
itemizedOverlay.addOverlay(overlayItem);

From what I can tell, the overlays are not being re-added to the map, this is simply a redraw after an onResume() event. Any ideas?

Update: I've actually figured out, only images of the type tapped move, and its not the shadow, but the image that moves. So, of there are x images of type A, and y images of type B, and I tap on any image of type A, all images of type A are misaligned with their shadows when I return to the MapActivity, while all images of type B are still properly aligned.

Another update: I am outputting the bounds to the log, and see that the bounds are changing after returning to the map screen. When the marker is initially added, its bounds are:

[Rect(-25, -48 - 25, 1)]

But when returning to the map screen, the bounds have changed to:

[Rect(0, 0 - 50, 49)]

...why would this be?

Yet another update: I have a workaround... but I would still like an answer as to why this is happening. The workaround is to override the draw method, and set the bounds (to center bottom) whenever the draw method is called. Here is the code:

@Override
public void draw(Canvas canvas) {
  int dWidth = getIntrinsicWidth();
  int dHeight = getIntrinsicHeight();
  setBounds(-dWidth / 2, -dHeight, dWidth / 2, 0);
  super.draw(canvas);
}
broadbear
  • 614
  • 8
  • 19
  • I believe the problem is not on the code above and it will be difficult to help you without more information or more code. Probably the issue is on your onTap method if the problem only appears after you touch an item. Are you changing the drawable when touched (i.e. from normal to selected or focoused state)? – Luis Sep 24 '12 at 13:15
  • I'm not doing much in the onTap method apart from opening a new activity. I commented out the code (and various parts of it individually) in onTap(), and do not see the error unless the new activity is started. I have traced when the bounds change, and it is after leaving the newly started activity and returning to the map screen. I'm guessing a redraw happens when returning to the map screen, and onBoundsChange() is called on the tapped drawable(s). – broadbear Sep 24 '12 at 16:45
  • Did you check you onResume method. After returning from the activity, you don't need to readd the markers (they should remain in memory). Are you doing any thing there that could mess the bounds? For your info, I've done several applications that call activitys from a mapview and none is showing this issue. – Luis Sep 25 '12 at 10:39
  • I thought about that, but I am not overriding the onResume method. This is actually a duplicate of another post (which I am having trouble finding right now). He actually posted screenshots demonstrating the exact behavior I am seeing. There was no resolution there either. I am not doing anything explicitly on return. There is no re-add of the markers. Something is changing the bounds internally, before the map is re-drawn, and I can't figure out what. – broadbear Sep 29 '12 at 17:01
  • One more hint ... you are not showing how you define the variable drawable, but if it's static you could be changing it somewhere else, even in other activity. – Luis Sep 29 '12 at 22:19

1 Answers1

0

I found out what the problem was. You can see from the code in the question, I am reading in BitmapDrawables from an InputStream. To reduce waste, I put all images created in a flyweight and reuse them when I can. That means, when I put 3 'sunshines' on the map, all three are based on the same image. In addition, when I place them on the map via an ItemizedOverlay, I am calling boundCenterBottom() so the shadow will display in the correct place.

When I open a different Activity, that Activity has an ImageView that I was populating with the same image. Turns out, that ImageView must have been resetting the bounds. I know this because, after overriding the draw() method, and setting the bounds on every draw, I was no longer able to see the images in the ImageViews. When I comment out the overridden draw() method, I am able to see the images in the ImageViews, but the images are shifted when I return to the map. This tells me that adding the image to the ImageView must be updating the bounds.

My solution is to create two distinct images for each image type; one for the map, and one for the ImageViews on the other Activities. I can still put them in the flyweight, but they have separate keys so multiple image instances can exist for the same image.

Hope this helps someone. I know there was another question of StackOverflow about this, but I can't find it to link to or to answer.

broadbear
  • 614
  • 8
  • 19