[android] Add SpriteFactory.drawable for custom markers

Used in InfoWindowAdapter activity
Fixes #2744
This commit is contained in:
Leith Bade
2015-10-23 16:30:29 +11:00
parent 54762825b0
commit 267e33ba5a
8 changed files with 47 additions and 8 deletions

View File

@@ -4,8 +4,6 @@ import android.support.annotation.NonNull;
import com.mapbox.mapboxsdk.views.MapView;
import java.lang.ref.WeakReference;
public abstract class Annotation implements Comparable<Annotation> {
/**
@@ -14,7 +12,7 @@ public abstract class Annotation implements Comparable<Annotation> {
* Internal C++ id is stored as unsigned int.
*/
private long id = -1; // -1 unless added to a MapView
private WeakReference<MapView> mapView;
private MapView mapView;
protected Annotation() {
}
@@ -27,10 +25,10 @@ public abstract class Annotation implements Comparable<Annotation> {
}
public void remove() {
if ((mapView == null) || (mapView.get() == null)) {
if (mapView == null) {
return;
}
mapView.get().removeAnnotation(this);
mapView.removeAnnotation(this);
}
/**
@@ -44,14 +42,14 @@ public abstract class Annotation implements Comparable<Annotation> {
* Do not use this method. Used internally by the SDK.
*/
public void setMapView(MapView mapView) {
this.mapView = new WeakReference<>(mapView);
this.mapView = mapView;
}
protected MapView getMapView() {
if (mapView == null) {
return null;
}
return mapView.get();
return mapView;
}
@Override

View File

@@ -3,6 +3,9 @@ package com.mapbox.mapboxsdk.annotations;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.WindowManager;
@@ -20,7 +23,6 @@ public final class SpriteFactory {
private static final String SPRITE_ID_PREFIX = "com.mapbox.sprites.sprite_";
// TODO make weak
private MapView mMapView;
private Sprite mDefaultMarker;
private BitmapFactory.Options mOptions;
@@ -62,6 +64,30 @@ public final class SpriteFactory {
return new Sprite(id, bitmap);
}
public Sprite fromDrawable(Drawable drawable) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
return fromDrawable(drawable, width, height);
}
public Sprite fromDrawable(Drawable drawable, int width, int height) {
if ((width < 0) || (height < 0)) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Rect temp = drawable.getBounds();
Rect bounds = new Rect(0, 0, width, height);
drawable.setBounds(bounds);
drawable.draw(canvas);
drawable.setBounds(temp);
return fromBitmap(bitmap);
}
public Sprite fromResource(int resourceId) {
Bitmap bitmap = BitmapFactory.decodeResource(mMapView.getResources(), resourceId);
return fromBitmap(bitmap);