欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

实战教程:运用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;
	}
}