为了减少处理器在地图上重新绘制路线的工作,我使用 Path 类。我想为一个 ZoomLevel 存储一个路径。我将路径保存在 SparseArray 中,其中 key = ZoomLevel,代码如下所示
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if (shadow){
return;
}
else
if(route==null){
return;
}
drawPath(mapView, canvas);
}
private void drawPath(MapView mv, Canvas canvas) {
Point point = new Point();
if (pathMap.get(gMapView.getZoomLevel())==null){
List<GeoPoint> list = null;
Projection p = mv.getProjection();
List<RouteMachine.Section> routeArray = route.getSections();
p.toPixels(routeArray.get(0).getPoints().get(0), point);
Point rememberThisPoint = new Point(point.x,point.y);
Path path = new Path();
path.moveTo(point.x,point.y);
for (RouteMachine.Section section : routeArray) {
list = section.getPoints();
for (int i=1; i < list.size(); i++) {
p.toPixels(list.get(i), point);
path.lineTo(point.x, point.y);
}
}
pathMap.put(gMapView.getZoomLevel(), path, rememberThisPoint);
}
else{
mv.getProjection().toPixels(route.getSections().get(0).getPoints().get(0), point);
pathMap.offset(gMapView.getZoomLevel(),point);
}
canvas.drawPath(pathMap.get(gMapView.getZoomLevel()),mPaint);
}
在不同的缩放级别下,它可以工作或不工作。级别更改,但路径看起来像上一个级别的路径。我认为,这是因为 zoomLevel 在映射 rapaints 之前有时会发生变化。路径计算在这两个动作之间起作用。ZoomLewel 已更改,但地图尚未重新绘制。我怎样才能解决这个问题?