/[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 275 - (hide annotations) (download)
Mon Aug 24 18:30:30 2009 UTC (14 years, 9 months ago) by torben
Original Path: android/FriendRadar/src/dk/thoerup/droidradar/RadarView.java
File size: 7284 byte(s)
Rename to droidradar
1 torben 275 package dk.thoerup.droidradar;
2 torben 264
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 torben 268 public class RadarView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
17    
18 torben 264 int angle = 0;
19     Paint p = new Paint();
20     Paint p2 = new Paint();
21     Paint p3 = new Paint();
22    
23     SurfaceHolder surfaceHolder;
24     int distanceBase = 500;
25    
26     String distanceText1;
27     String distanceText2;
28     String distanceText3;
29     String distanceText4;
30    
31     float textWidth1;
32     float textWidth2;
33     float textWidth3;
34     float textWidth4;
35    
36     String gpsText = "-";
37     Location currentLocation;
38    
39     int heading;
40     String headingText = "-";
41    
42 torben 267
43     int width;
44     int height;
45     int hcenter;
46     int vcenter;
47 torben 264
48    
49     boolean mRun = true;
50    
51     Thread t;
52    
53     public RadarView(Context context) {
54     super(context);
55     init();
56     }
57    
58    
59     public RadarView(Context context, AttributeSet attr) {
60     super(context,attr);
61     init();
62     }
63    
64     public RadarView(Context context, AttributeSet attr, int defstyle) {
65     super(context,attr, defstyle);
66     init();
67     }
68    
69    
70     void init() {
71     p.setColor(0xaa00aa00);//Dark green
72     p.setStrokeWidth(2);
73     p.setStyle(Paint.Style.STROKE);
74     p.setAntiAlias(true);
75     p.setTextSize(1);
76    
77     p2.setStrokeWidth(2);
78     p2.setColor(0xff00cc00);//Dark green
79     p2.setStyle(Paint.Style.STROKE);
80     //p2.setAntiAlias(true);
81    
82    
83     p3.setStrokeWidth(2);
84     p3.setColor(0xff00cc00);//Dark green
85     p3.setStyle(Paint.Style.FILL);
86    
87     surfaceHolder = this.getHolder();
88     surfaceHolder.addCallback(this);
89    
90     t = new Thread(this);
91    
92     updateDistanceTexts();
93     }
94    
95     protected void myDraw(Canvas canvas) {
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 torben 269 canvas.drawCircle(hcenter, vcenter, 40, p);
101     canvas.drawCircle(hcenter, vcenter, 80, p);
102     canvas.drawCircle(hcenter, vcenter, 115, p);
103     canvas.drawCircle(hcenter, vcenter, 150, p);
104 torben 264
105 torben 269 canvas.drawText(distanceText1, (width-textWidth1)/2, vcenter+40+1, p3);
106     canvas.drawText(distanceText2, (width-textWidth2)/2, vcenter+80+1, p3);
107     canvas.drawText(distanceText3, (width-textWidth3)/2, vcenter+115+1, p3);
108     canvas.drawText(distanceText4, (width-textWidth4)/2, vcenter+150+1, p3);
109 torben 264
110 torben 268
111 torben 264 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    
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 torben 267
154     width = this.getWidth();
155     height = this.getHeight();
156     hcenter = width / 2;
157     vcenter = hcenter + 20;
158    
159 torben 264 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 torben 265 if (meters == 1500) //yuck, special cases !!!
242     return "1.5 km";
243    
244 torben 264 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 torben 268
296 torben 264
297     //vertCathesis *= -1.0;
298    
299     vertCathesis += vcenter;
300     horzCathesis += hcenter;
301    
302     canvas.drawCircle( (float)horzCathesis, (float)vertCathesis, 3, p3);
303    
304     }
305     }
306    
307     }

  ViewVC Help
Powered by ViewVC 1.1.20