[android] Add SpriteFactory.drawable for custom markers
Used in InfoWindowAdapter activity Fixes #2744
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.mapbox.mapboxsdk.testapp;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
@@ -12,6 +15,8 @@ import android.widget.TextView;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Sprite;
|
||||
import com.mapbox.mapboxsdk.annotations.SpriteFactory;
|
||||
import com.mapbox.mapboxsdk.constants.Style;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.utils.ApiAccess;
|
||||
@@ -20,6 +25,8 @@ import com.mapbox.mapboxsdk.views.MapView;
|
||||
public class InfoWindowAdapterActivity extends AppCompatActivity {
|
||||
|
||||
private MapView mMapView;
|
||||
private SpriteFactory mSpriteFactory;
|
||||
private Drawable mIconDrawable;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -38,6 +45,10 @@ public class InfoWindowAdapterActivity extends AppCompatActivity {
|
||||
mMapView = (MapView) findViewById(R.id.mapView);
|
||||
mMapView.setAccessToken(ApiAccess.getToken(this));
|
||||
mMapView.onCreate(savedInstanceState);
|
||||
|
||||
mSpriteFactory = new SpriteFactory(mMapView);
|
||||
mIconDrawable = ContextCompat.getDrawable(this, R.drawable.ic_location_city_black_48dp);
|
||||
|
||||
mMapView.setInfoWindowAdapter(new MapView.InfoWindowAdapter() {
|
||||
|
||||
private int tenDp = (int) getResources().getDimension(R.dimen.attr_margin);
|
||||
@@ -66,6 +77,10 @@ public class InfoWindowAdapterActivity extends AppCompatActivity {
|
||||
marker.title(title);
|
||||
marker.snippet(color);
|
||||
marker.position(new LatLng(lat, lng));
|
||||
|
||||
mIconDrawable.setColorFilter(Color.parseColor(marker.getSnippet()), PorterDuff.Mode.SRC_IN);
|
||||
Sprite icon = mSpriteFactory.fromDrawable(mIconDrawable);
|
||||
marker.icon(icon);
|
||||
return marker;
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 172 B |
Binary file not shown.
|
After Width: | Height: | Size: 147 B |
Binary file not shown.
|
After Width: | Height: | Size: 190 B |
Binary file not shown.
|
After Width: | Height: | Size: 230 B |
Binary file not shown.
|
After Width: | Height: | Size: 255 B |
Reference in New Issue
Block a user