package com.mapbox.mapboxsdk.annotations; import android.graphics.PointF; import android.os.SystemClock; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.util.Pools; import android.view.View; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.Projection; import com.mapbox.mapboxsdk.utils.AnimatorUtils; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * Interface for interacting with ViewMarkers objects inside of a MapView. *
* This class is responsible for managing a {@link MarkerView} item. *
*/ public class MarkerViewManager { private Map* The {@link MarkerView} will be rotated from its current rotation to the given rotation. *
* * @param marker the MarkerView to rotate * @param rotation the rotation value */ public void animateRotation(@NonNull MarkerView marker, float rotation) { View convertView = mMarkerViewMap.get(marker); if (convertView != null) { AnimatorUtils.rotate(convertView, rotation); } } /** * Animate a MarkerView to a given alpha value. ** The {@link MarkerView} will be transformed from its current alpha value to the given value. *
* * @param marker the MarkerView to change its alpha value * @param alpha the alpha value */ public void animateAlpha(@NonNull MarkerView marker, float alpha) { View convertView = mMarkerViewMap.get(marker); if (convertView != null) { AnimatorUtils.alpha(convertView, alpha); } } /** * Animate a MarkerVIew to be visible or invisible ** The {@link MarkerView} will be made {@link View#VISIBLE} or {@link View#GONE}. *
* * @param marker the MarkerView to change its visibility * @param visible the flag indicating if MarkerView is visible */ public void animateVisible(@NonNull MarkerView marker, boolean visible) { View convertView = mMarkerViewMap.get(marker); if (convertView != null) { convertView.setVisibility(visible ? View.VISIBLE : View.GONE); } } /** * Updates the position of MarkerViews currently found in the viewport. ** The collection of {@link MarkerView} will be iterated and each item position will be updated. * If an item is View state is not visible and its related flag is set to visible, * The {@link MarkerView} will be animated to visible using alpha animation. *
*/ public void update() { View convertView; for (MarkerView marker : mMarkerViewMap.keySet()) { convertView = mMarkerViewMap.get(marker); if (convertView != null) { PointF point = mapboxMap.getProjection().toScreenLocation(marker.getPosition()); int x = (int) (marker.getAnchorU() * convertView.getMeasuredWidth()); int y = (int) (marker.getAnchorV() * convertView.getMeasuredHeight()); marker.setOffsetX(x); marker.setOffsetY(y); convertView.setX(point.x - x); convertView.setY(point.y - y); if (marker.isVisible() && convertView.getVisibility() == View.GONE) { convertView.animate().cancel(); convertView.setAlpha(0); AnimatorUtils.alpha(convertView, 1); } } } } /** * Set tilt on every non flat MarkerView currently shown in the Viewport. * * @param tilt the tilt value */ public void setTilt(float tilt) { View convertView; for (MarkerView markerView : mMarkerViewMap.keySet()) { if (markerView.isFlat()) { convertView = mMarkerViewMap.get(markerView); if (convertView != null) { markerView.setTilt(tilt); convertView.setRotationX(tilt); } } } } /** * Animate a MarkerView to a deselected state. ** The {@link MarkerView#getDeselectAnimRes()} will be called to get the related animation. * If non are provided, no animation will be started. *
* * @param marker the MarkerView to deselect */ public void deselect(@NonNull MarkerView marker) { final View convertView = mMarkerViewMap.get(marker); if (convertView != null) { int deselectAnimatorRes = marker.getDeselectAnimRes(); if (deselectAnimatorRes != 0) { AnimatorUtils.animate(convertView, deselectAnimatorRes); } } } /** * Remove a MarkerView from a map. ** The {@link MarkerView} will be removed using an alpha animation and related {@link View} * will be released to the {@link android.support.v4.util.Pools.SimplePool} from the related * {@link com.mapbox.mapboxsdk.maps.MapboxMap.MarkerViewAdapter}. It's possible to remove * the {@link MarkerView} from the underlying collection if needed. *
* * @param marker the MarkerView to remove * @param removeFromMap flag indicating if a MarkerView will be removed from the collection. */ public void removeMarkerView(MarkerView marker, boolean removeFromMap) { final View viewHolder = mMarkerViewMap.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* This method is rate limited, and {@link #invalidateViewMarkersInBounds} will only be called * once each 250 ms. *
*/ public void scheduleViewMarkerInvalidation() { if (!markerViewAdapters.isEmpty()) { long currentTime = SystemClock.elapsedRealtime(); if (currentTime < mViewMarkerBoundsUpdateTime) { return; } invalidateViewMarkersInBounds(); mViewMarkerBoundsUpdateTime = currentTime + 250; } } /** * Invalidate the ViewMarkers found in the viewport. ** This method will remove any markers that aren't in the viewport any more and will add new * ones for each found Marker in the changed viewport. *
*/ public void invalidateViewMarkersInBounds() { Projection projection = mapboxMap.getProjection(); List