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

  ViewVC Help
Powered by ViewVC 1.1.20