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

  ViewVC Help
Powered by ViewVC 1.1.20