[android] #5079 - add anchor and InfoWindowAnchor to MarkerView

This commit is contained in:
Tobrun Van Nuland
2016-05-25 11:59:21 +02:00
parent 621bb01845
commit 75dbbcdcc0
7 changed files with 111 additions and 88 deletions

View File

@@ -1,7 +1,5 @@
package com.mapbox.mapboxsdk.annotations;
import android.graphics.Point;
import android.graphics.PointF;
import android.os.Parcelable;
import android.support.annotation.AnimatorRes;
import android.support.annotation.NonNull;
@@ -23,16 +21,16 @@ public abstract class BaseMarkerViewOptions<U extends MarkerView, T extends Base
protected String title;
protected Icon icon;
protected boolean flat;
protected PointF centerOffset;
protected Point infoWindowOffset;
protected float anchorU = 0.5f;
protected float anchorV = 1f;
protected float infoWindowAnchorU = 0.5f;
protected float infoWindowAnchorV = 0.0f;
protected int selectAnimRes;
protected int deselectAnimRes;
protected int rotation;
protected boolean visible = true;
public BaseMarkerViewOptions() {
centerOffset = new PointF();
infoWindowOffset = new Point();
}
public T position(@NonNull LatLng position) {
@@ -60,13 +58,15 @@ public abstract class BaseMarkerViewOptions<U extends MarkerView, T extends Base
return getThis();
}
public T centerOffset(PointF centerOffset) {
this.centerOffset = centerOffset;
public T anchor(float u, float v) {
this.anchorU = u;
this.anchorV = v;
return getThis();
}
public T infoWindowOffset(Point infoWindowOffset) {
this.infoWindowOffset = infoWindowOffset;
public T infoWindowAnchor(float u, float v) {
this.infoWindowAnchorU = u;
this.infoWindowAnchorV = v;
return getThis();
}
@@ -80,7 +80,7 @@ public abstract class BaseMarkerViewOptions<U extends MarkerView, T extends Base
return getThis();
}
public T rotation(int rotation){
public T rotation(int rotation) {
this.rotation = rotation;
return getThis();
}
@@ -110,12 +110,20 @@ public abstract class BaseMarkerViewOptions<U extends MarkerView, T extends Base
return flat;
}
public PointF getCenterOffset() {
return centerOffset;
public float getAnchorU() {
return anchorU;
}
public Point getInfoWindowOffset() {
return infoWindowOffset;
public float getAnchorV() {
return anchorV;
}
public float getInfoWindowAnchorU() {
return infoWindowAnchorU;
}
public float getInfoWindowAnchorV() {
return infoWindowAnchorV;
}
public int getSelectAnimRes() {

View File

@@ -30,6 +30,7 @@ public class InfoWindow {
protected WeakReference<View> mView;
private float mMarkerHeightOffset;
private float mMarkerWidthOffset;
private float mViewWidthOffset;
private PointF mCoordinates;
private boolean mIsVisible;
@@ -108,9 +109,9 @@ public class InfoWindow {
// Calculate y-offset for update method
mMarkerHeightOffset = -view.getMeasuredHeight() + offsetY;
mMarkerWidthOffset = -offsetX;
// Calculate default Android x,y coordinate
mCoordinates = mapboxMap.getProjection().toScreenLocation(position);
float x = mCoordinates.x - (view.getMeasuredWidth() / 2) + offsetX;
float y = mCoordinates.y - view.getMeasuredHeight() + offsetY;
@@ -244,7 +245,7 @@ public class InfoWindow {
View view = mView.get();
if (mapboxMap != null && marker != null && view != null) {
mCoordinates = mapboxMap.getProjection().toScreenLocation(marker.getPosition());
view.setX(mCoordinates.x + mViewWidthOffset);
view.setX(mCoordinates.x + mViewWidthOffset - mMarkerWidthOffset);
view.setY(mCoordinates.y + mMarkerHeightOffset);
}
}

View File

@@ -1,18 +1,22 @@
package com.mapbox.mapboxsdk.annotations;
import android.graphics.Point;
import android.graphics.PointF;
import com.mapbox.mapboxsdk.maps.MapboxMap;
public class MarkerView extends Marker {
private MarkerViewManager markerViewManager;
private PointF centerOffset;
private Point infoWindowOffset;
private float anchorU;
private float anchorV;
private float offsetX;
private float offsetY;
private float infoWindowAnchorU;
private float infoWindowAnchorV;
private boolean flat;
private boolean visible;
private boolean visible = true;
private int selectAnimRes;
private int deselectAnimRes;
@@ -22,33 +26,63 @@ public class MarkerView extends Marker {
private float alpha = 1;
MarkerView() {
centerOffset = new PointF();
infoWindowOffset = new Point();
}
public MarkerView(BaseMarkerViewOptions baseMarkerViewOptions) {
super(baseMarkerViewOptions);
this.centerOffset = baseMarkerViewOptions.getCenterOffset();
this.infoWindowOffset = baseMarkerViewOptions.getInfoWindowOffset();
this.anchorU = baseMarkerViewOptions.getAnchorU();
this.anchorV = baseMarkerViewOptions.getAnchorV();
this.infoWindowAnchorU = baseMarkerViewOptions.getInfoWindowAnchorU();
this.infoWindowAnchorV = baseMarkerViewOptions.getInfoWindowAnchorV();
this.flat = baseMarkerViewOptions.isFlat();
this.selectAnimRes = baseMarkerViewOptions.getSelectAnimRes();
this.deselectAnimRes = baseMarkerViewOptions.getDeselectAnimRes();
this.infoWindowAnchorU = baseMarkerViewOptions.infoWindowAnchorU;
this.infoWindowAnchorV = baseMarkerViewOptions.infoWindowAnchorV;
this.anchorU = baseMarkerViewOptions.anchorU;
this.anchorV = baseMarkerViewOptions.anchorV;
}
public PointF getCenterOffset() {
return centerOffset;
public void setAnchor(float u, float v) {
this.anchorU = u;
this.anchorV = v;
}
public void setCenterOffset(PointF centerOffset) {
this.centerOffset = centerOffset;
public float getAnchorU() {
return anchorU;
}
public Point getInfoWindowOffset() {
return infoWindowOffset;
public float getAnchorV() {
return anchorV;
}
public void setInfoWindowOffset(Point infoWindowOffset) {
this.infoWindowOffset = infoWindowOffset;
void setOffsetX(float x){
offsetX = x;
}
void setOffsetY(float y){
offsetY = y;
}
float getOffsetX() {
return offsetX;
}
float getOffsetY() {
return offsetY;
}
public void setInfoWindowAnchor(float u, float v) {
this.infoWindowAnchorU = u;
this.infoWindowAnchorV = v;
}
public float getInfoWindowAnchorU() {
return infoWindowAnchorU;
}
public float getInfoWindowAnchorV() {
return infoWindowAnchorV;
}
public boolean isFlat() {

View File

@@ -1,6 +1,5 @@
package com.mapbox.mapboxsdk.annotations;
import android.graphics.Point;
import android.graphics.PointF;
import android.os.SystemClock;
import android.support.annotation.NonNull;
@@ -62,8 +61,13 @@ public class MarkerViewManager {
convertView = mMarkerViewMap.get(marker);
if (convertView != null) {
PointF point = mapboxMap.getProjection().toScreenLocation(marker.getPosition());
convertView.setX(point.x - (convertView.getMeasuredWidth() / 2));
convertView.setY(point.y - (convertView.getMeasuredHeight() / 2));
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);
@@ -176,7 +180,7 @@ public class MarkerViewManager {
for (final MapboxMap.MarkerViewAdapter adapter : markerViewAdapters) {
if (adapter.getMarkerClass() == marker.getClass()) {
convertView = (View) adapter.getViewReusePool().acquire();
View adaptedView = adapter.getView(marker, convertView, mapView);
final View adaptedView = adapter.getView(marker, convertView, mapView);
if (adaptedView != null) {
// tilt
@@ -210,11 +214,11 @@ public class MarkerViewManager {
}
if (!clickHandled) {
// InfoWindow offset
Point infoWindowOffset = marker.getInfoWindowOffset();
marker.setTopOffsetPixels(-(v.getHeight() / 2) - infoWindowOffset.y);
marker.setRightOffsetPixels(infoWindowOffset.x);
int infoWindowOffsetX = (int) ((adaptedView.getWidth() * marker.getInfoWindowAnchorU()) - marker.getOffsetX());
int infoWindowOffsetY = (int) ((adaptedView.getHeight() * marker.getInfoWindowAnchorV()) -marker.getOffsetY());
marker.setTopOffsetPixels(infoWindowOffsetY);
marker.setRightOffsetPixels(infoWindowOffsetX);
if (animSelectRes != 0) {
AnimatorUtils.animate(v, animSelectRes, new AnimatorUtils.OnAnimationEndListener() {

View File

@@ -1,8 +1,6 @@
package com.mapbox.mapboxsdk.annotations;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.PointF;
import android.os.Parcel;
import android.os.Parcelable;
@@ -22,8 +20,8 @@ public class MarkerViewOptions extends BaseMarkerViewOptions<MarkerView, MarkerV
snippet(in.readString());
title(in.readString());
flat(in.readByte() != 0);
centerOffset((PointF) in.readParcelable(PointF.class.getClassLoader()));
infoWindowOffset((Point) in.readParcelable(Point.class.getClassLoader()));
anchor(in.readFloat(), in.readFloat());
infoWindowAnchor(in.readFloat(), in.readFloat());
selectAnimatorResource(in.readInt());
deselectAnimatorResource(in.readInt());
rotation(in.readInt());
@@ -53,8 +51,10 @@ public class MarkerViewOptions extends BaseMarkerViewOptions<MarkerView, MarkerV
out.writeString(getSnippet());
out.writeString(getTitle());
out.writeByte((byte) (isFlat() ? 1 : 0));
out.writeParcelable(getCenterOffset(), flags);
out.writeParcelable(getInfoWindowOffset(), flags);
out.writeFloat(getAnchorU());
out.writeFloat(getAnchorV());
out.writeFloat(getInfoWindowAnchorU());
out.writeFloat(getInfoWindowAnchorV());
out.writeInt(getSelectAnimRes());
out.writeInt(getDeselectAnimRes());
out.writeInt(getRotation());
@@ -74,8 +74,8 @@ public class MarkerViewOptions extends BaseMarkerViewOptions<MarkerView, MarkerV
marker.setTitle(title);
marker.setIcon(icon);
marker.setFlat(flat);
marker.setCenterOffset(centerOffset);
marker.setInfoWindowOffset(infoWindowOffset);
marker.setAnchor(anchorU, anchorV);
marker.setInfoWindowAnchor(infoWindowAnchorU, infoWindowAnchorV);
marker.setSelectAnimRes(selectAnimRes);
marker.setDeselectAnimRes(deselectAnimRes);
marker.setRotation(rotation);

View File

@@ -73,7 +73,6 @@ public class MarkerViewActivity extends AppCompatActivity {
mMapboxMap.addMarker(new MarkerViewOptions()
.position(LAT_LNGS[i])
.title(String.valueOf(i))
.infoWindowOffset(new Point(0, (int) getResources().getDimension(R.dimen.fab_margin)))
.selectAnimatorResource(R.animator.scale_up)
.deselectAnimatorResource(R.animator.scale_down)
);

View File

@@ -9,6 +9,7 @@ import android.os.Parcelable;
import com.mapbox.mapboxsdk.annotations.BaseMarkerViewOptions;
import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.IconFactory;
import com.mapbox.mapboxsdk.annotations.MarkerView;
import com.mapbox.mapboxsdk.geometry.LatLng;
public class CountryMarkerViewOptions extends BaseMarkerViewOptions<CountryMarkerView, CountryMarkerViewOptions> {
@@ -24,10 +25,11 @@ public class CountryMarkerViewOptions extends BaseMarkerViewOptions<CountryMarke
snippet(in.readString());
title(in.readString());
flat(in.readByte() != 0);
centerOffset((PointF) in.readParcelable(PointF.class.getClassLoader()));
infoWindowOffset((Point) in.readParcelable(Point.class.getClassLoader()));
anchor(in.readFloat(), in.readFloat());
infoWindowAnchor(in.readFloat(), in.readFloat());
selectAnimatorResource(in.readInt());
deselectAnimatorResource(in.readInt());
rotation(in.readInt());
if (in.readByte() != 0) {
// this means we have an icon
String iconId = in.readString();
@@ -35,6 +37,8 @@ public class CountryMarkerViewOptions extends BaseMarkerViewOptions<CountryMarke
Icon icon = IconFactory.recreate(iconId, iconBitmap);
icon(icon);
}
abbrevName(in.readString());
flagRes(in.readInt());
}
@Override
@@ -53,16 +57,21 @@ public class CountryMarkerViewOptions extends BaseMarkerViewOptions<CountryMarke
out.writeString(getSnippet());
out.writeString(getTitle());
out.writeByte((byte) (isFlat() ? 1 : 0));
out.writeParcelable(getCenterOffset(), flags);
out.writeParcelable(getInfoWindowOffset(), flags);
out.writeFloat(getAnchorU());
out.writeFloat(getAnchorV());
out.writeFloat(getInfoWindowAnchorU());
out.writeFloat(getInfoWindowAnchorV());
out.writeInt(getSelectAnimRes());
out.writeInt(getDeselectAnimRes());
out.writeInt(getRotation());
Icon icon = getIcon();
out.writeByte((byte) (icon != null ? 1 : 0));
if (icon != null) {
out.writeString(getIcon().getId());
out.writeParcelable(getIcon().getBitmap(), flags);
}
out.writeString(abbrevName);
out.writeInt(flagRes);
}
@Override
@@ -80,38 +89,6 @@ public class CountryMarkerViewOptions extends BaseMarkerViewOptions<CountryMarke
return getThis();
}
public LatLng getPosition() {
return position;
}
public String getSnippet() {
return snippet;
}
public String getTitle() {
return title;
}
public boolean isFlat() {
return flat;
}
public PointF getCenterOffset() {
return centerOffset;
}
public Point getInfoWindowOffset() {
return infoWindowOffset;
}
public int getSelectAnimRes() {
return selectAnimRes;
}
public int getDeselectAnimRes() {
return deselectAnimRes;
}
public static final Parcelable.Creator<CountryMarkerViewOptions> CREATOR
= new Parcelable.Creator<CountryMarkerViewOptions>() {
public CountryMarkerViewOptions createFromParcel(Parcel in) {