/[aagtl_public1]/src/com/zoffcc/applications/aagtl/GeocachesView.java
aagtl

Contents of /src/com/zoffcc/applications/aagtl/GeocachesView.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations) (download)
Sun Aug 5 14:00:28 2012 UTC (11 years, 7 months ago) by zoffadmin
File size: 10290 byte(s)
license text correction
1 /**
2 * aagtl Advanced Geocaching Tool for Android
3 * loosely based on agtl by Daniel Fett <fett@danielfett.de>
4 * Copyright (C) 2010 - 2012 Zoff <aagtl@work.zoff.cc>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 package com.zoffcc.applications.aagtl;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import android.content.Context;
27 import android.graphics.Bitmap;
28 import android.graphics.Canvas;
29 import android.graphics.Color;
30 import android.graphics.Paint;
31 import android.graphics.RectF;
32 import android.graphics.Typeface;
33 import android.widget.ImageView;
34
35 public class GeocachesView extends ImageView
36 {
37 int gc_box_small_width = 8;
38 int gc_box_small_height = 8;
39 int gc_box_big_width = 15;
40 int gc_box_big_height = 15;
41
42 int TOO_MUCH_POINTS = 30;
43 int MAX_DRAW_POINTS = 250;
44
45 aagtl main_aagtl;
46
47 Bitmap bitmap_main = null;
48 Canvas image_main = null;
49
50 Boolean double_buffer = false;
51
52 int COLOR_FOUND = Color.parseColor("#bebebe");
53 int COLOR_REGULAR = Color.parseColor("#11a011");
54 int COLOR_MULTI = Color.parseColor("#b0a010");
55 int COLOR_WAYPOINTS = Color.parseColor("#b0a010");
56 int COLOR_DEFAULT = Color.parseColor("#1111ef");
57 int COLOR_CURRENT_CACHE = Color.parseColor("#fe0000");
58 int COLOR_CACHE_CENTER = Color.parseColor("#101010");
59
60 Paint box_paint = new Paint(0);
61 Paint text_paint = new Paint(0);
62
63 List<GeocacheCoordinate> caches_loaded = new ArrayList<GeocacheCoordinate>();
64
65 public GeocachesView(Context context, aagtl main_aagtl)
66 {
67 super(context);
68
69 text_paint.setColor(Color.BLACK);
70 //text_paint.setStyle(Paint.Style.FILL);
71 text_paint.setTextSize(21);
72 text_paint.setStrokeWidth(2);
73 text_paint.setTypeface(Typeface.DEFAULT_BOLD);
74 text_paint.setAntiAlias(true);
75
76 bitmap_main = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
77 image_main = new Canvas(bitmap_main);
78
79 this.main_aagtl = main_aagtl;
80 this.clear_stuff();
81 }
82
83 public void clear_stuff()
84 {
85 // clear caches loaded
86 if (caches_loaded != null)
87 {
88 caches_loaded.clear();
89 }
90 }
91
92 public void set_loaded_caches(List<GeocacheCoordinate> new_list)
93 {
94 this.caches_loaded = new_list;
95 //System.out.println("iigcXX");
96 this.invalidate();
97 }
98
99 public double[] deg2num_give_zoom(Coordinate coord, int zoom)
100 {
101 double[] f_ret = new double[2];
102 f_ret[0] = (coord.lon + 180) / 360 * (1 << zoom);
103 f_ret[1] = (1 - Math.log(Math.tan(Math.toRadians(coord.lat)) + 1 / Math.cos(Math.toRadians(coord.lat))) / Math.PI) / 2 * (1 << zoom);
104 // return --> tile_x,tile_y
105 return f_ret;
106 }
107
108 public int[] __coord2point_give_zoom(Coordinate coord, int zoom)
109 {
110 double[] point = new double[2];
111 point = this.deg2num_give_zoom(coord, zoom);
112 int[] i_ret = new int[2];
113 i_ret[0] = (int) (point[0] * this.main_aagtl.rose.tile_size_x - this.main_aagtl.rose.map_center_x * this.main_aagtl.rose.tile_size_x + this.main_aagtl.rose.mCanvasWidth / 2);
114 i_ret[1] = (int) (point[1] * this.main_aagtl.rose.tile_size_y - this.main_aagtl.rose.map_center_y * this.main_aagtl.rose.tile_size_y + this.main_aagtl.rose.mCanvasHeight / 2);
115 return i_ret;
116 }
117
118 public void onDraw(Canvas c)
119 {
120 //System.out.println("GeocachesView: onDraw");
121
122 GeocacheCoordinate this_gc = null;
123 Coordinate this_c = null;
124
125 if (this.main_aagtl.global_settings.options_turn_map_on_heading)
126 {
127 double_buffer = true;
128 }
129 else
130 {
131 double_buffer = false;
132 }
133
134 Canvas draw_on_me = c;
135 if (double_buffer)
136 {
137 draw_on_me = this.image_main;
138 this.bitmap_main.eraseColor(Color.TRANSPARENT);
139 }
140
141 if (this.caches_loaded == null)
142 {
143 return;
144 }
145
146 // this.main_aagtl.status_text.setText(this.main_aagtl.status_text.getText() + " c:"
147 // + this.caches_loaded.size());
148
149 // too much caches to display
150 if (this.caches_loaded.size() > this.MAX_DRAW_POINTS)
151 {
152 return;
153 }
154
155 int box_width = this.gc_box_big_width;
156 int box_height = this.gc_box_big_height;
157
158 if (this.caches_loaded.size() > this.TOO_MUCH_POINTS)
159 {
160 box_width = this.gc_box_small_width;
161 box_height = this.gc_box_small_height;
162 }
163
164 Boolean target_drawn = false;
165
166 for (int _cache = 0; _cache < this.caches_loaded.size(); _cache++)
167 {
168 this_gc = this.caches_loaded.get(_cache);
169
170 //this_c = new Coordinate(this_gc.lat, this_gc.lon);
171 this_c = (Coordinate) this_gc;
172
173 //System.out.println("" + String.valueOf(this_c));
174
175 int[] p = new int[2];
176 p = this.__coord2point_give_zoom(this_c, this.main_aagtl.rose.zoom);
177
178 //
179 //
180 if ((this_gc.found) || (this_gc.aagtl_status == GeocacheCoordinate.AAGTL_STATUS_FOUND))
181 {
182 //System.out.println("cache found!");
183 box_paint.setColor(this.COLOR_FOUND);
184 }
185 else
186 {
187 if (this_gc.type == null)
188 {
189 box_paint.setColor(this.COLOR_DEFAULT);
190 }
191 else
192 {
193 if (this_gc.type.matches(GeocacheCoordinate.TYPE_REGULAR))
194 {
195 box_paint.setColor(this.COLOR_REGULAR);
196 }
197 else if (this_gc.type.matches(GeocacheCoordinate.TYPE_MULTI))
198 {
199 box_paint.setColor(this.COLOR_MULTI);
200 }
201 else
202 {
203 box_paint.setColor(this.COLOR_DEFAULT);
204 }
205 }
206 }
207
208 //
209 //
210 box_paint.setStrokeWidth(4);
211 if (this.caches_loaded.size() > this.TOO_MUCH_POINTS)
212 {
213 box_paint.setStrokeWidth(3);
214 }
215 box_paint.setStyle(Paint.Style.STROKE);
216 RectF gc_box = new RectF(p[0] - box_width, p[1] - box_height, p[0] + box_width, p[1] + box_height);
217
218 draw_on_me.drawRect(gc_box, box_paint);
219 // gc_box = new RectF(p[0] - box_width + 1, p[1] - box_height + 1, p[0] + box_width - 1, p[1]
220 // + box_height - 1);
221 // draw_on_me.drawRect(gc_box, box_paint);
222
223 //
224 //
225 box_paint.setStrokeWidth(2);
226 int inside = 3;
227 int outside = 2;
228 if (this.caches_loaded.size() > this.TOO_MUCH_POINTS)
229 {
230 box_paint.setStrokeWidth(1);
231 inside = 1;
232 outside = 2;
233 }
234
235 box_paint.setColor(Color.BLACK);
236 gc_box = new RectF(p[0] - box_width - outside, p[1] - box_height - outside, p[0] + box_width + outside, p[1] + box_height + outside);
237 draw_on_me.drawRect(gc_box, box_paint);
238 gc_box = new RectF(p[0] - box_width + inside, p[1] - box_height + inside, p[0] + box_width - inside, p[1] + box_height - inside);
239 draw_on_me.drawRect(gc_box, box_paint);
240
241 // if this cache is disabled
242 if (this_gc.status == GeocacheCoordinate.STATUS_DISABLED)
243 {
244 Paint temp_paint = new Paint();
245 temp_paint.setColor(Color.RED);
246 temp_paint.setAntiAlias(true);
247 temp_paint.setStrokeWidth(4);
248 draw_on_me.drawLine(p[0] + box_width, p[1] - box_height, p[0] - box_width - 1, p[1] + box_height - 1, temp_paint);
249 }
250
251 //
252 //
253 // draw GC-code
254 if (this.main_aagtl.rose.zoom > 15)
255 {
256 text_paint.setTextScaleX(1.0f);
257 text_paint.setTextSize(25);
258 if (this.caches_loaded.size() > this.TOO_MUCH_POINTS)
259 {
260 text_paint.setTextSize(17);
261 }
262 draw_on_me.drawText(this_gc.name, p[0] - box_width, p[1] - box_height - 6, text_paint);
263 }
264 //
265 // draw fullname (only first 20 letters!!)
266 if (this.main_aagtl.rose.zoom > 17)
267 {
268 text_paint.setTextScaleX(0.9f);
269 text_paint.setTextSize(19);
270 if (this.caches_loaded.size() > this.TOO_MUCH_POINTS)
271 {
272 text_paint.setTextSize(15);
273 }
274 draw_on_me.drawText(this_gc.title.substring(0, Math.min(20, this_gc.title.length())), p[0] - box_width, p[1] + box_height + 19, text_paint);
275 }
276
277 if (this.main_aagtl.rose.current_target != null)
278 {
279 if (this.main_aagtl.rose.current_target.name.compareTo(this_gc.name) == 0)
280 {
281 // ok this is the current target
282 box_paint.setColor(Color.BLUE);
283 box_paint.setStrokeWidth(3);
284 //this_c = new Coordinate(this_gc.lat, this_gc.lon);
285 this_c = (Coordinate) this_gc;
286
287 //System.out.println("draw target 001" + String.valueOf(this_c));
288
289 draw_on_me.drawLine(p[0], p[1], this.getWidth() / 2, this.getHeight() / 2, box_paint);
290
291 target_drawn = true;
292 }
293 else
294 {
295 // we have a manual target!!
296 }
297 }
298
299 //System.out.println("yyyy1");
300
301 }
302
303 //System.out.println("zzzzz1");
304
305 if (!target_drawn)
306 {
307 if (this.main_aagtl.rose.current_target != null)
308 {
309 // ok , manual target found!!
310 // draw it!
311
312 this_gc = this.main_aagtl.rose.current_target;
313 this_c = (Coordinate) this_gc;
314 int[] p = new int[2];
315 p = this.__coord2point_give_zoom(this_c, this.main_aagtl.rose.zoom);
316
317 // ok this is the current target
318 box_paint.setColor(Color.BLUE);
319 box_paint.setStrokeWidth(3);
320
321 //System.out.println("draw target 002=" + this_c.lat + "," + this_c.lon);
322
323 draw_on_me.drawLine(p[0], p[1], this.getWidth() / 2, this.getHeight() / 2, box_paint);
324
325 }
326 }
327
328 if ((bitmap_main != null) && (this.double_buffer))
329 {
330 //System.out.println("GCView: doDraw");
331 if (this.main_aagtl.global_settings.options_turn_map_on_heading)
332 {
333 c.save();
334 c.rotate((int) -this.main_aagtl.cross.get_gps_heading(), this.getWidth() / 2, this.getHeight() / 2);
335 }
336 c.drawBitmap(bitmap_main, 0, 0, null);
337 if (this.main_aagtl.global_settings.options_turn_map_on_heading)
338 {
339 c.restore();
340 }
341 }
342
343 }
344
345 public void onSizeChanged(int width, int height, int old_w, int old_h)
346 {
347 //System.out.println("GCView: onSizeChanged");
348 if (bitmap_main != null)
349 {
350 bitmap_main.recycle();
351 }
352 bitmap_main = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
353 image_main = new Canvas(bitmap_main);
354 }
355
356 }

   
Visit the aagtl Website