Overlay 添加动态效果
public class MyLocationOverlay extends Overlay {
privateCanvascanvas;
privatefinalHandlerhandler=newHandler();
privateRunnablerunnable;
privateGeoPointgeoPoint;
privateintxMarker,yMarker;
privateintmarkerNumber=0;
privateBitmapmarker,marker0,marker1,marker2,marker3;
publicMyLocationOverlay(Contextcontext){
Resourcesr=context.getResources();=BitmapFactory.decodeResource(r,R.drawable.marker);
marker1=BitmapFactory.decodeResource(r,R.drawable.marker1);
marker2=BitmapFactory.decodeResource(r,R.drawable.marker2);
marker3=BitmapFactory.decodeResource(r,R.drawable.marker3);
marker=marker0;
}
/**设置位置*/
publicvoidsetLocation(GeoPointgeoPoint){
this.geoPoint=geoPoint;
}
/**设置标记是动态还是静态*/
publicvoidanimateMarker(booleanon){
if(on){//activateanimation
if(runnable==null)runnable=newRunnable(){
publicvoidrun(){
if(canvas!=null){switch(markerNumber){
case0:marker=marker0;break;
case1:marker=marker1;break;
case2:marker=marker2;break;
case3:marker=marker3;break;
}
markerNumber=++markerNumber%4;
canvas.drawBitmap(marker,xMarker,yMarker,null);
mapView.invalidate(xMarker,yMarker,
xMarker+wMarker,yMarker+hMarker);
}
handler.postDelayed(this,500);
}
};
handler.postDelayed(runnable,500);
} else { // 静态marker0
handler.removeCallbacks(runnable);
runnable=null;
marker=marker0;
}
}
@Override
publicvoiddraw(Canvascanvas,MapViewmapView,booleanshadow){
if(shadow==false){
Projectionprojection=mapView.getProjection();
//转换坐标到屏幕像素
Pointpoint=projection.toPixels(geoPoint,null);
//记录canvas和标记位置
this.canvas=canvas;
xMarker=point.x-marker.getWidth()/2;
yMarker=point.y-marker.getHeight()/2;
//绘画标记在位置的中心
canvas.drawBitmap(marker,xMarker,yMarker,null);
}
super.draw(canvas,mapView,shadow);
}
@Override
publicbooleanonTap(GeoPointpoint,MapViewmapView){returnfalse;}
}
然后在
MapActivity.onResume() 中调用locationOverlay = MyLocationOverlay(this)
GeoPoint point = new GeoPoint(latE6, lonE6);
locationOverlay.setLocation(point);
locationOverlay.animateMarker(true);
mapView.getController().animateTo(point);