/[projects]/android/FriendRadar/src/dk/thoerup/friendradar/RadarView.java
ViewVC logotype

Contents of /android/FriendRadar/src/dk/thoerup/friendradar/RadarView.java

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20