android: ScaleBar layer is back

This commit is contained in:
Hannes Janetzek 2014-01-28 00:41:36 +01:00
parent cad5c1ed7b
commit e620d15964
3 changed files with 300 additions and 293 deletions

View File

@ -15,6 +15,7 @@
* this program. If not, see <http://www.gnu.org/licenses/>. * this program. If not, see <http://www.gnu.org/licenses/>.
*/package org.oscim.android.test; */package org.oscim.android.test;
import org.oscim.android.MapScaleBar;
import org.oscim.layers.tile.vector.BuildingLayer; import org.oscim.layers.tile.vector.BuildingLayer;
import org.oscim.layers.tile.vector.labeling.LabelLayer; import org.oscim.layers.tile.vector.labeling.LabelLayer;
import org.oscim.map.Layers; import org.oscim.map.Layers;
@ -37,33 +38,13 @@ public class SimpleMapActivity extends BaseMapActivity {
layers.add(new LabelLayer(mMap, l)); layers.add(new LabelLayer(mMap, l));
//layers.add(new TileGridLayer(mMap)); //layers.add(new TileGridLayer(mMap));
layers.add(new MapScaleBar(mMapView));
mMap.setTheme(InternalRenderTheme.DEFAULT); mMap.setTheme(InternalRenderTheme.DEFAULT);
//mMap.setTheme(InternalRenderTheme.TRONRENDER); //mMap.setTheme(InternalRenderTheme.TRONRENDER);
//mMap.setTheme(InternalRenderTheme.OSMARENDER); //mMap.setTheme(InternalRenderTheme.OSMARENDER);
mMap.setMapPosition(53.08, 8.83, Math.pow(2, 14)); mMap.setMapPosition(53.08, 8.83, Math.pow(2, 14));
//loooop(0);
}
void loooop(final int i) {
mMapView.postDelayed(new Runnable() {
@Override
public void run() {
InternalRenderTheme t;
if (i == 0)
t = InternalRenderTheme.DEFAULT;
else if (i == 1)
t = InternalRenderTheme.TRONRENDER;
else
t = InternalRenderTheme.OSMARENDER;
mMapView.getMap().setTheme(t);
loooop((i + 1) % 3);
}
}, 300 + (int)(Math.random() * 200));
} }
@Override @Override
@ -94,4 +75,24 @@ public class SimpleMapActivity extends BaseMapActivity {
return false; return false;
} }
// Stress testing
void loooop(final int i) {
mMapView.postDelayed(new Runnable() {
@Override
public void run() {
InternalRenderTheme t;
if (i == 0)
t = InternalRenderTheme.DEFAULT;
else if (i == 1)
t = InternalRenderTheme.TRONRENDER;
else
t = InternalRenderTheme.OSMARENDER;
mMapView.getMap().setTheme(t);
loooop((i + 1) % 3);
}
}, 300 + (int)(Math.random() * 200));
}
} }

View File

@ -1,273 +1,279 @@
package org.oscim.android; package org.oscim.android;
///* /*
// * Copyright 2010, 2011, 2012 mapsforge.org * Copyright 2010, 2011, 2012 mapsforge.org
// * Copyright 2013 Hannes Janetzek * Copyright 2013 Hannes Janetzek
// * *
// * This program is free software: you can redistribute it and/or modify it under the * This program is free software: you can redistribute it and/or modify it under the
// * terms of the GNU Lesser General Public License as published by the Free Software * terms of the GNU Lesser General Public License as published by the Free Software
// * Foundation, either version 3 of the License, or (at your option) any later version. * Foundation, either version 3 of the License, or (at your option) any later version.
// * *
// * This program is distributed in the hope that it will be useful, but WITHOUT ANY * This program is distributed in the hope that it will be useful, but WITHOUT ANY
// * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
// * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
// * *
// * You should have received a copy of the GNU Lesser General Public License along with * You should have received a copy of the GNU Lesser General Public License along with
// * this program. If not, see <http://www.gnu.org/licenses/>. * this program. If not, see <http://www.gnu.org/licenses/>.
// */ */
//package org.oscim.layers.overlay;
// import java.util.HashMap;
//import java.util.HashMap; import java.util.Map;
//import java.util.Map;
// import org.oscim.android.canvas.AndroidBitmap;
//import org.oscim.core.MapPosition; import org.oscim.core.MapPosition;
//import org.oscim.core.MercatorProjection; import org.oscim.core.MercatorProjection;
//import org.oscim.layers.Layer; import org.oscim.layers.Layer;
//import org.oscim.renderer.layers.BitmapRenderLayer; import org.oscim.map.Map.UpdateListener;
//import org.oscim.view.MapView; import org.oscim.renderer.BitmapRenderer;
//
//import android.graphics.Bitmap; import android.graphics.Bitmap;
//import android.graphics.Canvas; import android.graphics.Canvas;
//import android.graphics.Color; import android.graphics.Color;
//import android.graphics.Paint; import android.graphics.Paint;
//import android.graphics.Typeface; import android.graphics.Typeface;
//
///** /**
// * A MapScaleBar displays the ratio of a distance on the map to the * A MapScaleBar displays the ratio of a distance on the map to the
// * corresponding distance on the ground. * corresponding distance on the ground.
// */ */
//public class MapScaleBar extends Layer { public class MapScaleBar extends Layer implements UpdateListener {
//
// private static final int BITMAP_HEIGHT = 64; private static final int BITMAP_HEIGHT = 64;
// private static final int BITMAP_WIDTH = 128; private static final int BITMAP_WIDTH = 128;
// private static final double LATITUDE_REDRAW_THRESHOLD = 0.2; private static final double LATITUDE_REDRAW_THRESHOLD = 0.2;
// // private static final int MARGIN_BOTTOM = 5; // private static final int MARGIN_BOTTOM = 5;
// // private static final int MARGIN_LEFT = 5; // private static final int MARGIN_LEFT = 5;
//
// private static final double METER_FOOT_RATIO = 0.3048; private static final double METER_FOOT_RATIO = 0.3048;
// private static final int ONE_KILOMETER = 1000; private static final int ONE_KILOMETER = 1000;
// private static final int ONE_MILE = 5280; private static final int ONE_MILE = 5280;
//
// private static final Paint SCALE_BAR = new Paint(Paint.ANTI_ALIAS_FLAG); private static final Paint SCALE_BAR = new Paint(Paint.ANTI_ALIAS_FLAG);
// private static final Paint SCALE_BAR_STROKE = new Paint(Paint.ANTI_ALIAS_FLAG); private static final Paint SCALE_BAR_STROKE = new Paint(Paint.ANTI_ALIAS_FLAG);
// private static final Paint SCALE_TEXT = new Paint(Paint.ANTI_ALIAS_FLAG); private static final Paint SCALE_TEXT = new Paint(Paint.ANTI_ALIAS_FLAG);
// private static final Paint SCALE_TEXT_STROKE = new Paint(Paint.ANTI_ALIAS_FLAG); private static final Paint SCALE_TEXT_STROKE = new Paint(Paint.ANTI_ALIAS_FLAG);
//
// private static final int[] SCALE_BAR_VALUES_IMPERIAL = { private static final int[] SCALE_BAR_VALUES_IMPERIAL = {
// 26400000, 10560000, 5280000, 26400000, 10560000, 5280000,
// 2640000, 1056000, 528000, 2640000, 1056000, 528000,
// 264000, 105600, 52800, 26400, 264000, 105600, 52800, 26400,
// 10560, 5280, 2000, 1000, 500, 10560, 5280, 2000, 1000, 500,
// 200, 100, 50, 20, 200, 100, 50, 20,
// 10, 5, 2, 1 }; 10, 5, 2, 1 };
// private static final int[] SCALE_BAR_VALUES_METRIC = { private static final int[] SCALE_BAR_VALUES_METRIC = {
// 10000000, 5000000, 2000000, 1000000, 10000000, 5000000, 2000000, 1000000,
// 500000, 200000, 100000, 50000, 500000, 200000, 100000, 50000,
// 20000, 10000, 5000, 2000, 1000, 20000, 10000, 5000, 2000, 1000,
// 500, 200, 100, 50, 20, 10, 5, 2, 1 }; 500, 200, 100, 50, 20, 10, 5, 2, 1 };
//
// private boolean mImperialUnits; private boolean mImperialUnits;
// private final Canvas mMapScaleCanvas; private final Canvas mMapScaleCanvas;
// private boolean mRedrawNeeded; private boolean mRedrawNeeded;
// private double mPrevLatitude = -1; private double mPrevLatitude = -1;
// private final double mPrevScale = -1; private final double mPrevScale = -1;
// private final Map<TextField, String> mTextFields; private final Map<TextField, String> mTextFields;
//
// private final Bitmap mMapScaleBitmap; private final Bitmap mBitmap;
// private final BitmapRenderLayer mBitmapLayer; // passed to BitmapRenderer - need to sync on this object.
// private final AndroidBitmap mLayerBitmap;
// public MapScaleBar(MapView map) { private final BitmapRenderer mBitmapLayer;
// super(map);
// public MapScaleBar(MapView map) {
// mMapScaleBitmap = Bitmap.createBitmap( super(map.getMap());
// BITMAP_WIDTH, BITMAP_HEIGHT,
// Bitmap.Config.ARGB_8888); mBitmap = Bitmap.createBitmap(BITMAP_WIDTH,
// BITMAP_HEIGHT,
// mMapScaleCanvas = new Canvas(mMapScaleBitmap); Bitmap.Config.ARGB_8888);
// mTextFields = new HashMap<TextField, String>();
// mMapScaleCanvas = new Canvas(mBitmap);
// setDefaultTexts(); mTextFields = new HashMap<TextField, String>();
// configurePaints();
// setDefaultTexts();
// mRedrawNeeded = true; configurePaints();
// mLayer = mBitmapLayer = new BitmapRenderLayer(map);
// mBitmapLayer.setBitmap(mMapScaleBitmap, 0, 0, mRedrawNeeded = true;
// (int)(BITMAP_WIDTH * 1.2f), mRenderer = mBitmapLayer = new BitmapRenderer();
// (int)(BITMAP_HEIGHT * 1.2f)); mLayerBitmap = new AndroidBitmap(mBitmap);
// } mBitmapLayer.setBitmap(mLayerBitmap,
// BITMAP_WIDTH,
// @Override BITMAP_HEIGHT,
// public void onUpdate(MapPosition mapPosition, boolean changed, boolean clear) { (int) (BITMAP_WIDTH * 1.2f),
// double latitude = MercatorProjection.toLatitude(mapPosition.y); (int) (BITMAP_HEIGHT * 1.2f));
// }
// if (!mRedrawNeeded) {
// double scaleDiff = mPrevScale / mapPosition.scale; @Override
// if (scaleDiff < 1.1 && scaleDiff > 0.9) { public void onMapUpdate(MapPosition mapPosition, boolean changed, boolean clear) {
// double latitudeDiff = Math.abs(mPrevLatitude - latitude);
// if (latitudeDiff < LATITUDE_REDRAW_THRESHOLD) double latitude = MercatorProjection.toLatitude(mapPosition.y);
// return;
// } if (!mRedrawNeeded) {
// } double scaleDiff = mPrevScale / mapPosition.scale;
// mPrevLatitude = latitude; if (scaleDiff < 1.1 && scaleDiff > 0.9) {
// double latitudeDiff = Math.abs(mPrevLatitude - latitude);
// double groundResolution = MercatorProjection.calculateGroundResolution( if (latitudeDiff < LATITUDE_REDRAW_THRESHOLD)
// latitude, mapPosition.scale); return;
// }
// int[] scaleBarValues; }
// if (mImperialUnits) { mPrevLatitude = latitude;
// groundResolution = groundResolution / METER_FOOT_RATIO;
// scaleBarValues = SCALE_BAR_VALUES_IMPERIAL; double groundResolution = MercatorProjection
// } else { .calculateGroundResolution(latitude, mapPosition.scale);
// scaleBarValues = SCALE_BAR_VALUES_METRIC;
// } int[] scaleBarValues;
// if (mImperialUnits) {
// float scaleBarLength = 0; groundResolution = groundResolution / METER_FOOT_RATIO;
// int mapScaleValue = 0; scaleBarValues = SCALE_BAR_VALUES_IMPERIAL;
// } else {
// for (int i = 0; i < scaleBarValues.length; ++i) { scaleBarValues = SCALE_BAR_VALUES_METRIC;
// mapScaleValue = scaleBarValues[i]; }
// scaleBarLength = mapScaleValue / (float) groundResolution;
// if (scaleBarLength < (BITMAP_WIDTH - 10)) { float scaleBarLength = 0;
// break; int mapScaleValue = 0;
// }
// } for (int i = 0; i < scaleBarValues.length; ++i) {
// synchronized (mMapScaleBitmap) { mapScaleValue = scaleBarValues[i];
// redrawMapScaleBitmap(scaleBarLength, mapScaleValue); scaleBarLength = mapScaleValue / (float) groundResolution;
// } if (scaleBarLength < (BITMAP_WIDTH - 10)) {
// break;
// mBitmapLayer.updateBitmap(); }
// }
// mRedrawNeeded = false; synchronized (mLayerBitmap) {
// } redrawMapScaleBitmap(scaleBarLength, mapScaleValue);
// }
// /**
// * @return true if imperial units are used, false otherwise. mBitmapLayer.updateBitmap();
// */
// public boolean isImperialUnits() { mRedrawNeeded = false;
// return mImperialUnits; }
// }
// /**
// /** * @return true if imperial units are used, false otherwise.
// * @param imperialUnits */
// * true if imperial units should be used rather than metric public boolean isImperialUnits() {
// * units. return mImperialUnits;
// */ }
// public void setImperialUnits(boolean imperialUnits) {
// mImperialUnits = imperialUnits; /**
// mRedrawNeeded = true; * @param imperialUnits
// } * true if imperial units should be used rather than metric
// * units.
// /** */
// * Overrides the specified text field with the given string. public void setImperialUnits(boolean imperialUnits) {
// * mImperialUnits = imperialUnits;
// * @param textField mRedrawNeeded = true;
// * the text field to override. }
// * @param value
// * the new value of the text field. /**
// */ * Overrides the specified text field with the given string.
// public void setText(TextField textField, String value) { *
// mTextFields.put(textField, value); * @param textField
// mRedrawNeeded = true; * the text field to override.
// } * @param value
// * the new value of the text field.
// private void drawScaleBar(float scaleBarLength, Paint paint) { */
// mMapScaleCanvas.drawLine(7, 25, scaleBarLength + 3, 25, paint); public void setText(TextField textField, String value) {
// mMapScaleCanvas.drawLine(5, 10, 5, 40, paint); mTextFields.put(textField, value);
// mMapScaleCanvas.drawLine(scaleBarLength + 5, 10, scaleBarLength + 5, 40, paint); mRedrawNeeded = true;
// } }
//
// private void drawScaleText(int scaleValue, String unitSymbol, Paint paint) { private void drawScaleBar(float scaleBarLength, Paint paint) {
// mMapScaleCanvas.drawText(scaleValue + unitSymbol, 12, 18, paint); mMapScaleCanvas.drawLine(7, 25, scaleBarLength + 3, 25, paint);
// } mMapScaleCanvas.drawLine(5, 10, 5, 40, paint);
// mMapScaleCanvas.drawLine(scaleBarLength + 5, 10, scaleBarLength + 5, 40, paint);
// /** }
// * Redraws the map scale bitmap with the given parameters.
// * private void drawScaleText(int scaleValue, String unitSymbol, Paint paint) {
// * @param scaleBarLength mMapScaleCanvas.drawText(scaleValue + unitSymbol, 12, 18, paint);
// * the length of the map scale bar in pixels. }
// * @param mapScaleValue
// * the map scale value in meters. /**
// */ * Redraws the map scale bitmap with the given parameters.
// private void redrawMapScaleBitmap(float scaleBarLength, int mapScaleValue) { *
// mMapScaleBitmap.eraseColor(Color.TRANSPARENT); * @param scaleBarLength
// * the length of the map scale bar in pixels.
// // draw the scale bar * @param mapScaleValue
// drawScaleBar(scaleBarLength, SCALE_BAR_STROKE); * the map scale value in meters.
// drawScaleBar(scaleBarLength, SCALE_BAR); */
// private void redrawMapScaleBitmap(float scaleBarLength, int mapScaleValue) {
// int scaleValue; mBitmap.eraseColor(Color.TRANSPARENT);
// String unitSymbol;
// if (mImperialUnits) { // draw the scale bar
// if (mapScaleValue < ONE_MILE) { drawScaleBar(scaleBarLength, SCALE_BAR_STROKE);
// scaleValue = mapScaleValue; drawScaleBar(scaleBarLength, SCALE_BAR);
// unitSymbol = mTextFields.get(TextField.FOOT);
// } else { int scaleValue;
// scaleValue = mapScaleValue / ONE_MILE; String unitSymbol;
// unitSymbol = mTextFields.get(TextField.MILE); if (mImperialUnits) {
// } if (mapScaleValue < ONE_MILE) {
// } else { scaleValue = mapScaleValue;
// if (mapScaleValue < ONE_KILOMETER) { unitSymbol = mTextFields.get(TextField.FOOT);
// scaleValue = mapScaleValue; } else {
// unitSymbol = mTextFields.get(TextField.METER); scaleValue = mapScaleValue / ONE_MILE;
// } else { unitSymbol = mTextFields.get(TextField.MILE);
// scaleValue = mapScaleValue / ONE_KILOMETER; }
// unitSymbol = mTextFields.get(TextField.KILOMETER); } else {
// } if (mapScaleValue < ONE_KILOMETER) {
// } scaleValue = mapScaleValue;
// unitSymbol = mTextFields.get(TextField.METER);
// // draw the scale text } else {
// drawScaleText(scaleValue, unitSymbol, SCALE_TEXT_STROKE); scaleValue = mapScaleValue / ONE_KILOMETER;
// drawScaleText(scaleValue, unitSymbol, SCALE_TEXT); unitSymbol = mTextFields.get(TextField.KILOMETER);
// } }
// }
// private void setDefaultTexts() {
// mTextFields.put(TextField.FOOT, " ft"); // draw the scale text
// mTextFields.put(TextField.MILE, " mi"); drawScaleText(scaleValue, unitSymbol, SCALE_TEXT_STROKE);
// drawScaleText(scaleValue, unitSymbol, SCALE_TEXT);
// mTextFields.put(TextField.METER, " m"); }
// mTextFields.put(TextField.KILOMETER, " km");
// } private void setDefaultTexts() {
// mTextFields.put(TextField.FOOT, " ft");
// private static void configurePaints() { mTextFields.put(TextField.MILE, " mi");
// SCALE_BAR.setStrokeWidth(2);
// SCALE_BAR.setStrokeCap(Paint.Cap.SQUARE); mTextFields.put(TextField.METER, " m");
// SCALE_BAR.setColor(Color.BLACK); mTextFields.put(TextField.KILOMETER, " km");
// SCALE_BAR_STROKE.setStrokeWidth(5); }
// SCALE_BAR_STROKE.setStrokeCap(Paint.Cap.SQUARE);
// SCALE_BAR_STROKE.setColor(Color.WHITE); private static void configurePaints() {
// SCALE_BAR.setStrokeWidth(2);
// SCALE_TEXT.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); SCALE_BAR.setStrokeCap(Paint.Cap.SQUARE);
// SCALE_TEXT.setTextSize(17); SCALE_BAR.setColor(Color.BLACK);
// SCALE_TEXT.setColor(Color.BLACK); SCALE_BAR_STROKE.setStrokeWidth(5);
// SCALE_TEXT_STROKE.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); SCALE_BAR_STROKE.setStrokeCap(Paint.Cap.SQUARE);
// SCALE_TEXT_STROKE.setStyle(Paint.Style.STROKE); SCALE_BAR_STROKE.setColor(Color.WHITE);
// SCALE_TEXT_STROKE.setColor(Color.WHITE);
// SCALE_TEXT_STROKE.setStrokeWidth(2); SCALE_TEXT.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
// SCALE_TEXT_STROKE.setTextSize(17); SCALE_TEXT.setTextSize(17);
// } SCALE_TEXT.setColor(Color.BLACK);
// SCALE_TEXT_STROKE.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
// /** SCALE_TEXT_STROKE.setStyle(Paint.Style.STROKE);
// * Enumeration of all text fields. SCALE_TEXT_STROKE.setColor(Color.WHITE);
// */ SCALE_TEXT_STROKE.setStrokeWidth(2);
// public enum TextField { SCALE_TEXT_STROKE.setTextSize(17);
// /** }
// * Unit symbol for one foot.
// */ /**
// FOOT, * Enumeration of all text fields.
// */
// /** public enum TextField {
// * Unit symbol for one kilometer. /**
// */ * Unit symbol for one foot.
// KILOMETER, */
// FOOT,
// /**
// * Unit symbol for one meter. /**
// */ * Unit symbol for one kilometer.
// METER, */
// KILOMETER,
// /**
// * Unit symbol for one mile. /**
// */ * Unit symbol for one meter.
// MILE; */
// } METER,
//}
/**
* Unit symbol for one mile.
*/
MILE;
}
}

View File

@ -42,7 +42,7 @@ public class AndroidBitmap implements org.oscim.backend.canvas.Bitmap {
.createBitmap(width, height, android.graphics.Bitmap.Config.ARGB_8888); .createBitmap(width, height, android.graphics.Bitmap.Config.ARGB_8888);
} }
AndroidBitmap(android.graphics.Bitmap bitmap) { public AndroidBitmap(android.graphics.Bitmap bitmap) {
mBitmap = bitmap; mBitmap = bitmap;
} }