加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

对Android中的Overlay draw的认识

发布时间:2021-12-22 13:03:28 所属栏目:PHP教程 来源:互联网
导读:修改同学的一个程序,真是让我抓狂,重载的draw只在程序启动的时候调用了,本来依照自己的经验,应该是可以很快定位的,可是调了半天,就是开始没感觉了。开始以为,直接new 一个Overlay就会执行的,不过这个想法很快被自己否决了。真的怪自己思路不够严谨,
修改同学的一个程序,真是让我抓狂,重载的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只是跑了一遍,很显然这里不是主要问题。下面就是
 
@Override  
public void onLocationChanged(Location location) {  
    // TODO Auto-generated method stub   
  
    System.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()的数据被无辜的清空了,唉,
 
下面是正确的代码
 
@Override  
public void onLocationChanged(Location location) {  
    // TODO Auto-generated method stub  
  
      
    if (null!=location) {  
        pointNum++;  
    myPath=new PathOverlay();  
          
        List<Overlay> pointOverlays=  
            mMap.getOverlays();//  
        pointOverlays.clear();  
        pointOverlays.add(myPath);  
        addPointFromLac(location);  
        updateWithNewLocation(location,point);  
    }     
          
}  
onLocationChanged  
onLocationChanged  

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读