add option for non-billboard MarkerSymbols

This commit is contained in:
Hannes Janetzek 2014-10-09 10:39:35 +02:00
parent 113ea64297
commit 03f6f96988
4 changed files with 46 additions and 6 deletions

View File

@ -16,12 +16,13 @@
*/ */
package org.oscim.android.test; package org.oscim.android.test;
import static org.oscim.android.canvas.AndroidGraphics.drawableToBitmap;
import static org.oscim.tiling.source.bitmap.DefaultSources.STAMEN_TONER; import static org.oscim.tiling.source.bitmap.DefaultSources.STAMEN_TONER;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.oscim.android.canvas.AndroidGraphics; import org.oscim.backend.canvas.Bitmap;
import org.oscim.core.GeoPoint; import org.oscim.core.GeoPoint;
import org.oscim.layers.TileGridLayer; import org.oscim.layers.TileGridLayer;
import org.oscim.layers.marker.ItemizedLayer; import org.oscim.layers.marker.ItemizedLayer;
@ -37,6 +38,7 @@ import android.widget.Toast;
public class MarkerOverlayActivity extends BitmapTileMapActivity public class MarkerOverlayActivity extends BitmapTileMapActivity
implements OnItemGestureListener<MarkerItem> { implements OnItemGestureListener<MarkerItem> {
private static final boolean BILLBOARDS = true;
private MarkerSymbol mFocusMarker; private MarkerSymbol mFocusMarker;
public MarkerOverlayActivity() { public MarkerOverlayActivity() {
@ -47,11 +49,23 @@ public class MarkerOverlayActivity extends BitmapTileMapActivity
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Drawable d = getResources().getDrawable(R.drawable.marker_poi); /* directly load bitmap from resources */
MarkerSymbol symbol = AndroidGraphics.makeMarker(d, HotspotPlace.CENTER); Bitmap bitmap = drawableToBitmap(getResources(), R.drawable.marker_poi);
d = getResources().getDrawable(R.drawable.ic_launcher); MarkerSymbol symbol;
mFocusMarker = AndroidGraphics.makeMarker(d, HotspotPlace.BOTTOM_CENTER); if (BILLBOARDS)
symbol = new MarkerSymbol(bitmap, HotspotPlace.CENTER);
else
symbol = new MarkerSymbol(bitmap, 0.5f, 0.5f, false);
/* another option: use some bitmap drawable */
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
if (BILLBOARDS)
mFocusMarker = new MarkerSymbol(drawableToBitmap(d),
HotspotPlace.BOTTOM_CENTER);
else
mFocusMarker = new MarkerSymbol(drawableToBitmap(d),
0.5f, 0.5f, false);
ItemizedLayer<MarkerItem> markerLayer = ItemizedLayer<MarkerItem> markerLayer =
new ItemizedLayer<MarkerItem>(mMap, new ArrayList<MarkerItem>(), new ItemizedLayer<MarkerItem>(mMap, new ArrayList<MarkerItem>(),

View File

@ -93,6 +93,13 @@ public final class AndroidGraphics extends CanvasAdapter {
return new AndroidBitmap(bitmap); return new AndroidBitmap(bitmap);
} }
public static Bitmap drawableToBitmap(Resources res, int resId) {
return new AndroidBitmap(res.openRawResource(resId));
}
/**
* @deprecated
*/
public static MarkerSymbol makeMarker(Drawable drawable, HotspotPlace place) { public static MarkerSymbol makeMarker(Drawable drawable, HotspotPlace place) {
if (place == null) if (place == null)
place = HotspotPlace.CENTER; place = HotspotPlace.CENTER;
@ -100,6 +107,9 @@ public final class AndroidGraphics extends CanvasAdapter {
return new MarkerSymbol(drawableToBitmap(drawable), place); return new MarkerSymbol(drawableToBitmap(drawable), place);
} }
/**
* @deprecated
*/
public static MarkerSymbol makeMarker(Resources res, int resId, HotspotPlace place) { public static MarkerSymbol makeMarker(Resources res, int resId, HotspotPlace place) {
if (place == null) if (place == null)
place = HotspotPlace.CENTER; place = HotspotPlace.CENTER;

View File

@ -159,6 +159,7 @@ public class MarkerRenderer extends BucketRenderer {
SymbolItem s = SymbolItem.pool.get(); SymbolItem s = SymbolItem.pool.get();
s.set(it.x, it.y, marker.getBitmap(), true); s.set(it.x, it.y, marker.getBitmap(), true);
s.offset = marker.getHotspot(); s.offset = marker.getHotspot();
s.billboard = marker.isBillboard();
mSymbolLayer.pushSymbol(s); mSymbolLayer.pushSymbol(s);
} }

View File

@ -24,14 +24,25 @@ public class MarkerSymbol {
final Bitmap[] mBitmap; final Bitmap[] mBitmap;
/** Hotspot offset */ /** Hotspot offset */
final PointF mOffset; final PointF mOffset;
final boolean mBillboard;
public MarkerSymbol(Bitmap bitmap, float relX, float relY) { public MarkerSymbol(Bitmap bitmap, float relX, float relY) {
this(bitmap, relX, relY, true);
}
public MarkerSymbol(Bitmap bitmap, float relX, float relY, boolean billboard) {
mBitmap = new Bitmap[1]; mBitmap = new Bitmap[1];
mBitmap[0] = bitmap; mBitmap[0] = bitmap;
mOffset = new PointF(relX, relY); mOffset = new PointF(relX, relY);
mBillboard = billboard;
} }
public MarkerSymbol(Bitmap bitmap, HotspotPlace hotspot) { public MarkerSymbol(Bitmap bitmap, HotspotPlace hotspot) {
this(bitmap, hotspot, true);
}
public MarkerSymbol(Bitmap bitmap, HotspotPlace hotspot, boolean billboard) {
switch (hotspot) { switch (hotspot) {
case BOTTOM_CENTER: case BOTTOM_CENTER:
mOffset = new PointF(0.5f, 1); mOffset = new PointF(0.5f, 1);
@ -63,7 +74,11 @@ public class MarkerSymbol {
mBitmap = new Bitmap[1]; mBitmap = new Bitmap[1];
mBitmap[0] = bitmap; mBitmap[0] = bitmap;
mBillboard = billboard;
}
public boolean isBillboard() {
return mBillboard;
} }
public PointF getHotspot() { public PointF getHotspot() {
@ -75,6 +90,7 @@ public class MarkerSymbol {
} }
public boolean isInside(float dx, float dy) { public boolean isInside(float dx, float dy) {
/* TODO handle no-billboard */
int w = mBitmap[0].getWidth(); int w = mBitmap[0].getWidth();
int h = mBitmap[0].getHeight(); int h = mBitmap[0].getHeight();
float ox = -w * mOffset.x; float ox = -w * mOffset.x;
@ -82,5 +98,4 @@ public class MarkerSymbol {
return dx >= ox && dy >= oy && dx <= ox + w && dy <= oy + h; return dx >= ox && dy >= oy && dx <= ox + w && dy <= oy + h;
} }
} }