diff options
author | Tobrun <tobrun.van.nuland@gmail.com> | 2016-05-23 10:18:20 +0200 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2016-05-23 12:36:55 +0200 |
commit | 40bb02fe749ea8221a8da57b715bcbc1866edb8b (patch) | |
tree | e7044feb5792d12caf9dc04d235ec9e25f2273c5 /platform | |
parent | 2865e7d35c92acc247937242821f7823599f623c (diff) | |
download | qtlocation-mapboxgl-40bb02fe749ea8221a8da57b715bcbc1866edb8b.tar.gz |
[android] - add MarkerViewManager and MarkerViewTransformer
Diffstat (limited to 'platform')
7 files changed, 361 insertions, 247 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerView.java index 63bb212cdf..308ad602e1 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerView.java @@ -16,6 +16,9 @@ public class MarkerView extends Marker { private float tiltValue; private float rotation; + private float alpha; + + private MarkerViewTransformer markerViewTransformer; MarkerView() { centerOffset = new PointF(); @@ -81,14 +84,30 @@ public class MarkerView extends Marker { public void setRotation(float rotation) { this.rotation = rotation; - - MapboxMap mapboxMap = getMapboxMap(); - if (mapboxMap != null) { - mapboxMap.setMarkerViewRotation(this, rotation); + if (markerViewTransformer != null) { + markerViewTransformer.animateRotation(this, rotation); } } public float getRotation() { return rotation; } + + public float getAlpha() { + return alpha; + } + + public void setAlpha(float alpha) { + this.alpha = alpha; + if (markerViewTransformer != null) { + markerViewTransformer.animateAlpha(this, rotation); + } + } + + @Override + public void setMapboxMap(MapboxMap mapboxMap) { + super.setMapboxMap(mapboxMap); + MarkerViewManager manager = mapboxMap.getMarkerViewManager(); + markerViewTransformer = manager.getMarkerViewTransformer(); + } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerViewManager.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerViewManager.java new file mode 100644 index 0000000000..8bd17a75af --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerViewManager.java @@ -0,0 +1,228 @@ +package com.mapbox.mapboxsdk.annotations; + +import android.animation.Animator; +import android.animation.AnimatorInflater; +import android.animation.AnimatorListenerAdapter; +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Point; +import android.os.SystemClock; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.util.Pools; +import android.support.v4.view.animation.FastOutSlowInInterpolator; +import android.view.View; + +import com.mapbox.mapboxsdk.constants.MapboxConstants; +import com.mapbox.mapboxsdk.maps.MapView; +import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.maps.Projection; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public class MarkerViewManager { + + private MapboxMap mapboxMap; + private MapView mapView; + private Context context; + private List<MapboxMap.MarkerViewAdapter> markerViewAdapters; + private MarkerViewTransformer markerViewTransformer; + private long mViewMarkerBoundsUpdateTime; + private MapboxMap.OnMarkerViewClickListener onMarkerViewClickListener; + private Bitmap mViewMarkerBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); + + public MarkerViewManager(@NonNull MapboxMap mapboxMap, @NonNull MapView mapView) { + this.mapboxMap = mapboxMap; + this.markerViewAdapters = new ArrayList<>(); + this.markerViewTransformer = new MarkerViewTransformer(); + this.mapView = mapView; + this.context = mapView.getContext().getApplicationContext(); + } + + public void deselect(@NonNull MarkerView marker) { + final View viewMarker = markerViewTransformer.getMarkerViewMap().get(marker); + if (viewMarker != null) { + int deselectAnimatorRes = marker.getDeselectAnimRes(); + if (deselectAnimatorRes != 0) { + viewMarker.setLayerType(View.LAYER_TYPE_HARDWARE, null); + Animator animator = AnimatorInflater.loadAnimator(context, deselectAnimatorRes); + animator.addListener(new AnimatorListenerAdapter() { + @Override + public void onAnimationEnd(Animator animation) { + super.onAnimationEnd(animation); + viewMarker.setLayerType(View.LAYER_TYPE_NONE, null); + } + }); + animator.setTarget(viewMarker); + animator.start(); + } + } + } + + public void removeMarkerView(MarkerView marker, boolean removeFromMap) { + final View viewHolder = markerViewTransformer.getMarkerViewMap().get(marker); + if (viewHolder != null && marker != null) { + for (final MapboxMap.MarkerViewAdapter<?> adapter : markerViewAdapters) { + if (adapter.getMarkerClass() == marker.getClass()) { + + // get pool of Views associated to an adapter + final Pools.SimplePool<View> viewPool = adapter.getViewReusePool(); + + // cancel ongoing animations + viewHolder.animate().cancel(); + viewHolder.setAlpha(1); + + // animate alpha + viewHolder.animate() + .alpha(0) + .setDuration(MapboxConstants.ANIMATION_DURATION_SHORT) + .setInterpolator(new FastOutSlowInInterpolator()) + .setListener(new AnimatorListenerAdapter() { + + @Override + public void onAnimationEnd(Animator animation) { + super.onAnimationEnd(animation); + viewHolder.setVisibility(View.GONE); + viewPool.release(viewHolder); + } + }); + } + } + } + if (removeFromMap) { + markerViewTransformer.getMarkerViewMap().remove(marker); + } + } + + public MarkerViewTransformer getMarkerViewTransformer() { + return markerViewTransformer; + } + + public void addMarkerViewAdapter(@Nullable MapboxMap.MarkerViewAdapter markerViewAdapter) { + if (!markerViewAdapters.contains(markerViewAdapter)) { + markerViewAdapters.add(markerViewAdapter); + invalidateViewMarkersInBounds(); + } + } + + public void setOnMarkerViewClickListener(@Nullable MapboxMap.OnMarkerViewClickListener listener) { + onMarkerViewClickListener = listener; + } + + public List<MapboxMap.MarkerViewAdapter> getMarkerViewAdapters() { + return markerViewAdapters; + } + + public void invalidateViewMarkers() { + if (!markerViewAdapters.isEmpty()) { + long currentTime = SystemClock.elapsedRealtime(); + if (currentTime < mViewMarkerBoundsUpdateTime) { + return; + } + invalidateViewMarkersInBounds(); + mViewMarkerBoundsUpdateTime = currentTime + 250; + } + } + + public void invalidateViewMarkersInBounds() { + Projection projection = mapboxMap.getProjection(); + List<MarkerView> markers = mapView.getMarkerViewsInBounds(projection.getVisibleRegion().latLngBounds); + Map<MarkerView, View> markerView = markerViewTransformer.getMarkerViewMap(); + View convertView; + + // remove old markers + Iterator<MarkerView> iterator = markerView.keySet().iterator(); + while (iterator.hasNext()) { + MarkerView m = iterator.next(); + if (!markers.contains(m)) { + // remove marker + convertView = markerView.get(m); + int deselectAnimRes = m.getDeselectAnimRes(); + if (deselectAnimRes != 0) { + Animator animator = AnimatorInflater.loadAnimator(context, deselectAnimRes); + animator.setDuration(0); + animator.setTarget(convertView); + animator.start(); + } + removeMarkerView(m, false); + iterator.remove(); + } + } + + // introduce new markers + for (final MarkerView marker : markers) { + if (!markerView.containsKey(marker)) { + for (final MapboxMap.MarkerViewAdapter adapter : markerViewAdapters) { + if (adapter.getMarkerClass() == marker.getClass()) { + convertView = (View) adapter.getViewReusePool().acquire(); + View adaptedView = adapter.getView(marker, convertView, mapView); + + // InfoWindow offset + Point infoWindowOffset = marker.getInfoWindowOffset(); + marker.setTopOffsetPixels(-infoWindowOffset.y); + marker.setRightOffsetPixels(infoWindowOffset.x); + + if (adaptedView != null) { + + // tilt + adaptedView.setRotationX(marker.getTiltValue()); + + // rotation + adaptedView.setRotation(marker.getRotation()); + + if (mapboxMap.getSelectedMarkers().contains(marker)) { + // if a marker to be shown was selected + // replay that animation with duration 0 + int selectAnimRes = marker.getSelectAnimRes(); + if (selectAnimRes != 0) { + Animator animator = AnimatorInflater.loadAnimator(mapView.getContext(), selectAnimRes); + animator.setDuration(0); + animator.setTarget(convertView); + animator.start(); + } + } + + final int animSelectRes = marker.getSelectAnimRes(); + adaptedView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(final View v) { + boolean clickHandled = false; + if (onMarkerViewClickListener != null) { + clickHandled = onMarkerViewClickListener.onMarkerClick(marker, v, adapter); + } + + if (!clickHandled) { + if (animSelectRes != 0) { + v.setLayerType(View.LAYER_TYPE_HARDWARE, null); + Animator animator = AnimatorInflater.loadAnimator(context, animSelectRes); + animator.setTarget(v); + animator.addListener(new AnimatorListenerAdapter() { + @Override + public void onAnimationEnd(Animator animation) { + super.onAnimationEnd(animation); + mapboxMap.selectMarker(marker); + v.setLayerType(View.LAYER_TYPE_NONE, null); + } + }); + animator.start(); + } else { + mapboxMap.selectMarker(marker); + } + } + } + }); + + markerView.put(marker, adaptedView); + if (convertView == null) { + mapView.addView(adaptedView); + } + } + } + } + } + } + } +} diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerViewTransformer.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerViewTransformer.java new file mode 100644 index 0000000000..1c958d396d --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerViewTransformer.java @@ -0,0 +1,76 @@ +package com.mapbox.mapboxsdk.annotations; + +import android.animation.Animator; +import android.animation.AnimatorListenerAdapter; +import android.animation.ObjectAnimator; +import android.support.annotation.NonNull; +import android.view.View; + +import java.util.HashMap; +import java.util.Map; + +public class MarkerViewTransformer { + + private Map<MarkerView, View> mMarkerViewMap; + + public MarkerViewTransformer() { + mMarkerViewMap = new HashMap<>(); + } + + /** + * Animate the rotation of a marker view. + * + * @param markerView + * @param rotation + */ + public void animateRotation(@NonNull MarkerView markerView, float rotation) { + final View convertView = mMarkerViewMap.get(markerView); + if (convertView != null) { + convertView.setLayerType(View.LAYER_TYPE_HARDWARE, null); + ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(convertView, View.ROTATION, convertView.getRotation(), rotation); + rotateAnimator.addListener(new AnimatorListenerAdapter() { + @Override + public void onAnimationEnd(Animator animation) { + super.onAnimationEnd(animation); + convertView.setLayerType(View.LAYER_TYPE_NONE, null); + } + }); + rotateAnimator.start(); + } + } + + public void animateAlpha(@NonNull MarkerView markerView, float alpha) { + final View convertView = mMarkerViewMap.get(markerView); + if (convertView != null) { + convertView.setLayerType(View.LAYER_TYPE_HARDWARE, null); + ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(convertView, View.ALPHA, convertView.getAlpha(), alpha); + rotateAnimator.addListener(new AnimatorListenerAdapter() { + @Override + public void onAnimationEnd(Animator animation) { + super.onAnimationEnd(animation); + convertView.setLayerType(View.LAYER_TYPE_NONE, null); + } + }); + rotateAnimator.start(); + } + } + + /** + * Set tilt to marker views in the viewport. + * + * @param tilt + */ + public void setTilt(float tilt) { + for (MarkerView markerView : mMarkerViewMap.keySet()) { + if (markerView.isFlat()) { + markerView.setTiltValue(tilt); + mMarkerViewMap.get(markerView).setRotationX(tilt); + } + } + } + + public Map<MarkerView, View> getMarkerViewMap() { + return mMarkerViewMap; + } + +} diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index f2ad66b18c..e36429fc30 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -26,7 +26,6 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.os.Bundle; -import android.os.SystemClock; import android.support.annotation.CallSuper; import android.support.annotation.FloatRange; import android.support.annotation.IntDef; @@ -158,7 +157,6 @@ public class MapView extends FrameLayout { private StyleInitializer mStyleInitializer; private List<OnMapReadyCallback> mOnMapReadyCallbackList; - private long mViewMarkerBoundsUpdateTime; @UiThread public MapView(@NonNull Context context) { @@ -452,12 +450,8 @@ public class MapView extends FrameLayout { iterator.remove(); } } - } else if (change == REGION_IS_CHANGING || change == REGION_DID_CHANGE) { - if (!mMapboxMap.getMarkerViewAdapters().isEmpty()) { - invalidateViewMarkers(); - } - }else if(change== DID_FINISH_LOADING_MAP){ - invalidateViewMarkers(); + } else if (change == REGION_IS_CHANGING || change == REGION_DID_CHANGE || change == DID_FINISH_LOADING_MAP) { + mMapboxMap.getMarkerViewManager().invalidateViewMarkers(); } } }); @@ -471,15 +465,6 @@ public class MapView extends FrameLayout { } } - void invalidateViewMarkers() { - long currentTime = SystemClock.elapsedRealtime(); - if (currentTime < mViewMarkerBoundsUpdateTime) { - return; - } - mMapboxMap.invalidateViewMarkersInBounds(); - mViewMarkerBoundsUpdateTime = currentTime + 250; - } - /** * You must call this method from the parent's {@link android.app.Activity#onSaveInstanceState(Bundle)} * or {@link android.app.Fragment#onSaveInstanceState(Bundle)}. @@ -888,7 +873,7 @@ public class MapView extends FrameLayout { * <p/> * Returns the current Mapbox access token used to load map styles and tiles. * </p> - * + * * @return The current Mapbox access token. * @deprecated As of release 4.1.0, replaced by {@link MapboxAccountManager#getAccessToken()} */ @@ -1110,7 +1095,7 @@ public class MapView extends FrameLayout { return new ArrayList<>(annotations); } - List<MarkerView> getMarkerViewsInBounds(@NonNull LatLngBounds bbox) { + public List<MarkerView> getMarkerViewsInBounds(@NonNull LatLngBounds bbox) { if (mDestroyed || bbox == null) { return new ArrayList<>(); } @@ -1378,7 +1363,7 @@ public class MapView extends FrameLayout { mCompassView.update(getDirection()); mMyLocationView.update(); - Map<MarkerView, View> viewMarkers = mMapboxMap.getMarkerViewMap(); + Map<MarkerView, View> viewMarkers = mMapboxMap.getMarkerViewManager().getMarkerViewTransformer().getMarkerViewMap(); for (Marker marker : viewMarkers.keySet()) { mViewHolder = viewMarkers.get(marker); if (mViewHolder != null) { @@ -1698,7 +1683,7 @@ public class MapView extends FrameLayout { if (annotation.getId() == newSelectedMarkerId) { if (selectedMarkers.isEmpty() || !selectedMarkers.contains(annotation)) { // only handle click if no marker view is available - if (mMapboxMap.getMarkerViewMap().get(annotation) == null) { + if (!(annotation instanceof MarkerView)) { mMapboxMap.selectMarker((Marker) annotation); } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java index 74ecfcb9f8..bbf8307b56 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java @@ -1,12 +1,7 @@ package com.mapbox.mapboxsdk.maps; -import android.animation.ObjectAnimator; import android.content.Context; -import android.animation.Animator; -import android.animation.AnimatorInflater; -import android.animation.AnimatorListenerAdapter; import android.graphics.Bitmap; -import android.graphics.Point; import android.location.Location; import android.os.SystemClock; import android.support.annotation.FloatRange; @@ -15,15 +10,12 @@ import android.support.annotation.Nullable; import android.support.annotation.UiThread; import android.support.v4.util.LongSparseArray; import android.support.v4.util.Pools; -import android.support.v4.view.animation.FastOutSlowInInterpolator; import android.text.TextUtils; import android.util.Log; import android.view.View; - -import com.mapbox.mapboxsdk.MapboxAccountManager; - import android.view.ViewGroup; +import com.mapbox.mapboxsdk.MapboxAccountManager; import com.mapbox.mapboxsdk.annotations.Annotation; import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions; import com.mapbox.mapboxsdk.annotations.BaseMarkerViewOptions; @@ -33,6 +25,7 @@ import com.mapbox.mapboxsdk.annotations.InfoWindow; import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.annotations.MarkerOptions; import com.mapbox.mapboxsdk.annotations.MarkerView; +import com.mapbox.mapboxsdk.annotations.MarkerViewManager; import com.mapbox.mapboxsdk.annotations.Polygon; import com.mapbox.mapboxsdk.annotations.PolygonOptions; import com.mapbox.mapboxsdk.annotations.Polyline; @@ -51,10 +44,7 @@ import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings; import java.lang.reflect.ParameterizedType; import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.concurrent.TimeUnit; /** @@ -78,12 +68,10 @@ public class MapboxMap { private LongSparseArray<Annotation> mAnnotations; private List<Marker> mSelectedMarkers; - private Map<MarkerView, View> mMarkerViewMap; + private MarkerViewManager mMarkerViewManager; private List<InfoWindow> mInfoWindows; private MapboxMap.InfoWindowAdapter mInfoWindowAdapter; - - private OnMarkerViewClickListener mOnMarkerViewClickListener; private Bitmap mViewMarkerBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); private boolean mMyLocationEnabled; @@ -105,8 +93,6 @@ public class MapboxMap { private double mMaxZoomLevel = -1; private double mMinZoomLevel = -1; - private List<MapboxMap.MarkerViewAdapter> mMarkerViewAdapters; - MapboxMap(@NonNull MapView mapView) { mMapView = mapView; mMapView.addOnMapChangedListener(new MapChangeCameraPositionListener()); @@ -114,10 +100,9 @@ public class MapboxMap { mTrackingSettings = new TrackingSettings(mMapView, mUiSettings); mProjection = new Projection(mapView); mAnnotations = new LongSparseArray<>(); - mMarkerViewAdapters = new ArrayList<>(); mSelectedMarkers = new ArrayList<>(); mInfoWindows = new ArrayList<>(); - mMarkerViewMap = new HashMap<>(); + mMarkerViewManager = new MarkerViewManager(this, mapView); } // @@ -651,110 +636,8 @@ public class MapboxMap { // Annotations // - void invalidateViewMarkersInBounds() { - List<MarkerView> markers = mMapView.getMarkerViewsInBounds(mProjection.getVisibleRegion().latLngBounds); - View convertView; - - // remove old markers - Iterator<MarkerView> iterator = mMarkerViewMap.keySet().iterator(); - while (iterator.hasNext()) { - MarkerView m = iterator.next(); - if (!markers.contains(m)) { - // remove marker - convertView = mMarkerViewMap.get(m); - int deselectAnimRes = m.getDeselectAnimRes(); - if (deselectAnimRes != 0) { - Animator animator = AnimatorInflater.loadAnimator(mMapView.getContext(), deselectAnimRes); - animator.setDuration(0); - animator.setTarget(convertView); - animator.start(); - } - removeMarkerView(m); - iterator.remove(); - } - } - - // introduce new markers - for (final MarkerView marker : markers) { - if (!mMarkerViewMap.containsKey(marker)) { - for (final MarkerViewAdapter adapter : mMarkerViewAdapters) { - if (adapter.getMarkerClass() == marker.getClass()) { - convertView = (View) adapter.getViewReusePool().acquire(); - View adaptedView = adapter.getView(marker, convertView, mMapView); - - // InfoWindow offset - Point infoWindowOffset = marker.getInfoWindowOffset(); - marker.setTopOffsetPixels(-infoWindowOffset.y); - marker.setRightOffsetPixels(infoWindowOffset.x); - - if (adaptedView != null) { - - // tilt - adaptedView.setRotationX(marker.getTiltValue()); - - // rotation - adaptedView.setRotation(marker.getRotation()); - - if (mSelectedMarkers.contains(marker)) { - // if a marker to be shown was selected - // replay that animation with duration 0 - int selectAnimRes = marker.getSelectAnimRes(); - if (selectAnimRes != 0) { - Animator animator = AnimatorInflater.loadAnimator(mMapView.getContext(), selectAnimRes); - animator.setDuration(0); - animator.setTarget(convertView); - animator.start(); - } - } - - final int animSelectRes = marker.getSelectAnimRes(); - adaptedView.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(final View v) { - boolean clickHandled = false; - if (mOnMarkerViewClickListener != null) { - clickHandled = mOnMarkerViewClickListener.onMarkerClick(marker, v, adapter); - } - - if (!clickHandled) { - if (animSelectRes != 0) { - v.setLayerType(View.LAYER_TYPE_HARDWARE, null); - Animator animator = AnimatorInflater.loadAnimator(mMapView.getContext(), animSelectRes); - animator.setTarget(v); - animator.addListener(new AnimatorListenerAdapter() { - @Override - public void onAnimationEnd(Animator animation) { - super.onAnimationEnd(animation); - selectMarker(marker); - v.setLayerType(View.LAYER_TYPE_NONE, null); - } - }); - animator.start(); - } else { - selectMarker(marker); - } - } - } - }); - - mMarkerViewMap.put(marker, adaptedView); - if (convertView == null) { - mMapView.addView(adaptedView); - } - } - } - } - } - } - } - void setTilt(double tilt) { - for (MarkerView markerView : mMarkerViewMap.keySet()) { - if (markerView.isFlat()) { - markerView.setTiltValue((float) tilt); - mMarkerViewMap.get(markerView).setRotationX((float) tilt); - } - } + mMarkerViewManager.getMarkerViewTransformer().setTilt((float) tilt); mMapView.setTilt(tilt); } @@ -914,7 +797,7 @@ public class MapboxMap { } } } - invalidateViewMarkersInBounds(); + mMarkerViewManager.invalidateViewMarkersInBounds(); return markers; } @@ -1110,46 +993,15 @@ public class MapboxMap { if (annotation instanceof Marker) { Marker marker = (Marker) annotation; marker.hideInfoWindow(); - removeMarkerView(marker); - mMarkerViewMap.remove(marker); + if (marker instanceof MarkerView) { + mMarkerViewManager.removeMarkerView((MarkerView) marker, true); + } } long id = annotation.getId(); mMapView.removeAnnotation(id); mAnnotations.remove(id); } - private void removeMarkerView(Marker marker) { - final View viewHolder = mMarkerViewMap.get(marker); - if (viewHolder != null && marker != null) { - for (final MarkerViewAdapter<?> adapter : mMarkerViewAdapters) { - if (adapter.getMarkerClass() == marker.getClass()) { - - // get pool of Views associated to an adapter - final Pools.SimplePool<View> viewPool = adapter.getViewReusePool(); - - // cancel ongoing animations - viewHolder.animate().cancel(); - viewHolder.setAlpha(1); - - // animate alpha - viewHolder.animate() - .alpha(0) - .setDuration(MapboxConstants.ANIMATION_DURATION_SHORT) - .setInterpolator(new FastOutSlowInInterpolator()) - .setListener(new AnimatorListenerAdapter() { - - @Override - public void onAnimationEnd(Animator animation) { - super.onAnimationEnd(animation); - viewHolder.setVisibility(View.GONE); - viewPool.release(viewHolder); - } - }); - } - } - } - } - /** * Removes an annotation from the map * @@ -1175,8 +1027,9 @@ public class MapboxMap { if (annotation instanceof Marker) { Marker marker = (Marker) annotation; marker.hideInfoWindow(); - removeMarkerView(marker); - mMarkerViewMap.remove(marker); + if (marker instanceof MarkerView) { + mMarkerViewManager.removeMarkerView((MarkerView) marker, true); + } } ids[i] = annotationList.get(i).getId(); } @@ -1200,8 +1053,9 @@ public class MapboxMap { if (annotation instanceof Marker) { Marker marker = (Marker) annotation; marker.hideInfoWindow(); - removeMarkerView(marker); - mMarkerViewMap.remove(marker); + if (marker instanceof MarkerView) { + mMarkerViewManager.removeMarkerView((MarkerView) marker, true); + } } } mMapView.removeAnnotations(ids); @@ -1354,23 +1208,7 @@ public class MapboxMap { } if (marker instanceof MarkerView) { - final View viewMarker = mMarkerViewMap.get(marker); - if (viewMarker != null) { - int deselectAnimatorRes = ((MarkerView) marker).getDeselectAnimRes(); - if (deselectAnimatorRes != 0) { - viewMarker.setLayerType(View.LAYER_TYPE_HARDWARE, null); - Animator animator = AnimatorInflater.loadAnimator(mMapView.getContext(), deselectAnimatorRes); - animator.addListener(new AnimatorListenerAdapter() { - @Override - public void onAnimationEnd(Animator animation) { - super.onAnimationEnd(animation); - viewMarker.setLayerType(View.LAYER_TYPE_NONE, null); - } - }); - animator.setTarget(viewMarker); - animator.start(); - } - } + mMarkerViewManager.deselect((MarkerView) marker); } } @@ -1418,35 +1256,8 @@ public class MapboxMap { return marker; } - public void addMarkerViewAdapter(@Nullable MarkerViewAdapter markerViewAdapter) { - if (!mMarkerViewAdapters.contains(markerViewAdapter)) { - mMarkerViewAdapters.add(markerViewAdapter); - invalidateViewMarkersInBounds(); - } - } - - public List<MarkerViewAdapter> getMarkerViewAdapters() { - return mMarkerViewAdapters; - } - - public void setMarkerViewRotation(@NonNull MarkerView markerView, float rotation) { - final View convertView = mMarkerViewMap.get(markerView); - if (convertView != null) { - convertView.setLayerType(View.LAYER_TYPE_HARDWARE, null); - ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(convertView, View.ROTATION, convertView.getRotation(), rotation); - rotateAnimator.addListener(new AnimatorListenerAdapter() { - @Override - public void onAnimationEnd(Animator animation) { - super.onAnimationEnd(animation); - convertView.setLayerType(View.LAYER_TYPE_NONE, null); - } - }); - rotateAnimator.start(); - } - } - - public void setOnMarkerViewClickListener(@Nullable OnMarkerViewClickListener listener) { - mOnMarkerViewClickListener = listener; + public MarkerViewManager getMarkerViewManager() { + return mMarkerViewManager; } // @@ -1504,10 +1315,6 @@ public class MapboxMap { return mInfoWindows; } - Map<MarkerView, View> getMarkerViewMap() { - return mMarkerViewMap; - } - private boolean isInfoWindowValidForMarker(@NonNull Marker marker) { return !TextUtils.isEmpty(marker.getTitle()) || !TextUtils.isEmpty(marker.getSnippet()); } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/BulkMarkerActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/BulkMarkerActivity.java index 86c384cc8f..5f054f80f6 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/BulkMarkerActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/BulkMarkerActivity.java @@ -8,7 +8,6 @@ import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.annotation.StringDef; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; @@ -28,11 +27,11 @@ import com.mapbox.mapboxsdk.annotations.MarkerOptions; import com.mapbox.mapboxsdk.annotations.MarkerView; import com.mapbox.mapboxsdk.annotations.MarkerViewOptions; 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.utils.GeoParseUtil; -import com.mapbox.mapboxsdk.maps.MapView; import org.json.JSONException; @@ -252,13 +251,13 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView } // add adapter - mMapboxMap.addMarkerViewAdapter(new TextAdapter(BulkMarkerActivity.this)); + mMapboxMap.getMarkerViewManager().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.getMarkerViewAdapters().isEmpty()) { + if (!mMapboxMap.getMarkerViewManager().getMarkerViewAdapters().isEmpty()) { TextView viewCountView = (TextView) findViewById(R.id.countView); viewCountView.setText("ViewCache size " + (mMapView.getChildCount() - 5)); } @@ -266,7 +265,7 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView } }); - mMapboxMap.setOnMarkerViewClickListener(new MapboxMap.OnMarkerViewClickListener() { + mMapboxMap.getMarkerViewManager().setOnMarkerViewClickListener(new MapboxMap.OnMarkerViewClickListener() { @Override public boolean onMarkerClick(@NonNull Marker marker, @NonNull View view, @NonNull MapboxMap.MarkerViewAdapter adapter) { Toast.makeText(BulkMarkerActivity.this, "Hello " + marker.getId(), Toast.LENGTH_SHORT).show(); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/ViewMarkerAdapterActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/ViewMarkerAdapterActivity.java index 533d5eb7bc..52e9cc9d1b 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/ViewMarkerAdapterActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/ViewMarkerAdapterActivity.java @@ -8,7 +8,6 @@ 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; @@ -19,8 +18,8 @@ import android.widget.Toast; import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.annotations.MarkerView; +import com.mapbox.mapboxsdk.annotations.MarkerViewManager; import com.mapbox.mapboxsdk.annotations.MarkerViewOptions; -import com.mapbox.mapboxsdk.constants.MapboxConstants; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; @@ -99,21 +98,22 @@ public class ViewMarkerAdapterActivity extends AppCompatActivity { ); // set adapters - mMapboxMap.addMarkerViewAdapter(new TextAdapter(ViewMarkerAdapterActivity.this)); - mMapboxMap.addMarkerViewAdapter(new CountryAdapter(ViewMarkerAdapterActivity.this)); + final MarkerViewManager markerViewManager = mapboxMap.getMarkerViewManager(); + markerViewManager.addMarkerViewAdapter(new TextAdapter(ViewMarkerAdapterActivity.this)); + markerViewManager.addMarkerViewAdapter(new CountryAdapter(ViewMarkerAdapterActivity.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.getMarkerViewAdapters().isEmpty() && viewCountView != null) { + if (!markerViewManager.getMarkerViewAdapters().isEmpty() && viewCountView != null) { viewCountView.setText("ViewCache size " + (mMapView.getChildCount() - 5)); } } } }); - mMapboxMap.setOnMarkerViewClickListener(new MapboxMap.OnMarkerViewClickListener() { + mMapboxMap.getMarkerViewManager().setOnMarkerViewClickListener(new MapboxMap.OnMarkerViewClickListener() { @Override public boolean onMarkerClick(@NonNull Marker marker, @NonNull View view, @NonNull MapboxMap.MarkerViewAdapter adapter) { Toast.makeText(ViewMarkerAdapterActivity.this, "Hello " + marker.getId(), Toast.LENGTH_SHORT).show(); |