对android中的Overlay draw的理解

来源:互联网 发布:linux设置权限 编辑:程序博客网 时间:2024/05/21 11:33

今天在修改同学的一个程序,真是让我抓狂,重载的draw只在程序启动的时候调用了,本来依照自己的经验,应该是可以很快定位的,可是调了半天,就是开始没感觉了。开始以为,直接new 一个Overlay就会执行的,不过这个想法很快被自己否决了。真的怪自己思路不够严谨,然后之前对draw的机制不是很清楚。然后就先找出api研究下

public void draw(android.graphics.Canvas canvas,                 MapView mapView,                 boolean shadow)Draw the overlay over the map. This will be called on all active overlays with shadow=true, to lay down the shadow layer, and then again on all overlays with shadow=false. By default, draws nothing.Parameters:canvas - The Canvas upon which to draw. Note that this may already have a transformation applied, so be sure to leave it the way you found it.mapView - the MapView that requested the draw. Use MapView.getProjection() to convert between on-screen pixels and latitude/longitude pairs.shadow - If true, draw the shadow layer. If false, draw the overlay contents.

这个是api里的说明,这下我注意到了mapView相到必定是当overlay的对象添加到mapview是才执行的。带着这个思路,我很快发现继承的Overlay的确有大问题。他尽然采用了一个自己定义的set方法,目的是给Overlay对象赋值。悲剧啊。不过下面的调试还是有问题的,其实不想也知道了,那就是那个draw只是跑了一遍,很显然这里不是主要问题。下面就是

@Overridepublic void onLocationChanged(Location location) {// TODO Auto-generated method stubSystem.out.println("3");if (null!=location) {pointNum++;Log.e("newpath", "添加00");myPath=new PathOverlay();List<Overlay> pointOverlays=mMap.getOverlays();//.add(myPath);pointOverlays.clear();pointOverlays.add(myPath);addPointFromLac(location);                        mMapOverlay.clear();updateWithNewLocation(location,point);}}

           List<Overlay> mMapOverlay;  //代码中这个是全局变量           mMapOverlay=mMap.getOverlays();//这是一段写在oncreate中的代码
问题就在这两段代码中,这样的结果很显然,在onLocationChanged中,mMap.getOverlays()的数据被无辜的清空了,唉,

下面是正确的代码

@Overridepublic void onLocationChanged(Location location) {// TODO Auto-generated method stubif (null!=location) {pointNum++;myPath=new PathOverlay();List<Overlay> pointOverlays=mMap.getOverlays();//pointOverlays.clear();pointOverlays.add(myPath);addPointFromLac(location);updateWithNewLocation(location,point);}}



onLocationChanged
onLocationChanged

原创粉丝点击