[android] #3276 - add tilt support for view markers
This commit is contained in:
@@ -2,40 +2,26 @@ package com.mapbox.mapboxsdk.annotations;
|
||||
|
||||
import android.graphics.Point;
|
||||
import android.graphics.PointF;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.AnimatorRes;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
public class MarkerViewSettings implements Parcelable {
|
||||
public class MarkerViewSettings {
|
||||
|
||||
private PointF centerOffset;
|
||||
private Point infoWindowOffset;
|
||||
private boolean flat;
|
||||
|
||||
private int animEnterRes;
|
||||
private int animSelectRes;
|
||||
private int animDeselectRes;
|
||||
private int animExitRes;
|
||||
private int selectAnimRes;
|
||||
private int deselectAnimRes;
|
||||
|
||||
MarkerViewSettings(PointF centerOffset, Point infoWindowOffset, boolean flat, int animEnterRes, int animSelectRes, int animDeselectRes, int animExitRes) {
|
||||
private float tiltValue;
|
||||
|
||||
MarkerViewSettings(PointF centerOffset, Point infoWindowOffset, boolean flat, int selectAnimRes, int deselectAnimRes) {
|
||||
this.centerOffset = centerOffset;
|
||||
this.infoWindowOffset = infoWindowOffset;
|
||||
this.flat = flat;
|
||||
this.animEnterRes = animEnterRes;
|
||||
this.animSelectRes = animSelectRes;
|
||||
this.animDeselectRes = animDeselectRes;
|
||||
this.animExitRes = animExitRes;
|
||||
}
|
||||
|
||||
MarkerViewSettings(Parcel parcel) {
|
||||
this.centerOffset = parcel.readParcelable(PointF.class.getClassLoader());
|
||||
this.infoWindowOffset = parcel.readParcelable(Point.class.getClassLoader());
|
||||
this.flat = parcel.readByte() != 0;
|
||||
this.animEnterRes = parcel.readInt();
|
||||
this.animSelectRes = parcel.readInt();
|
||||
this.animDeselectRes = parcel.readInt();
|
||||
this.animExitRes = parcel.readInt();
|
||||
this.selectAnimRes = selectAnimRes;
|
||||
this.deselectAnimRes = deselectAnimRes;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@@ -48,55 +34,26 @@ public class MarkerViewSettings implements Parcelable {
|
||||
return infoWindowOffset;
|
||||
}
|
||||
|
||||
@AnimatorRes
|
||||
public int getSelectAnimRes() {
|
||||
return selectAnimRes;
|
||||
}
|
||||
|
||||
@AnimatorRes
|
||||
public int getDeselectAnimRes() {
|
||||
return deselectAnimRes;
|
||||
}
|
||||
|
||||
public boolean isFlat() {
|
||||
return flat;
|
||||
}
|
||||
|
||||
@AnimatorRes
|
||||
public int getAnimEnterRes() {
|
||||
return animEnterRes;
|
||||
public void setTiltValue(float tilt) {
|
||||
tiltValue = tilt;
|
||||
}
|
||||
|
||||
@AnimatorRes
|
||||
public int getAnimSelectRes() {
|
||||
return animSelectRes;
|
||||
}
|
||||
|
||||
@AnimatorRes
|
||||
public int getAnimDeselectRes() {
|
||||
return animDeselectRes;
|
||||
}
|
||||
|
||||
@AnimatorRes
|
||||
public int getAnimExitRes() {
|
||||
return animExitRes;
|
||||
}
|
||||
|
||||
public static final Parcelable.Creator<MarkerViewSettings> CREATOR
|
||||
= new Parcelable.Creator<MarkerViewSettings>() {
|
||||
public MarkerViewSettings createFromParcel(Parcel in) {
|
||||
return new MarkerViewSettings(in);
|
||||
}
|
||||
|
||||
public MarkerViewSettings[] newArray(int size) {
|
||||
return new MarkerViewSettings[size];
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeParcelable(centerOffset, flags);
|
||||
dest.writeParcelable(infoWindowOffset, flags);
|
||||
dest.writeByte((byte) (flat ? 1 : 0));
|
||||
dest.writeInt(animEnterRes);
|
||||
dest.writeInt(animSelectRes);
|
||||
dest.writeInt(animDeselectRes);
|
||||
dest.writeInt(animExitRes);
|
||||
public float getTiltValue() {
|
||||
return tiltValue;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,10 +65,8 @@ public class MarkerViewSettings implements Parcelable {
|
||||
private Point infoWindowOffset;
|
||||
private boolean flat;
|
||||
|
||||
private int animEnterRes;
|
||||
private int animSelectRes;
|
||||
private int animDeselectRes;
|
||||
private int animExitRes;
|
||||
|
||||
public Builder() {
|
||||
centerOffset = new PointF();
|
||||
@@ -133,11 +88,6 @@ public class MarkerViewSettings implements Parcelable {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder animEnterRes(@AnimatorRes int animatorRes) {
|
||||
animEnterRes = animatorRes;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder animSelectRes(@AnimatorRes int animatorRes) {
|
||||
animSelectRes = animatorRes;
|
||||
return this;
|
||||
@@ -148,13 +98,8 @@ public class MarkerViewSettings implements Parcelable {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder animExitRes(@AnimatorRes int animatorRes) {
|
||||
animExitRes = animatorRes;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MarkerViewSettings build() {
|
||||
return new MarkerViewSettings(centerOffset, infoWindowOffset, flat, animEnterRes, animSelectRes, animDeselectRes, animExitRes);
|
||||
return new MarkerViewSettings(centerOffset, infoWindowOffset, flat, animSelectRes, animDeselectRes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,7 +158,6 @@ public class MapView extends FrameLayout {
|
||||
|
||||
private List<OnMapReadyCallback> mOnMapReadyCallbackList;
|
||||
private long mViewMarkerBoundsUpdateTime;
|
||||
private boolean mViewMarkersUpdateRunning;
|
||||
|
||||
@UiThread
|
||||
public MapView(@NonNull Context context) {
|
||||
@@ -453,9 +452,11 @@ public class MapView extends FrameLayout {
|
||||
}
|
||||
}
|
||||
} else if (change == REGION_IS_CHANGING || change == REGION_DID_CHANGE) {
|
||||
if (mMapboxMap.getMarkerViewAdapters() != null) {
|
||||
if (!mMapboxMap.getMarkerViewAdapters().isEmpty()) {
|
||||
invalidateViewMarkers();
|
||||
}
|
||||
}else if(change== DID_FINISH_LOADING_MAP){
|
||||
invalidateViewMarkers();
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -471,17 +472,11 @@ public class MapView extends FrameLayout {
|
||||
|
||||
void invalidateViewMarkers() {
|
||||
long currentTime = SystemClock.elapsedRealtime();
|
||||
|
||||
if (mViewMarkersUpdateRunning || currentTime < mViewMarkerBoundsUpdateTime) {
|
||||
if (currentTime < mViewMarkerBoundsUpdateTime) {
|
||||
return;
|
||||
}
|
||||
mViewMarkerBoundsUpdateTime = currentTime + 300;
|
||||
mViewMarkersUpdateRunning = true;
|
||||
|
||||
mMapboxMap.invalidateViewMarkersInBounds();
|
||||
|
||||
mViewMarkersUpdateRunning = false;
|
||||
Log.v(MapboxConstants.TAG, "Amount of child views " + getChildCount());
|
||||
mViewMarkerBoundsUpdateTime = currentTime + 250;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -623,13 +618,9 @@ public class MapView extends FrameLayout {
|
||||
return mNativeMapView.getPitch();
|
||||
}
|
||||
|
||||
void setTilt(Double pitch, @Nullable Long duration) {
|
||||
long actualDuration = 0;
|
||||
if (duration != null) {
|
||||
actualDuration = duration;
|
||||
}
|
||||
void setTilt(Double pitch) {
|
||||
mMyLocationView.setTilt(pitch);
|
||||
mNativeMapView.setPitch(pitch, actualDuration);
|
||||
mNativeMapView.setPitch(pitch, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -1356,8 +1347,6 @@ public class MapView extends FrameLayout {
|
||||
return;
|
||||
}
|
||||
|
||||
Log.v(MapboxConstants.TAG, "SurfaceTexture has been updated");
|
||||
|
||||
mCompassView.update(getDirection());
|
||||
mMyLocationView.update();
|
||||
|
||||
@@ -2003,7 +1992,7 @@ public class MapView extends FrameLayout {
|
||||
pitch = Math.max(MapboxConstants.MINIMUM_TILT, Math.min(MapboxConstants.MAXIMUM_TILT, pitch));
|
||||
|
||||
// Tilt the map
|
||||
setTilt(pitch, null);
|
||||
mMapboxMap.setTilt(pitch);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -7,10 +7,8 @@ import android.animation.AnimatorListenerAdapter;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.PointF;
|
||||
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;
|
||||
@@ -655,8 +653,6 @@ public class MapboxMap {
|
||||
|
||||
void invalidateViewMarkersInBounds() {
|
||||
List<Marker> markers = mMapView.getMarkersInBounds(mProjection.getVisibleRegion().latLngBounds);
|
||||
Log.v(MapboxConstants.TAG, "Annotations in bounds: " + markers.size());
|
||||
|
||||
View convertView;
|
||||
|
||||
// remove old markers
|
||||
@@ -667,7 +663,7 @@ public class MapboxMap {
|
||||
// remove marker
|
||||
convertView = mMarkerViewMap.get(m);
|
||||
MarkerViewSettings settings = mMarkerViewSettingsMap.get(m.getId());
|
||||
int deselectAnimRes = settings.getAnimDeselectRes();
|
||||
int deselectAnimRes = settings.getDeselectAnimRes();
|
||||
if (deselectAnimRes != 0) {
|
||||
Animator animator = AnimatorInflater.loadAnimator(mMapView.getContext(), deselectAnimRes);
|
||||
animator.setDuration(0);
|
||||
@@ -681,83 +677,92 @@ public class MapboxMap {
|
||||
|
||||
// introduce new markers
|
||||
for (final Marker marker : markers) {
|
||||
if (marker.isViewMarker()) {
|
||||
if (!mMarkerViewMap.containsKey(marker)) {
|
||||
Log.v("TAG", "Calling get view for " + marker.getId());
|
||||
for (final MarkerViewAdapter adapter : mMarkerViewAdapters) {
|
||||
if (adapter.getMarkerClass() == marker.getClass()) {
|
||||
if (marker.isViewMarker() && !mMarkerViewMap.containsKey(marker)) {
|
||||
for (final MarkerViewAdapter adapter : mMarkerViewAdapters) {
|
||||
if (adapter.getMarkerClass() == marker.getClass()) {
|
||||
|
||||
if (mMarkerViewSettingsMap.get(marker.getId()) == null) {
|
||||
mMarkerViewSettingsMap.put(marker.getId(), adapter.getMarkerViewSettings(marker));
|
||||
MarkerViewSettings settings = mMarkerViewSettingsMap.get(marker.getId());
|
||||
if (settings == null) {
|
||||
settings = adapter.getMarkerViewSettings(marker);
|
||||
mMarkerViewSettingsMap.put(marker.getId(), settings);
|
||||
}
|
||||
|
||||
convertView = (View) adapter.getViewReusePool().acquire();
|
||||
View adaptedView = adapter.getView(marker, convertView, mMapView);
|
||||
|
||||
// infowindow offset
|
||||
Point infoWindowOffset = settings.getInfoWindowOffset();
|
||||
marker.setTopOffsetPixels(infoWindowOffset.y);
|
||||
marker.setRightOffsetPixels(infoWindowOffset.x);
|
||||
|
||||
if (adaptedView != null) {
|
||||
|
||||
// tilt
|
||||
adaptedView.setRotationX(settings.getTiltValue());
|
||||
|
||||
if (mSelectedMarkers.contains(marker)) {
|
||||
// if a marker to be shown was selected
|
||||
// replay that animation with duration 0
|
||||
int selectAnimRes = settings.getSelectAnimRes();
|
||||
if (selectAnimRes != 0) {
|
||||
Animator animator = AnimatorInflater.loadAnimator(mMapView.getContext(), selectAnimRes);
|
||||
animator.setDuration(0);
|
||||
animator.setTarget(convertView);
|
||||
animator.start();
|
||||
}
|
||||
}
|
||||
|
||||
convertView = (View) adapter.getViewReusePool().acquire();
|
||||
View adaptedView = adapter.getView(marker, convertView, mMapView);
|
||||
|
||||
// set user provided offset to view marker
|
||||
final MarkerViewSettings markerViewSettings = mMarkerViewSettingsMap.get(marker.getId());
|
||||
Point infoWindowOffset = markerViewSettings.getInfoWindowOffset();
|
||||
marker.setTopOffsetPixels(infoWindowOffset.y);
|
||||
marker.setRightOffsetPixels(infoWindowOffset.x);
|
||||
|
||||
if (adaptedView != null) {
|
||||
if (mSelectedMarkers.contains(marker)) {
|
||||
// if a marker to be shown was selected
|
||||
// replay that animation with duration 0
|
||||
int selectAnimRes = markerViewSettings.getAnimSelectRes();
|
||||
if (selectAnimRes != 0) {
|
||||
Animator animator = AnimatorInflater.loadAnimator(mMapView.getContext(), selectAnimRes);
|
||||
animator.setDuration(0);
|
||||
animator.setTarget(convertView);
|
||||
animator.start();
|
||||
final int animSelectRes = settings.getSelectAnimRes();
|
||||
adaptedView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
boolean clickHandled = false;
|
||||
if (mOnMarkerViewClickListener != null) {
|
||||
clickHandled = mOnMarkerViewClickListener.onMarkerClick(marker, v, adapter);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
int enterAnimatorRes = markerViewSettings.getAnimSelectRes();
|
||||
if (enterAnimatorRes != 0) {
|
||||
Animator animator = AnimatorInflater.loadAnimator(mMapView.getContext(), enterAnimatorRes);
|
||||
animator.setTarget(v);
|
||||
animator.addListener(new AnimatorListenerAdapter() {
|
||||
@Override
|
||||
public void onAnimationEnd(Animator animation) {
|
||||
super.onAnimationEnd(animation);
|
||||
selectMarker(marker);
|
||||
}
|
||||
});
|
||||
animator.start();
|
||||
} else {
|
||||
selectMarker(marker);
|
||||
}
|
||||
if (!clickHandled) {
|
||||
if (animSelectRes != 0) {
|
||||
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);
|
||||
}
|
||||
});
|
||||
animator.start();
|
||||
} else {
|
||||
selectMarker(marker);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
mMarkerViewMap.put(marker, adaptedView);
|
||||
if (convertView == null) {
|
||||
mMapView.addView(adaptedView);
|
||||
}
|
||||
});
|
||||
|
||||
mMarkerViewMap.put(marker, adaptedView);
|
||||
if (convertView == null) {
|
||||
mMapView.addView(adaptedView);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//already added
|
||||
Log.v(MapboxConstants.TAG, "Marker already added");
|
||||
}
|
||||
} else {
|
||||
Log.v(MapboxConstants.TAG, "Marker is not a view marker");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setTilt(double tilt) {
|
||||
for (Map.Entry<Marker, View> entry : mMarkerViewMap.entrySet()) {
|
||||
MarkerViewSettings settings = mMarkerViewSettingsMap.get(entry.getKey().getId());
|
||||
if (settings.isFlat()) {
|
||||
settings.setTiltValue((float) tilt);
|
||||
entry.getValue().setRotationX((float) tilt);
|
||||
}
|
||||
}
|
||||
mMapView.setTilt(tilt);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Adds a marker to this map.
|
||||
@@ -1281,7 +1286,7 @@ public class MapboxMap {
|
||||
|
||||
View viewMarker = mMarkerViewMap.get(marker);
|
||||
if (viewMarker != null) {
|
||||
int deselectAnimatorRes = mMarkerViewSettingsMap.get(marker.getId()).getAnimDeselectRes();
|
||||
int deselectAnimatorRes = mMarkerViewSettingsMap.get(marker.getId()).getDeselectAnimRes();
|
||||
if (deselectAnimatorRes != 0) {
|
||||
Animator animator = AnimatorInflater.loadAnimator(mMapView.getContext(), deselectAnimatorRes);
|
||||
animator.setTarget(viewMarker);
|
||||
@@ -1965,7 +1970,6 @@ public class MapboxMap {
|
||||
public MarkerViewSettings getMarkerViewSettings(Marker marker) {
|
||||
return new MarkerViewSettings.Builder().build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public interface OnMarkerViewClickListener {
|
||||
|
||||
Reference in New Issue
Block a user