实战教程:运用Java Canvas类实例 - 案例3:绘制功能详解
最编程
2024-07-21 21:38:32
...
import org.mapsforge.core.graphics.Canvas; //导入依赖的package包/类
@Override public synchronized void draw(BoundingBox boundingBox, byte zoomLevel, Canvas canvas, Point topLeftPoint) {
if (DEBUG) { Log.d(TAG, "AlternatingLine.draw"); }
if (getLatLongs().isEmpty()) {
return;
}
Iterator<LatLong> iterator = getLatLongs().iterator();
if (!iterator.hasNext()) {
return;
}
long mapSize = MercatorProjection.getMapSize(zoomLevel, displayModel.getTileSize());
LatLong from = iterator.next();
while (iterator.hasNext()) {
LatLong to = iterator.next();
if (boundingBox.contains(to) || boundingBox.contains(from)) {
Paint paint = getPaintStroke(from, to);
int x1 = (int) (MercatorProjection.longitudeToPixelX(from.longitude, mapSize) - topLeftPoint.x);
int y1 = (int) (MercatorProjection.latitudeToPixelY(from.latitude, mapSize) - topLeftPoint.y);
int x2 = (int) (MercatorProjection.longitudeToPixelX(to.longitude, mapSize) - topLeftPoint.x);
int y2 = (int) (MercatorProjection.latitudeToPixelY(to.latitude, mapSize) - topLeftPoint.y);
canvas.drawLine(x1, y1, x2, y2, paint);
}
from = to;
}
}
上一篇: Java SE基础知识题库及答案详解
下一篇: 玩转Java集合工具箱