{"action":"created","issue":{"url":"https://api.github.com/repos/osmdroid/osmdroid/issues/212","labels_url":"https://api.github.com/repos/osmdroid/osmdroid/issues/212/labels{/name}","comments_url":"https://api.github.com/repos/osmdroid/osmdroid/issues/212/comments","events_url":"https://api.github.com/repos/osmdroid/osmdroid/issues/212/events","html_url":"https://github.com/osmdroid/osmdroid/issues/212","id":121917776,"number":212,"title":"ScaleBar aligned bottom or right","user":{"login":"stellardeveloper","id":15923908,"avatar_url":"https://avatars.githubusercontent.com/u/15923908?v=3","gravatar_id":"","url":"https://api.github.com/users/stellardeveloper","html_url":"https://github.com/stellardeveloper","followers_url":"https://api.github.com/users/stellardeveloper/followers","following_url":"https://api.github.com/users/stellardeveloper/following{/other_user}","gists_url":"https://api.github.com/users/stellardeveloper/gists{/gist_id}","starred_url":"https://api.github.com/users/stellardeveloper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stellardeveloper/subscriptions","organizations_url":"https://api.github.com/users/stellardeveloper/orgs","repos_url":"https://api.github.com/users/stellardeveloper/repos","events_url":"https://api.github.com/users/stellardeveloper/events{/privacy}","received_events_url":"https://api.github.com/users/stellardeveloper/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-12-13T12:57:33Z","updated_at":"2015-12-13T16:20:05Z","closed_at":null,"body":"I want to include a scale bar, but bottom aligned. As it is still on the ScaleBarOverlay'sTODO list, I did it.\r\nMy solution:\r\nmodifying/adding a few variables and methods:\r\n\r\n\tprivate final MapView mMapView;\r\n\r\n\tprotected boolean alignBottom = false;\r\n\tprotected boolean alignRight  = false;\r\n\r\n\tpublic void setCentred(final boolean centred) {\r\n\t\tthis.centred = centred;\r\n\t\talignBottom  = !centred;\r\n\t\talignRight   = !centred;\r\n\t\tlastZoomLevel = -1; // Force redraw of scalebar\r\n\t}\r\n\t\r\n\tpublic void setAlignBottom(final boolean alignBottom) {\r\n\t\tthis.centred = false;\r\n\t\tthis.alignBottom  = alignBottom;\r\n\t\tlastZoomLevel = -1; // Force redraw of scalebar\r\n\t}\r\n\r\n\tpublic void setAlignRight(final boolean alignRight) {\r\n\t\tthis.centred = false;\r\n\t\tthis.alignRight  = alignRight;\r\n\t\tlastZoomLevel = -1; // Force redraw of scalebar\r\n\t}\r\n\r\nFor further operations we need the size of the mapView. I changed the constructor like this:\r\n\r\n\tpublic ScaleBarOverlay(final MapView mapView) {\r\n\t\tthis(mapView, new DefaultResourceProxyImpl(mapView.getContext()));\r\n\t}\r\n\r\n\tpublic ScaleBarOverlay(final MapView mapView, final ResourceProxy pResourceProxy) {\r\n\t\tsuper(pResourceProxy);\r\n\t\tthis.mMapView = mapView;\r\n\t\tthis.resourceProxy = pResourceProxy;\r\n\t\tthis.context = mapView.getContext();\r\n               // the rest of constructor is not  changed\r\n\r\nUnfortunately, when creating the scaleBar in onCreate or similar mthods, the mapView dimensions are still set to zero, but they are visible at onDraw. So Initialization of screenWidth and height is useless in the constructor. Of  course you have  to call the constructor now with your current mapView as the parameter.\r\n\r\nAdd to the draw method\r\n\r\n\t\t\tscreenWidth\t   = mMapView.getWidth();\r\n\t\t\tscreenHeight   = mMapView.getHeight();\r\n\r\nBEFORE calling  \r\n  final IGeoPoint center = projection.fromPixels(screenWidth / 2, screenHeight / 2, null)!\r\n\r\nBTW: until now, the center was not exactly the center, as you usually have appr. 100 pixels of window borders on top of your screen.\r\n\r\nAlso add to draw method\r\n\r\n\t\t\tif (alignBottom) offsetY *=-1;\r\n\t\t\tif (alignRight ) offsetX *=-1;\r\n\r\nafter offsetX and offsetY initialization. This will keep the offset effect independent of the alignment.\r\n\r\nThe largest modification is required in rebuildBarPath:\r\n\r\n                // .... parts before comment \"create text\" are not changed\r\n\r\n\t\t// create text\r\n\t\tfinal String xMsg = scaleBarLengthText((int) xMetersAdjusted);\r\n\t\tfinal Rect xTextRect = new Rect();\r\n\t\ttextPaint.getTextBounds(xMsg, 0, xMsg.length(), xTextRect);\r\n\t\tint xTextSpacing = (int) (xTextRect.height() / 5.0);           \r\n\r\n\t\t// create text\r\n\t\tfinal String yMsg = scaleBarLengthText((int) yMetersAdjusted);\r\n\t\tfinal Rect yTextRect = new Rect();\r\n\t\ttextPaint.getTextBounds(yMsg, 0, yMsg.length(), yTextRect);\r\n\t\tint yTextSpacing = (int) (yTextRect.height() / 5.0);\r\n\t\tint xTextHeight  = xTextRect.height();\r\n\t\tint yTextHeight  = yTextRect.height();\r\n\r\n\t\tbarPath.rewind();\r\n\t\t\r\n\t\t// ** alignBottom ad-ons, initilize with their former values  \r\n\t\tint barOriginX = 0;\r\n\t\tint barOriginY = 0;\r\n\t\tint barToX     = xBarLengthPixels;\r\n\t\tint barToY\t   = yBarLengthPixels;\r\n\t\tif (alignBottom) {\r\n\t\t\txTextSpacing *= -1;\r\n\t\t\txTextHeight  *= -1;\r\n\t\t\tbarOriginY   = mMapView.getHeight();\r\n\t\t\tbarToY       = barOriginY -yBarLengthPixels;\r\n\t\t}\r\n\t\t\t\t\r\n\t\tif (alignRight) {\r\n\t\t\tyTextSpacing *= -1;\r\n\t\t\tyTextHeight  *= -1;\t\t\r\n\t\t\tbarOriginX   = mMapView.getWidth();\r\n\t\t\tbarToX       = barOriginX -xBarLengthPixels;    \r\n\t\t}\r\n\t\t\r\n\t\tif (latitudeBar) {\r\n\t\t\t// draw latitude bar\r\n\t\t\tbarPath.moveTo(barToX, barOriginY +xTextHeight + xTextSpacing * 2);\r\n\t\t\tbarPath.lineTo(barToX, barOriginY);\r\n\t\t\tbarPath.lineTo(barOriginX, barOriginY);\r\n\r\n\t\t\tif (!longitudeBar) {\r\n\t\t\t\tbarPath.lineTo(barOriginX,  barOriginY +xTextHeight + xTextSpacing * 2);\r\n\t\t\t}\r\n\t\t\tlatitudeBarRect.set(barOriginX, barOriginY, barToX, barOriginY +xTextHeight + xTextSpacing * 2);\r\n\t\t}\r\n\r\n\t\tif (longitudeBar) {\r\n\t\t\t// draw longitude bar\r\n\t\t\tif (!latitudeBar) {\r\n\t\t\t\tbarPath.moveTo(barOriginX +yTextHeight + yTextSpacing * 2, barOriginY);\r\n\t\t\t\tbarPath.lineTo(barOriginX, barOriginY);\r\n\t\t\t}\r\n\r\n\t\t\tbarPath.lineTo(barOriginX, barToY);\r\n\t\t\tbarPath.lineTo(barOriginX +yTextHeight + yTextSpacing * 2, barToY);\r\n\r\n\t\t\tlongitudeBarRect.set(barOriginX, barOriginY, barOriginX +yTextHeight + yTextSpacing * 2, barToY);\r\n\t\t}\r\n\t}\r\n\r\nNow change/add in the drawLatitudeText bevore calling canvas.drawText:\r\n\r\n\t\tfloat x = xBarLengthPixels / 2 - sTextBoundsRect.width() / 2;\r\n\t\tif (alignRight)  x+= screenWidth -xBarLengthPixels;\r\n\t\tfloat y; \r\n\t\tif (alignBottom) { y = screenHeight   -xTextSpacing*2; }\r\n\t\telse y = sTextBoundsRect.height() + xTextSpacing;\r\n\r\nand do similar in  drawLongitudeText before calling canvas.save():\r\n\r\n\t\tfloat x; \r\n\t\tif (alignRight)  {x = screenWidth  -yTextSpacing*2;}\r\n\t\telse x = sTextBoundsRect.height() + yTextSpacing;\r\n\t\tfloat y = yBarLengthPixels / 2 + sTextBoundsRect.width() / 2;\r\n\t\tif (alignBottom) y+= screenHeight -yBarLengthPixels;\r\n\r\n\r\n "},"comment":{"url":"https://api.github.com/repos/osmdroid/osmdroid/issues/comments/164272113","html_url":"https://github.com/osmdroid/osmdroid/issues/212#issuecomment-164272113","issue_url":"https://api.github.com/repos/osmdroid/osmdroid/issues/212","id":164272113,"user":{"login":"spyhunter99","id":1975321,"avatar_url":"https://avatars.githubusercontent.com/u/1975321?v=3","gravatar_id":"","url":"https://api.github.com/users/spyhunter99","html_url":"https://github.com/spyhunter99","followers_url":"https://api.github.com/users/spyhunter99/followers","following_url":"https://api.github.com/users/spyhunter99/following{/other_user}","gists_url":"https://api.github.com/users/spyhunter99/gists{/gist_id}","starred_url":"https://api.github.com/users/spyhunter99/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/spyhunter99/subscriptions","organizations_url":"https://api.github.com/users/spyhunter99/orgs","repos_url":"https://api.github.com/users/spyhunter99/repos","events_url":"https://api.github.com/users/spyhunter99/events{/privacy}","received_events_url":"https://api.github.com/users/spyhunter99/received_events","type":"User","site_admin":false},"created_at":"2015-12-13T16:20:05Z","updated_at":"2015-12-13T16:20:05Z","body":"There's a related issue for scale bars. Adjustable font size, color, and screen render size. I think right now its rendered as one inch wide, which on a big display, wraps the text, which is ugly. Bottom line, it could use some work"}}