[android] #3276 - added item animation for select/deselect state of a View Marker. Removed asynctask due to unneeded overhead. Improved integration with selectMarker. Added item animators for the test app.
This commit is contained in:
@@ -469,7 +469,7 @@ public class MapView extends FrameLayout {
|
||||
}
|
||||
}
|
||||
|
||||
void invalidateViewMarkers(){
|
||||
void invalidateViewMarkers() {
|
||||
long currentTime = SystemClock.elapsedRealtime();
|
||||
|
||||
if (mViewMarkersUpdateRunning || currentTime < mViewMarkerBoundsUpdateTime) {
|
||||
@@ -477,101 +477,84 @@ public class MapView extends FrameLayout {
|
||||
}
|
||||
|
||||
mViewMarkerBoundsUpdateTime = currentTime + 300;
|
||||
// new ViewMarkerInBoundsTask().execute();
|
||||
// }
|
||||
|
||||
// public class ViewMarkerInBoundsTask extends AsyncTask<Void, Void, ViewMarkerInBoundsTask.Result> {
|
||||
//
|
||||
// @Override
|
||||
// protected void onPreExecute() {
|
||||
// super.onPreExecute();
|
||||
mViewMarkersUpdateRunning = true;
|
||||
// }
|
||||
mViewMarkersUpdateRunning = true;
|
||||
|
||||
// @Override
|
||||
// protected Result doInBackground(Void... params) {
|
||||
List<Marker> inBounds = new ArrayList<>();
|
||||
Map<Marker, View> outBounds = new HashMap<>();
|
||||
List<Marker> inBounds = new ArrayList<>();
|
||||
Map<Marker, View> outBounds = new HashMap<>();
|
||||
|
||||
LatLngBounds bounds = mMapboxMap.getProjection().getVisibleRegion().latLngBounds;
|
||||
long[] ids = mNativeMapView.getAnnotationsInBounds(bounds);
|
||||
LongSparseArray<View> markerViews = mMapboxMap.getMarkerViews();
|
||||
Log.v(MapboxConstants.TAG, "Annotations in bounds: " + ids.length);
|
||||
LatLngBounds bounds = mMapboxMap.getProjection().getVisibleRegion().latLngBounds;
|
||||
long[] ids = mNativeMapView.getAnnotationsInBounds(bounds);
|
||||
LongSparseArray<View> markerViews = mMapboxMap.getMarkerViews();
|
||||
Log.v(MapboxConstants.TAG, "Annotations in bounds: " + ids.length);
|
||||
|
||||
boolean found;
|
||||
long key;
|
||||
boolean found;
|
||||
long key;
|
||||
|
||||
// introduce new markers
|
||||
for (long id : ids) {
|
||||
found = false;
|
||||
for (int i = 0; i < markerViews.size(); i++) {
|
||||
key = markerViews.keyAt(i);
|
||||
|
||||
if (id == key) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
Annotation annotation = mMapboxMap.getAnnotation(id);
|
||||
if (annotation instanceof Marker) {
|
||||
inBounds.add((Marker) annotation);
|
||||
} else {
|
||||
Log.v(MapboxConstants.TAG, "Not instance of Marker" + id);
|
||||
}
|
||||
} else {
|
||||
Log.v(MapboxConstants.TAG, "Already added " + id);
|
||||
}
|
||||
}
|
||||
|
||||
// clean up out of bound markers
|
||||
// introduce new markers
|
||||
for (long id : ids) {
|
||||
found = false;
|
||||
for (int i = 0; i < markerViews.size(); i++) {
|
||||
found = false;
|
||||
key = markerViews.keyAt(i);
|
||||
for (long id : ids) {
|
||||
if (id == key) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
Annotation annotation = mMapboxMap.getAnnotation(key);
|
||||
if (annotation instanceof Marker) {
|
||||
outBounds.put((Marker) annotation, markerViews.get(key));
|
||||
} else {
|
||||
Log.v(MapboxConstants.TAG, "Not instance of Marker" + key);
|
||||
}
|
||||
|
||||
if (id == key) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
Annotation annotation = mMapboxMap.getAnnotation(id);
|
||||
if (annotation instanceof Marker) {
|
||||
inBounds.add((Marker) annotation);
|
||||
} else {
|
||||
Log.v(MapboxConstants.TAG, "Not instance of Marker" + id);
|
||||
}
|
||||
} else {
|
||||
Log.v(MapboxConstants.TAG, "Already added " + id);
|
||||
}
|
||||
}
|
||||
|
||||
// clean up out of bound markers
|
||||
for (int i = 0; i < markerViews.size(); i++) {
|
||||
found = false;
|
||||
key = markerViews.keyAt(i);
|
||||
for (long id : ids) {
|
||||
if (id == key) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
Annotation annotation = mMapboxMap.getAnnotation(key);
|
||||
if (annotation instanceof Marker) {
|
||||
outBounds.put((Marker) annotation, markerViews.get(key));
|
||||
} else {
|
||||
Log.v(MapboxConstants.TAG, "Not instance of Marker" + key);
|
||||
}
|
||||
}
|
||||
}
|
||||
Result result = new Result(inBounds, outBounds);
|
||||
mMapboxMap.setViewMarkersBoundsTaskResult(result);
|
||||
mViewMarkersUpdateRunning = false;
|
||||
Log.v(MapboxConstants.TAG, "Amount of child views " + getChildCount());
|
||||
}
|
||||
|
||||
// return new Result(inBounds, outBounds);
|
||||
// }
|
||||
public class Result {
|
||||
private List<Marker> inBounds;
|
||||
private Map<Marker, View> outBounds;
|
||||
|
||||
// @Override
|
||||
// protected void onPostExecute(Result result) {
|
||||
// super.onPostExecute(result);
|
||||
mMapboxMap.setViewMarkersBoundsTaskResult(result);
|
||||
mViewMarkersUpdateRunning = false;
|
||||
Log.v(MapboxConstants.TAG, "Amount of child views " + getChildCount());
|
||||
public Result(List<Marker> inBounds, Map<Marker, View> outBounds) {
|
||||
this.inBounds = inBounds;
|
||||
this.outBounds = outBounds;
|
||||
}
|
||||
|
||||
public class Result {
|
||||
private List<Marker> inBounds;
|
||||
private Map<Marker, View> outBounds;
|
||||
|
||||
public Result(List<Marker> inBounds, Map<Marker, View> outBounds) {
|
||||
this.inBounds = inBounds;
|
||||
this.outBounds = outBounds;
|
||||
}
|
||||
|
||||
public List<Marker> getInBounds() {
|
||||
return inBounds;
|
||||
}
|
||||
|
||||
public Map<Marker, View> getOutBounds() {
|
||||
return outBounds;
|
||||
}
|
||||
public List<Marker> getInBounds() {
|
||||
return inBounds;
|
||||
}
|
||||
|
||||
public Map<Marker, View> getOutBounds() {
|
||||
return outBounds;
|
||||
}
|
||||
}
|
||||
// }
|
||||
|
||||
/**
|
||||
@@ -1456,7 +1439,7 @@ public class MapView extends FrameLayout {
|
||||
mViewHolder = viewMarkers.valueAt(i);
|
||||
if (mViewHolder != null) {
|
||||
Marker marker = (Marker) mMapboxMap.getAnnotation(viewMarkers.keyAt(i));
|
||||
if(marker!=null) {
|
||||
if (marker != null) {
|
||||
PointF point = mMapboxMap.getProjection().toScreenLocation(marker.getPosition());
|
||||
mViewHolder.setX(point.x - (mViewHolder.getMeasuredWidth() / 2));
|
||||
mViewHolder.setY(point.y - (mViewHolder.getMeasuredHeight() / 2));
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
package com.mapbox.mapboxsdk.maps;
|
||||
|
||||
import android.content.Context;
|
||||
import android.animation.Animator;
|
||||
import android.animation.AnimatorInflater;
|
||||
import android.animation.AnimatorListenerAdapter;
|
||||
import android.animation.ObjectAnimator;
|
||||
import android.graphics.Bitmap;
|
||||
import android.location.Location;
|
||||
import android.os.SystemClock;
|
||||
import android.support.annotation.AnimatorRes;
|
||||
import android.support.annotation.FloatRange;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
@@ -17,6 +22,7 @@ import android.view.View;
|
||||
import com.mapbox.mapboxsdk.MapboxAccountManager;
|
||||
|
||||
import android.view.ViewGroup;
|
||||
import android.view.animation.Animation;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Annotation;
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||
@@ -68,12 +74,15 @@ public class MapboxMap {
|
||||
|
||||
private List<Marker> mSelectedMarkers;
|
||||
private LongSparseArray<View> mMarkerViews;
|
||||
private List<InfoWindow> mInfoWindows;
|
||||
|
||||
private List<InfoWindow> mInfoWindows;
|
||||
private MapboxMap.InfoWindowAdapter mInfoWindowAdapter;
|
||||
|
||||
private MapboxMap.MarkerViewAdapter mMarkerViewAdapter;
|
||||
private Bitmap mViewMarkerBitmap;
|
||||
private OnMarkerViewClickListener mOnMarkerViewClickListener;
|
||||
private int mMarkerViewItemAnimatorInRes;
|
||||
private int mMarkerViewItemAnimatorOutRes;
|
||||
private Bitmap mViewMarkerBitmap;
|
||||
|
||||
private boolean mMyLocationEnabled;
|
||||
private boolean mAllowConcurrentMultipleInfoWindows;
|
||||
@@ -660,7 +669,7 @@ public class MapboxMap {
|
||||
if (adaptedView != null) {
|
||||
// hack to hide old marker, todo replace with visibility
|
||||
Icon icon = marker.getIcon();
|
||||
if(!icon.getBitmap().equals(mViewMarkerBitmap)) {
|
||||
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);
|
||||
@@ -671,15 +680,27 @@ public class MapboxMap {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
if (mSelectedMarkers.contains(marker)) {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean clickHandled = false;
|
||||
if (mOnMarkerViewClickListener != null) {
|
||||
mOnMarkerViewClickListener.onMarkerClick(marker, v);
|
||||
clickHandled = mOnMarkerViewClickListener.onMarkerClick(marker, v);
|
||||
}
|
||||
|
||||
mSelectedMarkers.add(marker);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1184,6 +1205,13 @@ public class MapboxMap {
|
||||
if (marker.isInfoWindowShown()) {
|
||||
marker.hideInfoWindow();
|
||||
}
|
||||
|
||||
View viewMarker = mMarkerViews.get(marker.getId());
|
||||
if(viewMarker!=null){
|
||||
Animator animator = AnimatorInflater.loadAnimator(mMapView.getContext(),mMarkerViewItemAnimatorOutRes);
|
||||
animator.setTarget(viewMarker);
|
||||
animator.start();
|
||||
}
|
||||
}
|
||||
|
||||
// Removes all selected markers from the list
|
||||
@@ -1242,6 +1270,21 @@ public class MapboxMap {
|
||||
return mOnMarkerViewClickListener;
|
||||
}
|
||||
|
||||
public void setMarkerViewItemAnimation(@AnimatorRes int animationInRes,@AnimatorRes int animationOutRes) {
|
||||
mMarkerViewItemAnimatorInRes = animationInRes;
|
||||
mMarkerViewItemAnimatorOutRes = animationOutRes;
|
||||
}
|
||||
|
||||
@AnimatorRes
|
||||
public int getMarkerViewItemAnimatorInRes() {
|
||||
return mMarkerViewItemAnimatorInRes;
|
||||
}
|
||||
|
||||
@AnimatorRes
|
||||
public int getMarkerViewItemAnimatorOutRes() {
|
||||
return mMarkerViewItemAnimatorOutRes;
|
||||
}
|
||||
|
||||
//
|
||||
// InfoWindow
|
||||
//
|
||||
@@ -1838,7 +1881,7 @@ public class MapboxMap {
|
||||
|
||||
public interface OnMarkerViewClickListener {
|
||||
|
||||
void onMarkerClick(@NonNull Marker marker, @NonNull View view);
|
||||
boolean onMarkerClick(@NonNull Marker marker, @NonNull View view);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user