[android] #3276 - allowing multiple ViewAdapter implementations

This commit is contained in:
Tobrun
2016-05-13 12:27:37 +02:00
parent feb5832ee9
commit e50fb87e7b
7 changed files with 336 additions and 72 deletions

View File

@@ -457,7 +457,7 @@ public class MapView extends FrameLayout {
}
}
} else if (change == REGION_IS_CHANGING || change == REGION_DID_CHANGE) {
if (mMapboxMap.getMarkerViewAdapter() != null) {
if (mMapboxMap.getMarkerViewAdapters() != null) {
invalidateViewMarkers();
}
}

View File

@@ -4,6 +4,7 @@ import android.content.Context;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.graphics.Bitmap;
import android.location.Location;
import android.os.SystemClock;
@@ -46,6 +47,7 @@ import com.mapbox.mapboxsdk.layers.CustomLayer;
import com.mapbox.mapboxsdk.location.LocationListener;
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -77,7 +79,6 @@ public class MapboxMap {
private List<InfoWindow> mInfoWindows;
private MapboxMap.InfoWindowAdapter mInfoWindowAdapter;
private MapboxMap.MarkerViewAdapter mMarkerViewAdapter;
private OnMarkerViewClickListener mOnMarkerViewClickListener;
private int mMarkerViewItemAnimatorInRes;
private int mMarkerViewItemAnimatorOutRes;
@@ -102,7 +103,8 @@ public class MapboxMap {
private double mMaxZoomLevel = -1;
private double mMinZoomLevel = -1;
private Pools.SimplePool<View> viewSimplePool = new Pools.SimplePool<>(20);
private Pools.SimplePool<View> mViewReusePool = new Pools.SimplePool<>(20);
private List<MapboxMap.MarkerViewAdapter> mMarkerViewAdapters;
MapboxMap(@NonNull MapView mapView) {
mMapView = mapView;
@@ -112,6 +114,7 @@ public class MapboxMap {
mProjection = new Projection(mapView);
mAnnotations = new LongSparseArray<>();
mMarkerViews = new LongSparseArray<>();
mMarkerViewAdapters = new ArrayList<>();
mSelectedMarkers = new ArrayList<>();
mInfoWindows = new ArrayList<>();
}
@@ -667,60 +670,64 @@ public class MapboxMap {
// in bounds markers
List<Marker> inBoundsMarkers = result.getInBounds();
for (final Marker marker : inBoundsMarkers) {
convertView = viewSimplePool.acquire();
convertView = mViewReusePool.acquire();
Log.v("TAG", "Calling get view for " + marker.getId());
View adaptedView = mMarkerViewAdapter.getView(marker, convertView, mMapView);
if (adaptedView != null) {
// hack to hide old marker, todo replace with visibility
Icon icon = marker.getIcon();
if (!icon.getBitmap().equals(mViewMarkerBitmap)) {
if (mViewMarkerBitmap == null) {
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
mViewMarkerBitmap = Bitmap.createBitmap(icon.getBitmap().getWidth(), icon.getBitmap().getHeight(), conf);
}
marker.setIcon(IconFactory.recreate(icon.getId(), mViewMarkerBitmap));
}
if (mSelectedMarkers.contains(marker)) {
if (mMarkerViewItemAnimatorInRes != 0) {
Animator animator = AnimatorInflater.loadAnimator(mMapView.getContext(), mMarkerViewItemAnimatorInRes);
animator.setDuration(0);
animator.setTarget(convertView);
animator.start();
}
}
adaptedView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean clickHandled = false;
if (mOnMarkerViewClickListener != null) {
clickHandled = mOnMarkerViewClickListener.onMarkerClick(marker, v);
for (final MarkerViewAdapter adapter : mMarkerViewAdapters) {
if (adapter.getMarkerClass()==marker.getClass()) {
View adaptedView = adapter.getView(marker, convertView, mMapView);
if (adaptedView != null) {
// hack to hide old marker, todo replace with visibility
Icon icon = marker.getIcon();
if (!icon.getBitmap().equals(mViewMarkerBitmap)) {
if (mViewMarkerBitmap == null) {
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
mViewMarkerBitmap = Bitmap.createBitmap(icon.getBitmap().getWidth(), icon.getBitmap().getHeight(), conf);
}
marker.setIcon(IconFactory.recreate(icon.getId(), mViewMarkerBitmap));
}
if (!clickHandled) {
if (mSelectedMarkers.contains(marker)) {
if (mMarkerViewItemAnimatorInRes != 0) {
Animator animator = AnimatorInflater.loadAnimator(mMapView.getContext(), mMarkerViewItemAnimatorInRes);
animator.setTarget(v);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
selectMarker(marker);
}
});
animator.setDuration(0);
animator.setTarget(convertView);
animator.start();
} else {
selectMarker(marker);
}
}
}
});
mMarkerViews.append(marker.getId(), adaptedView);
if (convertView == null) {
mMapView.addView(adaptedView);
adaptedView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean clickHandled = false;
if (mOnMarkerViewClickListener != null) {
clickHandled = mOnMarkerViewClickListener.onMarkerClick(marker, v, adapter);
}
if (!clickHandled) {
if (mMarkerViewItemAnimatorInRes != 0) {
Animator animator = AnimatorInflater.loadAnimator(mMapView.getContext(), mMarkerViewItemAnimatorInRes);
animator.setTarget(v);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
selectMarker(marker);
}
});
animator.start();
} else {
selectMarker(marker);
}
}
}
});
mMarkerViews.append(marker.getId(), adaptedView);
if (convertView == null) {
mMapView.addView(adaptedView);
}
}
}
}
}
@@ -935,7 +942,7 @@ public class MapboxMap {
long[] ids = mMapView.addPolygons(polygons);
// if unit tests or polygons correcly added to map
// if unit tests or polygons correctly added to map
if (ids == null || ids.length == polygons.size()) {
long id = 0;
for (int i = 0; i < polygons.size(); i++) {
@@ -1030,7 +1037,7 @@ public class MapboxMap {
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
viewHolder.setVisibility(View.GONE);
viewSimplePool.release(viewHolder);
mViewReusePool.release(viewHolder);
}
});
}
@@ -1281,15 +1288,15 @@ public class MapboxMap {
return marker;
}
public void setMarkerViewAdapter(@Nullable MarkerViewAdapter markerViewAdapter) {
mMarkerViewAdapter = markerViewAdapter;
if (markerViewAdapter != null) {
public void addMarkerViewAdapter(@Nullable MarkerViewAdapter markerViewAdapter) {
if (!mMarkerViewAdapters.contains(markerViewAdapter)) {
mMarkerViewAdapters.add(markerViewAdapter);
mMapView.invalidateViewMarkers();
}
}
public MarkerViewAdapter getMarkerViewAdapter() {
return mMarkerViewAdapter;
public List<MarkerViewAdapter> getMarkerViewAdapters() {
return mMarkerViewAdapters;
}
public void setOnMarkerViewClickListener(@Nullable OnMarkerViewClickListener listener) {
@@ -1386,7 +1393,7 @@ public class MapboxMap {
/**
* Sets the distance from the edges of the map view’s frame to the edges of the map
* view’s logical viewport.
* <p>
* <p/>
* When the value of this property is equal to {0,0,0,0}, viewport
* properties such as `centerCoordinate` assume a viewport that matches the map
* view’s frame. Otherwise, those properties are inset, excluding part of the
@@ -1903,15 +1910,54 @@ public class MapboxMap {
View getInfoWindow(@NonNull Marker marker);
}
public interface MarkerViewAdapter<U extends Marker> {
public static abstract class MarkerViewAdapter<U extends Marker> {
private Class<U> persistentClass;
@SuppressWarnings("unchecked")
public MarkerViewAdapter(Context context) {
this.persistentClass = (Class<U>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
@AnimatorRes
private int animSelectOutRes;
@AnimatorRes
private int animSelectInRes;
@AnimatorRes
private int animEnterRes;
@AnimatorRes
private int animExitRes;
@Nullable
View getView(@NonNull U marker, @Nullable View convertView, @NonNull ViewGroup parent);
public abstract View getView(@NonNull U marker, @Nullable View convertView, @NonNull ViewGroup parent);
public int getAnimSelectOutRes() {
return animSelectOutRes;
}
public int getAnimSelectInRes() {
return animSelectInRes;
}
public int getAnimEnterRes() {
return animEnterRes;
}
public int getAnimExitRes() {
return animExitRes;
}
public Class<U> getMarkerClass(){
return persistentClass;
}
}
public interface OnMarkerViewClickListener {
boolean onMarkerClick(@NonNull Marker marker, @NonNull View view);
boolean onMarkerClick(@NonNull Marker marker, @NonNull View view, @NonNull MarkerViewAdapter adapter);
}
/**

View File

@@ -262,6 +262,14 @@
android:value="@string/category_annotation" />
</activity>
<activity
android:name=".activity.annotation.MultipleViewMarkerAdapterActivity"
android:description="@string/description_view_marker2"
android:label="@string/activity_view_marker2">
<meta-data
android:name="@string/category"
android:value="@string/category_annotation" />
</activity>
<!-- Configuration Settings -->
<meta-data

View File

@@ -89,12 +89,12 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
public void onClick(View v) {
if (mMapboxMap != null) {
fab.animate().alpha(0).start();
mMapboxMap.setMarkerViewAdapter(new TextAdapter(BulkMarkerActivity.this));
mMapboxMap.addMarkerViewAdapter(new TextAdapter(BulkMarkerActivity.this));
mMapView.addOnMapChangedListener(new MapView.OnMapChangedListener() {
@Override
public void onMapChanged(@MapView.MapChange int change) {
if (change == MapView.REGION_IS_CHANGING || change == MapView.REGION_DID_CHANGE) {
if (mMapboxMap.getMarkerViewAdapter() != null) {
if (!mMapboxMap.getMarkerViewAdapters().isEmpty()) {
viewCountView.setText("ViewCache size " + (mMapView.getChildCount()-5));
}
}
@@ -104,7 +104,7 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
mMapboxMap.setMarkerViewItemAnimation(R.animator.scale_up, R.animator.scale_down);
mMapboxMap.setOnMarkerViewClickListener(new MapboxMap.OnMarkerViewClickListener() {
@Override
public boolean onMarkerClick(@NonNull final Marker marker, @NonNull View view) {
public boolean onMarkerClick(@NonNull Marker marker, @NonNull View view, @NonNull MapboxMap.MarkerViewAdapter adapter) {
Toast.makeText(BulkMarkerActivity.this, "Hello "+marker.getId(), Toast.LENGTH_SHORT).show();
return false;
}
@@ -115,11 +115,12 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
}
}
public static class TextAdapter implements MapboxMap.MarkerViewAdapter<Marker> {
public static class TextAdapter extends MapboxMap.MarkerViewAdapter<Marker> {
private LayoutInflater inflater;
public TextAdapter(@NonNull Context context) {
super(context);
this.inflater = LayoutInflater.from(context);
}

View File

@@ -0,0 +1,203 @@
package com.mapbox.mapboxsdk.testapp.activity.annotation;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.model.annotations.CountryMarker;
import com.mapbox.mapboxsdk.testapp.model.annotations.CountryMarkerOptions;
public class MultipleViewMarkerAdapterActivity extends AppCompatActivity {
private MapboxMap mMapboxMap;
private MapView mMapView;
private final static LatLng[] LAT_LNGS = new LatLng[]{
new LatLng(38.907327, -77.041293),
new LatLng(38.909698, -77.029642),
new LatLng(38.907227, -77.036530),
new LatLng(38.905607, -77.031916),
new LatLng(38.897424, -77.036508),
new LatLng(38.897642, -77.041980),
new LatLng(38.889876, -77.008849),
new LatLng(38.889441, -77.050134),
new LatLng(38.902580, -77.050102)};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marker_bulk);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
final TextView viewCountView = (TextView) findViewById(R.id.countView);
mMapView = (MapView) findViewById(R.id.mapView);
mMapView.setAccessToken(getString(R.string.mapbox_access_token));
mMapView.onCreate(savedInstanceState);
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
mMapboxMap = mapboxMap;
mMapboxMap.addMarker(new CountryMarkerOptions().title("United States").abbrevName("us").flagRes(R.drawable.ic_us).position(new LatLng(38.899774, -77.023237)));
mMapboxMap.addMarkerViewAdapter(new TextAdapter(MultipleViewMarkerAdapterActivity.this));
mMapboxMap.addMarkerViewAdapter(new CountryAdapter(MultipleViewMarkerAdapterActivity.this));
for (int i = 0; i < LAT_LNGS.length; i++) {
mMapboxMap.addMarker(new MarkerOptions().position(LAT_LNGS[i]).title(String.valueOf(i)));
}
mMapView.addOnMapChangedListener(new MapView.OnMapChangedListener() {
@Override
public void onMapChanged(@MapView.MapChange int change) {
if (change == MapView.REGION_IS_CHANGING || change == MapView.REGION_DID_CHANGE) {
if (!mMapboxMap.getMarkerViewAdapters().isEmpty()) {
viewCountView.setText("ViewCache size " + (mMapView.getChildCount() - 5));
}
}
}
});
mMapboxMap.setMarkerViewItemAnimation(R.animator.scale_up, R.animator.scale_down);
mMapboxMap.setOnMarkerViewClickListener(new MapboxMap.OnMarkerViewClickListener() {
@Override
public boolean onMarkerClick(@NonNull Marker marker, @NonNull View view, @NonNull MapboxMap.MarkerViewAdapter adapter) {
Toast.makeText(MultipleViewMarkerAdapterActivity.this, "Hello " + marker.getId(), Toast.LENGTH_SHORT).show();
return false;
}
});
}
});
}
private static class TextAdapter extends MapboxMap.MarkerViewAdapter<Marker> {
private LayoutInflater inflater;
public TextAdapter(@NonNull Context context) {
super(context);
this.inflater = LayoutInflater.from(context);
}
@Nullable
@Override
public View getView(@NonNull Marker marker, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.view_text_marker, parent, false);
viewHolder.title = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.title.setText(marker.getTitle());
return convertView;
}
private static class ViewHolder {
TextView title;
}
}
private static class CountryAdapter extends MapboxMap.MarkerViewAdapter<CountryMarker> {
private LayoutInflater inflater;
public CountryAdapter(@NonNull Context context) {
super(context);
this.inflater = LayoutInflater.from(context);
}
@Nullable
@Override
public View getView(@NonNull CountryMarker marker, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.view_custom_marker, parent, false);
viewHolder.flag = (ImageView) convertView.findViewById(R.id.imageView);
viewHolder.abbrev = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.flag.setImageResource(marker.getFlagRes());
viewHolder.abbrev.setText(marker.getAbbrevName());
return convertView;
}
private static class ViewHolder {
ImageView flag;
TextView abbrev;
}
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mMapView.onSaveInstanceState(outState);
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

View File

@@ -65,29 +65,33 @@ public class ViewMarkerActivity extends AppCompatActivity implements OnMapReadyC
final List<Marker> markers = mapboxMap.addMarkers(countries);
// Add view marker adapter
mapboxMap.setMarkerViewAdapter(new CountryAdapter(this));
mapboxMap.addMarkerViewAdapter(new CountryAdapter(this));
mapboxMap.setOnMarkerViewClickListener(new MapboxMap.OnMarkerViewClickListener() {
@Override
public boolean onMarkerClick(@NonNull Marker marker, @NonNull View view) {
public boolean onMarkerClick(@NonNull Marker marker, @NonNull View view, @NonNull MapboxMap.MarkerViewAdapter adapter) {
Log.d(MapboxConstants.TAG, "Country clicked " + ((CountryMarker) marker).getAbbrevName());
return true;
}
});
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mapboxMap.removeAnnotation(markers.get(2));
}
});
View view = findViewById(R.id.fab);
if(view!=null) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mapboxMap.removeAnnotation(markers.get(2));
}
});
}
}
private static class CountryAdapter implements MapboxMap.MarkerViewAdapter<CountryMarker> {
private static class CountryAdapter extends MapboxMap.MarkerViewAdapter<CountryMarker> {
private LayoutInflater inflater;
public CountryAdapter(@NonNull Context context) {
super(context);
this.inflater = LayoutInflater.from(context);
}

View File

@@ -18,6 +18,7 @@
<string name="activity_polygon">Polygon</string>
<string name="activity_press_for_marker">Press Map For Marker</string>
<string name="activity_view_marker">View Marker API</string>
<string name="activity_view_marker2">View Marker AP2</string>
<!-- InfoWindow-->
<string name="activity_info_window">Standard InfoWindow example</string>
@@ -78,6 +79,7 @@
<string name="description_snapshot">Example to make a snapshot of the map</string>
<string name="description_doublemap">2 maps in a view hierarchy</string>
<string name="description_view_marker">Use an Android SDK View as marker</string>
<string name="description_view_marker2">Use an Android SDK View as marker2</string>
<string name="menuitem_title_concurrent_infowindow">Concurrent Open InfoWindows</string>r
<string name="menuitem_title_tracking_mode_dismiss_on_gesture">Dismiss location tracking on gesture</string>