Skip to content

Commit

Permalink
[android] Add SpriteFactory.drawable for custom markers
Browse files Browse the repository at this point in the history
Used in InfoWindowAdapter activity
Fixes mapbox#2744
  • Loading branch information
Leith Bade committed Oct 23, 2015
1 parent 89c9d62 commit faba6d3
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import com.mapbox.mapboxsdk.views.MapView;

import java.lang.ref.WeakReference;

public abstract class Annotation implements Comparable<Annotation> {

/**
Expand All @@ -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() {
}
Expand All @@ -27,10 +25,10 @@ public long getId() {
}

public void remove() {
if ((mapView == null) || (mapView.get() == null)) {
if (mapView == null) {
return;
}
mapView.get().removeAnnotation(this);
mapView.removeAnnotation(this);
}

/**
Expand All @@ -44,14 +42,14 @@ public void setId(long id) {
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
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;
Expand All @@ -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;
Expand Down Expand Up @@ -62,6 +64,30 @@ public Sprite fromBitmap(Bitmap bitmap) {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,6 +15,8 @@

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;
Expand All @@ -20,6 +25,8 @@
public class InfoWindowAdapterActivity extends AppCompatActivity {

private MapView mMapView;
private SpriteFactory mSpriteFactory;
private Drawable mIconDrawable;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -38,6 +45,10 @@ protected void onCreate(Bundle savedInstanceState) {
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);
Expand Down Expand Up @@ -66,6 +77,10 @@ private MarkerOptions generateMarker(String title, double lat, double lng, Strin
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;
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit faba6d3

Please sign in to comment.