3

We're trying to write our own MapView, and I'm trying to figure out how adding overlays to the mapview causes them to get drawn in other mapping APIs

I've got a MapView that extends ViewGroup. I think I've figured out that calling MapView.invalidate() causes the mapview's dispatchDraw method to be called. Does that sound correct?

If that's true, when is the onDraw and draw method of the mapview called?

More importantly is all this view and which methods called when stuff documented well anywhere?

Thanks!

EDIT This SO post explained that for classes that extend ViewGroup, the onDraw method is not called automatically. You have to force it if you need it. But as ebarrenchea pointed out, the order is draw, onDraw, dispatchDraw IF all the methods are called

Community
  • 1
  • 1
joshkendrick
  • 3,497
  • 8
  • 38
  • 52

2 Answers2

1

Calling invalidate on your viewgroup will force draw to run which will in turn call onDraw and then dispatchDraw. You should have a look at the view documentation here and the View source code here for more information.

ebarrenechea
  • 3,775
  • 1
  • 31
  • 37
  • yeah ive seen that page, but no where on there does it say "calling invalidate causes ... to happen". so when I call mapView.invalidate, mapview's onDraw is executed, then mapView's draw, then mapView's dispatchDraw, correct? – joshkendrick Feb 22 '13 at 13:00
  • 1
    The documentation there states "If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future." And looking at the View source code states that `onDraw` is called from `draw` and right after `dispatchDraw` will be called as you can see [here](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.0_r1/android/view/View.java#View.draw%28android.graphics.Canvas%29). – ebarrenechea Feb 22 '13 at 15:25
  • hmmm i missed the source code link the first time. ive read that and looked at the documentation, and it makes sense, but it doesn't seem my onDraw method is ever called! I've got a log statement there so I can see when it happens, but it's never written.. Any idea why onDraw wouldn't be called? The view is visible, it's the thing on screen! – joshkendrick Feb 22 '13 at 16:21
  • The source link wasn't there before, I edited the answer a little while ago. There are two possibilities for why your `onDraw` is never called: 1) your `invalidate` call is never triggered; 2) you overrode `draw` (which you really shouldn't) and messed things up. You should probably look at what is triggering your `invalidate` call and make sure it is actually happening. – ebarrenechea Feb 22 '13 at 16:52
0

invalidate() has to be called from UI thread to cause onDraw(). Try to use postInvalidate() which should have the same effect as invalidate(), but works from non UI threads.

Spooky
  • 2,966
  • 8
  • 27
  • 41
ole
  • 31
  • 1