Attempting to calculate width of Map Overlays on the fly
I am working on an Android app that utilizes the Google Maps API MapView, MapController, MapActivity, and ItemizedOverlay. I am basically trying to recreate certain functionalities of the Maps app (damn Google for not providing speech bubbles—for lack of a开发者_如何学JAVA better name—for items!), particularly those speech bubbles.
I have an invisible XML structure for the speech bubble in the XML layout file containing my MapView. The first time I show a speech bubble I grab that XML and remove it from it's current parent, applying some ItemizedOverlay.LayoutParams to it, and add it to the MapView as an Overlay. I position it above the item that was selected, fill it with the proper text, then set it to visible.
This all works great. The goal here, though, is to also automatically animate the map to reveal any parts of a speech bubble that may be off-screen when it opens. So I'm trying popup.getWidth() (popup is the instance of my LinearLayout that is the speech bubble) after I do all the manipulation to the bubble, even after I display it to the user. Problem is, popup.getWidth() is returning me the width of the previously displayed popup, not the currently displayed one. I can't figure out why this would be happening if I'm fetching the width after I set it to visible with its new dimensions (which, by the way, are relative when I'm setting them with LayoutParams: fill_content for both width and height).. I have even tried forcing both the MapView and the "popup" to invalidate() before trying to fetch the width.
Any ideas why this may be happening? How can I force the View to settle into its new dimensions before trying to fetch them?
Thanks! Nick
Calls like addView()
and invalidate()
do not happen immediately. You can think of it as if those calls create messages that go on a message queue. When you return from whatever callback you're in (e.g., onTap()
), the main application thread goes back to its normal role of popping messages off that queue and processing them. Hence, the changes you are making will not take immediate effect, but only after you return.
What you can try doing is putting your logic that needs the width in a Runnable
and post()
-ing that Runnable
. That will put it in the queue, presumably after your addView()
and related stuff. At that point, when that Runnable
is run, you should be able to get the correct width.
精彩评论