package dk.thoerup.friendradar; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; import android.location.Location; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; //public class RadarView extends View { public class RadarView extends SurfaceView implements SurfaceHolder.Callback, Runnable { int angle = 0; Paint p = new Paint(); Paint p2 = new Paint(); Paint p3 = new Paint(); SurfaceHolder surfaceHolder; int distanceBase = 500; String distanceText1; String distanceText2; String distanceText3; String distanceText4; float textWidth1; float textWidth2; float textWidth3; float textWidth4; String gpsText = "-"; Location currentLocation; int heading; String headingText = "-"; int width; int height; int hcenter; int vcenter; boolean mRun = true; Thread t; public RadarView(Context context) { super(context); init(); } public RadarView(Context context, AttributeSet attr) { super(context,attr); init(); } public RadarView(Context context, AttributeSet attr, int defstyle) { super(context,attr, defstyle); init(); } void init() { p.setColor(0xaa00aa00);//Dark green p.setStrokeWidth(2); p.setStyle(Paint.Style.STROKE); p.setAntiAlias(true); p.setTextSize(1); p2.setStrokeWidth(2); p2.setColor(0xff00cc00);//Dark green p2.setStyle(Paint.Style.STROKE); //p2.setAntiAlias(true); p3.setStrokeWidth(2); p3.setColor(0xff00cc00);//Dark green p3.setStyle(Paint.Style.FILL); surfaceHolder = this.getHolder(); surfaceHolder.addCallback(this); t = new Thread(this); updateDistanceTexts(); } protected void myDraw(Canvas canvas) { canvas.drawColor(0xff000000); canvas.drawLine(hcenter-5, vcenter-5, hcenter+5, vcenter+5, p); canvas.drawLine(hcenter-5, vcenter+5, hcenter+5, vcenter-5, p); canvas.drawCircle(hcenter, vcenter+5, 40, p); canvas.drawCircle(hcenter, vcenter+5, 80, p); canvas.drawCircle(hcenter, vcenter+5, 115, p); canvas.drawCircle(hcenter, vcenter+5, 150, p); canvas.drawText(distanceText1, (width-textWidth1)/2, vcenter+40+6, p3); canvas.drawText(distanceText2, (width-textWidth2)/2, vcenter+80+6, p3); canvas.drawText(distanceText3, (width-textWidth3)/2, vcenter+115+6, p3); canvas.drawText(distanceText4, (width-textWidth4)/2, vcenter+150+6, p3); final float correction = 5; final float lineLength = 150; angle -= 6; double base = (Math.sin(angle/360.0)*lineLength) + (float)hcenter; double height = (Math.cos(angle/360.0)*lineLength) + (float)vcenter; height += correction; canvas.drawLine(hcenter, vcenter, (float)base, (float)height, p2); //left,top,right,bottom canvas.drawRoundRect( new RectF(20,360,180,400), 10, 10, p); canvas.drawLine(100, 360, 100, 400, p); canvas.drawLine(45, 380, 75, 380, p); // Minus canvas.drawLine(125, 380, 155, 380, p); // Plus canvas.drawLine(140, 365, 140, 395, p); // Plus // Gps status canvas.drawText("Gps accuracy", 10, 20,p3); canvas.drawText(gpsText, 10, 40, p3); // Heading canvas.drawText("Heading", 270, 20, p3); canvas.drawText(headingText, 280, 40, p3); drawLocations(canvas); } @Override //surfaceholder.CallBack public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.i("surfacChaned()","-"); } @Override public void surfaceCreated(SurfaceHolder holder) { //surfaceholder.CallBack Log.i("surfacCreated()","-"); width = this.getWidth(); height = this.getHeight(); hcenter = width / 2; vcenter = hcenter + 20; t.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { //surfaceholder.CallBack Log.i("surfacDestroyed()","-"); mRun = false; } public void run() { while (mRun) { Canvas c = null; try { c = surfaceHolder.lockCanvas(null); synchronized (surfaceHolder) { //if (mMode == STATE_RUNNING) // updatePhysics(); myDraw(c); } } finally { // do this in a finally so that if an exception is thrown // during the above, we don't leave the Surface in an // inconsistent state if (c != null) { surfaceHolder.unlockCanvasAndPost(c); } } try { Thread.sleep(20); } catch (InterruptedException e) {} } } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); Log.i("TouchEvent", "Coords: " + x + "," + y ); if (x>25 && x<95 && y>360 && y<400) { //zoom out Log.i("Button", "Zoom Out"); if (distanceBase < 16000) { distanceBase *= 2; updateDistanceTexts(); } } if (x>105 && x<175 && y>360 && y<400) { Log.i("Button", "Zoom In"); // zoom in if (distanceBase > 125) { distanceBase /= 2; updateDistanceTexts(); } } return false; } void updateDistanceTexts() { distanceText1 = formatLength(distanceBase); distanceText2 = formatLength(distanceBase*2); distanceText3 = formatLength(distanceBase*3); distanceText4 = formatLength(distanceBase*4); textWidth1 = textWidth(distanceText1); textWidth2 = textWidth(distanceText2); textWidth3 = textWidth(distanceText3); textWidth4 = textWidth(distanceText4); } float textWidth(String text) { float[] width = { 0.0f }; p3.breakText(text, true, 200, width); return width[0]; } String formatLength(int meters) { if (meters == 1500) //yuck, special cases !!! return "1.5 km"; if (meters>=1000) return "" + (meters/1000) + " km"; else return "" + meters + " m"; } void setCurrentLocation(Location loc) { currentLocation = loc; gpsText = formatLength( (int)loc.getAccuracy()); } void setHeading(int head) { heading = head; headingText = "" + head; } void drawLocations(Canvas canvas) { if (currentLocation != null) { Location loc = new Location("gps"); loc.setLatitude(56.394908); //Hedemøllevej loc.setLongitude(9.666356); // loc.setLatitude(56.298307); //Kongensbro // loc.setLongitude(9.669293); loc.setLatitude(56.398577); //Langå loc.setLongitude(9.899515); drawLocation(canvas, loc); } } void drawLocation(Canvas canvas, Location location) { float bearing = currentLocation.bearingTo(location); float distance = currentLocation.distanceTo(location); //Log.e("Bearing to", ""+bearing); //Log.e("Distance to", ""+distance); bearing -= (float) heading; if (distance <= (distanceBase*4)) { //convert from degrees to radians double bearingRad = Math.toRadians(bearing-90); double hypotenuse = (distance / (distanceBase*4.0)) * 150.0; double vertCathesis = Math.sin(bearingRad)*hypotenuse; double horzCathesis = Math.cos(bearingRad)*hypotenuse; //vertCathesis *= -1.0; vertCathesis += vcenter; horzCathesis += hcenter; canvas.drawCircle( (float)horzCathesis, (float)vertCathesis, 3, p3); } } }