/[projects]/android/DroidRadar/src/dk/thoerup/droidradar/RadarView.java
ViewVC logotype

Annotation of /android/DroidRadar/src/dk/thoerup/droidradar/RadarView.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 281 - (hide annotations) (download)
Tue Aug 25 19:00:58 2009 UTC (14 years, 9 months ago) by torben
File size: 7904 byte(s)
Small fixes to the threading

1 torben 275 package dk.thoerup.droidradar;
2 torben 264
3    
4 torben 277 import java.util.Date;
5     import java.util.List;
6    
7 torben 264 import android.content.Context;
8     import android.graphics.Canvas;
9     import android.graphics.Paint;
10     import android.graphics.RectF;
11     import android.location.Location;
12     import android.util.AttributeSet;
13     import android.util.Log;
14     import android.view.MotionEvent;
15     import android.view.SurfaceHolder;
16     import android.view.SurfaceView;
17    
18     //public class RadarView extends View {
19 torben 277 public class RadarView extends SurfaceView implements SurfaceHolder.Callback, Runnable, DroidLocator.DroidsLocatedListener {
20 torben 268
21 torben 264 int angle = 0;
22     Paint p = new Paint();
23     Paint p2 = new Paint();
24     Paint p3 = new Paint();
25    
26     SurfaceHolder surfaceHolder;
27     int distanceBase = 500;
28    
29     String distanceText1;
30     String distanceText2;
31     String distanceText3;
32     String distanceText4;
33    
34     float textWidth1;
35     float textWidth2;
36     float textWidth3;
37     float textWidth4;
38    
39     String gpsText = "-";
40     Location currentLocation;
41    
42     int heading;
43     String headingText = "-";
44    
45 torben 267
46     int width;
47     int height;
48     int hcenter;
49     int vcenter;
50 torben 264
51    
52     boolean mRun = true;
53    
54 torben 277 List<DroidBean> droidBeans;
55    
56     DroidLocator locator = new DroidLocator();
57    
58     long imei;
59    
60 torben 281 //Thread t;
61 torben 264
62     public RadarView(Context context) {
63     super(context);
64     init();
65     }
66    
67    
68     public RadarView(Context context, AttributeSet attr) {
69     super(context,attr);
70     init();
71     }
72    
73     public RadarView(Context context, AttributeSet attr, int defstyle) {
74     super(context,attr, defstyle);
75     init();
76     }
77    
78    
79     void init() {
80     p.setColor(0xaa00aa00);//Dark green
81     p.setStrokeWidth(2);
82     p.setStyle(Paint.Style.STROKE);
83     p.setAntiAlias(true);
84     p.setTextSize(1);
85    
86     p2.setStrokeWidth(2);
87     p2.setColor(0xff00cc00);//Dark green
88     p2.setStyle(Paint.Style.STROKE);
89     //p2.setAntiAlias(true);
90    
91    
92     p3.setStrokeWidth(2);
93     p3.setColor(0xff00cc00);//Dark green
94     p3.setStyle(Paint.Style.FILL);
95    
96     surfaceHolder = this.getHolder();
97     surfaceHolder.addCallback(this);
98    
99    
100     updateDistanceTexts();
101     }
102    
103     protected void myDraw(Canvas canvas) {
104    
105     canvas.drawColor(0xff000000);
106     canvas.drawLine(hcenter-5, vcenter-5, hcenter+5, vcenter+5, p);
107     canvas.drawLine(hcenter-5, vcenter+5, hcenter+5, vcenter-5, p);
108 torben 269 canvas.drawCircle(hcenter, vcenter, 40, p);
109     canvas.drawCircle(hcenter, vcenter, 80, p);
110     canvas.drawCircle(hcenter, vcenter, 115, p);
111     canvas.drawCircle(hcenter, vcenter, 150, p);
112 torben 264
113 torben 269 canvas.drawText(distanceText1, (width-textWidth1)/2, vcenter+40+1, p3);
114     canvas.drawText(distanceText2, (width-textWidth2)/2, vcenter+80+1, p3);
115     canvas.drawText(distanceText3, (width-textWidth3)/2, vcenter+115+1, p3);
116     canvas.drawText(distanceText4, (width-textWidth4)/2, vcenter+150+1, p3);
117 torben 264
118 torben 268
119 torben 264 final float lineLength = 150;
120     angle -= 6;
121    
122    
123     double base = (Math.sin(angle/360.0)*lineLength) + (float)hcenter;
124     double height = (Math.cos(angle/360.0)*lineLength) + (float)vcenter;
125    
126    
127     canvas.drawLine(hcenter, vcenter, (float)base, (float)height, p2);
128    
129    
130    
131     //left,top,right,bottom
132     canvas.drawRoundRect( new RectF(20,360,180,400), 10, 10, p);
133     canvas.drawLine(100, 360, 100, 400, p);
134    
135     canvas.drawLine(45, 380, 75, 380, p); // Minus
136     canvas.drawLine(125, 380, 155, 380, p); // Plus
137     canvas.drawLine(140, 365, 140, 395, p); // Plus
138    
139     // Gps status
140     canvas.drawText("Gps accuracy", 10, 20,p3);
141     canvas.drawText(gpsText, 10, 40, p3);
142    
143     // Heading
144     canvas.drawText("Heading", 270, 20, p3);
145     canvas.drawText(headingText, 280, 40, p3);
146    
147    
148     drawLocations(canvas);
149     }
150    
151    
152     @Override //surfaceholder.CallBack
153     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
154     Log.i("surfacChaned()","-");
155     }
156    
157    
158     @Override
159     public void surfaceCreated(SurfaceHolder holder) { //surfaceholder.CallBack
160     Log.i("surfacCreated()","-");
161 torben 267
162     width = this.getWidth();
163     height = this.getHeight();
164     hcenter = width / 2;
165     vcenter = hcenter + 20;
166    
167 torben 281 mRun = true;
168     Thread drawThread = new Thread(this);
169    
170     drawThread.start();
171 torben 277 locator.setDroidsLocatedListener(this);
172 torben 281 locator.setContinue(true);
173 torben 277 Thread locatorThread = new Thread(locator);
174     locatorThread.start();
175 torben 264 }
176    
177    
178     @Override
179     public void surfaceDestroyed(SurfaceHolder holder) { //surfaceholder.CallBack
180     Log.i("surfacDestroyed()","-");
181     mRun = false;
182 torben 281 locator.setContinue(false);
183 torben 264 }
184    
185     public void run() {
186     while (mRun) {
187     Canvas c = null;
188     try {
189     c = surfaceHolder.lockCanvas(null);
190     synchronized (surfaceHolder) {
191     //if (mMode == STATE_RUNNING)
192     // updatePhysics();
193     myDraw(c);
194     }
195     } finally {
196     // do this in a finally so that if an exception is thrown
197     // during the above, we don't leave the Surface in an
198     // inconsistent state
199     if (c != null) {
200     surfaceHolder.unlockCanvasAndPost(c);
201     }
202     }
203     try {
204     Thread.sleep(20);
205     } catch (InterruptedException e) {}
206     }
207 torben 281 Log.i("RadarView", "Draw thread exiting");
208 torben 264 }
209    
210    
211    
212     @Override
213     public boolean onTouchEvent(MotionEvent event) {
214     float x = event.getX();
215     float y = event.getY();
216     Log.i("TouchEvent", "Coords: " + x + "," + y );
217     if (x>25 && x<95 && y>360 && y<400) { //zoom out
218     Log.i("Button", "Zoom Out");
219     if (distanceBase < 16000) {
220     distanceBase *= 2;
221     updateDistanceTexts();
222     }
223     }
224    
225     if (x>105 && x<175 && y>360 && y<400) {
226     Log.i("Button", "Zoom In"); // zoom in
227     if (distanceBase > 125) {
228     distanceBase /= 2;
229     updateDistanceTexts();
230     }
231     }
232    
233    
234     return false;
235     }
236    
237     void updateDistanceTexts() {
238     distanceText1 = formatLength(distanceBase);
239     distanceText2 = formatLength(distanceBase*2);
240     distanceText3 = formatLength(distanceBase*3);
241     distanceText4 = formatLength(distanceBase*4);
242    
243     textWidth1 = textWidth(distanceText1);
244     textWidth2 = textWidth(distanceText2);
245     textWidth3 = textWidth(distanceText3);
246     textWidth4 = textWidth(distanceText4);
247    
248     }
249    
250     float textWidth(String text) {
251     float[] width = { 0.0f };
252     p3.breakText(text, true, 200, width);
253     return width[0];
254    
255     }
256    
257     String formatLength(int meters) {
258 torben 265 if (meters == 1500) //yuck, special cases !!!
259     return "1.5 km";
260    
261 torben 264 if (meters>=1000)
262     return "" + (meters/1000) + " km";
263     else
264     return "" + meters + " m";
265     }
266    
267     void setCurrentLocation(Location loc) {
268     currentLocation = loc;
269     gpsText = formatLength( (int)loc.getAccuracy());
270 torben 277
271     locator.setCurrentLocation(loc);
272    
273 torben 264 }
274    
275     void setHeading(int head) {
276     heading = head;
277     headingText = "" + head;
278     }
279    
280 torben 277 public void setImei(long imei) {
281     this.imei = imei;
282     locator.setImei(imei);
283     }
284    
285 torben 264 void drawLocations(Canvas canvas) {
286 torben 277 if (currentLocation != null && droidBeans != null) {
287 torben 264
288 torben 277 for(DroidBean droid : droidBeans) {
289     drawLocation(canvas, droid);
290     }
291 torben 264 }
292     }
293    
294 torben 277 void drawLocation(Canvas canvas, DroidBean droid) {
295 torben 264
296 torben 277 Location location = droid.getLocation();
297    
298 torben 264 float bearing = currentLocation.bearingTo(location);
299     float distance = currentLocation.distanceTo(location);
300    
301     //Log.e("Bearing to", ""+bearing);
302     //Log.e("Distance to", ""+distance);
303    
304     bearing -= (float) heading;
305    
306     if (distance <= (distanceBase*4)) {
307    
308     //convert from degrees to radians
309     double bearingRad = Math.toRadians(bearing-90);
310    
311     double hypotenuse = (distance / (distanceBase*4.0)) * 150.0;
312     double vertCathesis = Math.sin(bearingRad)*hypotenuse;
313     double horzCathesis = Math.cos(bearingRad)*hypotenuse;
314 torben 268
315 torben 264
316     //vertCathesis *= -1.0;
317    
318     vertCathesis += vcenter;
319     horzCathesis += hcenter;
320    
321 torben 277 droid.setGuiX((int)horzCathesis);
322     droid.setGuiY((int)vertCathesis);
323    
324 torben 264 canvas.drawCircle( (float)horzCathesis, (float)vertCathesis, 3, p3);
325    
326     }
327     }
328 torben 277
329    
330     @Override
331     public void onDroidsLocated() { //callback
332     droidBeans = locator.getDroids();
333     Log.i("RadarView", "OnDroidsLocated");
334     }
335 torben 264
336     }

  ViewVC Help
Powered by ViewVC 1.1.20