1 | package org.mapsforge.android.maps;
|
---|
2 |
|
---|
3 | import org.mapsforge.android.maps.overlay.Overlay;
|
---|
4 | import org.mapsforge.core.GeoPoint;
|
---|
5 |
|
---|
6 | import android.graphics.Canvas;
|
---|
7 | import android.graphics.Paint;
|
---|
8 | import android.graphics.Path;
|
---|
9 | import android.graphics.Point;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * RouteOverlayResetable is a special Overlay to display a sequence of way nodes. To draw an arbitrary
|
---|
13 | * polygon, add a way node sequence where the first and the last way node are equal.
|
---|
14 | * <p>
|
---|
15 | * All rendering parameters like color, stroke width, pattern and transparency can be configured
|
---|
16 | * via the two {@link android.graphics.Paint Paint} objects in the
|
---|
17 | * {@link #RouteOverlay(Paint,Paint)} constructor.
|
---|
18 | */
|
---|
19 |
|
---|
20 | public class RouteOverlayResetable extends Overlay {
|
---|
21 |
|
---|
22 | private static final String THREAD_NAME = "RouteOverlayResetable";
|
---|
23 |
|
---|
24 | private Point[] cachedWayPositions;
|
---|
25 | private byte cachedZoomLevel;
|
---|
26 | private Paint fillPaint;
|
---|
27 | private Paint outlinePaint;
|
---|
28 | private final Path path;
|
---|
29 | private GeoPoint[] wayNodes;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Constructs a new RouteOverlay.
|
---|
33 | *
|
---|
34 | * @param fillPaint
|
---|
35 | * the paint object which will be used to fill the overlay.
|
---|
36 | * @param outlinePaint
|
---|
37 | * the paint object which will be used to draw the outline of the overlay.
|
---|
38 | */
|
---|
39 | public RouteOverlayResetable(Paint fillPaint, Paint outlinePaint) {
|
---|
40 | this.path = new Path();
|
---|
41 | this.cachedWayPositions = new Point[0];
|
---|
42 | setPaint(fillPaint, outlinePaint);
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | public void reset() {
|
---|
47 | this.path.reset();
|
---|
48 | this.cachedWayPositions = new Point[0];
|
---|
49 | this.wayNodes = new GeoPoint[0];
|
---|
50 | super.requestRedraw();
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Sets the paint objects which will be used to draw the overlay.
|
---|
56 | *
|
---|
57 | * @param fillPaint
|
---|
58 | * the paint object which will be used to fill the overlay.
|
---|
59 | * @param outlinePaint
|
---|
60 | * the paint object which will be used to draw the outline of the overlay.
|
---|
61 | */
|
---|
62 | public synchronized void setPaint(Paint fillPaint, Paint outlinePaint) {
|
---|
63 | this.fillPaint = fillPaint;
|
---|
64 | this.outlinePaint = outlinePaint;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Sets the way nodes of the route.
|
---|
69 | *
|
---|
70 | * @param wayNodes
|
---|
71 | * the geographical coordinates of the way nodes.
|
---|
72 | */
|
---|
73 | public synchronized void setRouteData(GeoPoint[] wayNodes) {
|
---|
74 | this.wayNodes = wayNodes;
|
---|
75 | if (this.wayNodes != null && this.wayNodes.length != this.cachedWayPositions.length) {
|
---|
76 | this.cachedWayPositions = new Point[this.wayNodes.length];
|
---|
77 | }
|
---|
78 | this.cachedZoomLevel = Byte.MIN_VALUE;
|
---|
79 | super.requestRedraw();
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | protected synchronized void drawOverlayBitmap(Canvas canvas, Point drawPosition,
|
---|
84 | Projection projection, byte drawZoomLevel) {
|
---|
85 | if (this.wayNodes == null || this.wayNodes.length < 1) {
|
---|
86 | // no way nodes to draw
|
---|
87 | return;
|
---|
88 | } else if (this.fillPaint == null && this.outlinePaint == null) {
|
---|
89 | // no paint to draw
|
---|
90 | return;
|
---|
91 | }
|
---|
92 |
|
---|
93 | // make sure that the cached way node positions are valid
|
---|
94 | if (drawZoomLevel != this.cachedZoomLevel) {
|
---|
95 | for (int i = 0; i < this.cachedWayPositions.length; ++i) {
|
---|
96 | this.cachedWayPositions[i] = projection.toPoint(this.wayNodes[i],
|
---|
97 | this.cachedWayPositions[i], drawZoomLevel);
|
---|
98 | }
|
---|
99 | this.cachedZoomLevel = drawZoomLevel;
|
---|
100 | }
|
---|
101 |
|
---|
102 | // assemble the path
|
---|
103 | this.path.reset();
|
---|
104 | this.path.moveTo(this.cachedWayPositions[0].x - drawPosition.x,
|
---|
105 | this.cachedWayPositions[0].y - drawPosition.y);
|
---|
106 | for (int i = 1; i < this.cachedWayPositions.length; ++i) {
|
---|
107 | this.path.lineTo(this.cachedWayPositions[i].x - drawPosition.x,
|
---|
108 | this.cachedWayPositions[i].y - drawPosition.y);
|
---|
109 | }
|
---|
110 |
|
---|
111 | // draw the path on the canvas
|
---|
112 | if (this.fillPaint != null) {
|
---|
113 | canvas.drawPath(this.path, this.fillPaint);
|
---|
114 | }
|
---|
115 | if (this.outlinePaint != null) {
|
---|
116 | canvas.drawPath(this.path, this.outlinePaint);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | protected String getThreadName() {
|
---|
122 | return THREAD_NAME;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|