[android] #2273 - Added InfoWindowView, Refactored InfoWindow to take in account margins/offset, exposed margin of InfoWindow as configurable

This commit is contained in:
Tobrun
2015-10-12 18:17:57 +02:00
parent c010c3aef8
commit 981ac4038d
16 changed files with 292 additions and 66 deletions

View File

@@ -1,12 +1,15 @@
package com.mapbox.mapboxsdk.annotations;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.PointF;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.views.MapView;
@@ -28,10 +31,7 @@ public final class InfoWindow {
public InfoWindow(int layoutResId, MapView mapView) {
mMapView = mapView;
mIsVisible = false;
Context context = mapView.getContext();
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(layoutResId, (ViewGroup) mapView, false);
mView = LayoutInflater.from(mapView.getContext()).inflate(layoutResId, mapView, false);
if (mTitleId == 0) {
setResIds(mapView.getContext());
@@ -52,10 +52,6 @@ public final class InfoWindow {
public InfoWindow(View view, MapView mapView) {
mMapView = mapView;
mIsVisible = false;
ViewGroup parent = (ViewGroup) mapView.getParent();
Context context = mapView.getContext();
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = view;
// default behavior: close it when clicking on the tooltip:
@@ -83,13 +79,67 @@ public final class InfoWindow {
MapView.LayoutParams lp = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT);
mView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
// Calculate default Android x,y coordinate
PointF coords = mMapView.toScreenLocation(position);
double y = mMapView.getTopOffsetPixelsForAnnotationSymbol(object.getSprite());
float x = coords.x - (mView.getMeasuredWidth() / 2);
float y = (float) mMapView.getTopOffsetPixelsForAnnotationSymbol(object.getSprite());
y = y * mMapView.getScreenDensity();
y += coords.y - mView.getMeasuredHeight();
lp.leftMargin = (int) coords.x - (mView.getMeasuredWidth() / 2);
// Add y because it's a negative value
lp.topMargin = (int) coords.y - mView.getMeasuredHeight() + (int) y;
// if getTopOffsetPixelsForAnnotationSymbol lands
// float x = coords.x - (mView.getMeasuredWidth() / 2) + offsetX;
// float y = coords.y - mView.getMeasuredHeight() + offsetY;
// get right/left popup window
float right = x + mView.getMeasuredWidth();
float left = x;
// get right/left map view
float mapRight = mMapView.getRight();
float mapLeft = mMapView.getLeft();
if (mView instanceof InfoWindowView) {
// only apply repositioning/margin for InfoWindowView
Resources resources = mMapView.getContext().getResources();
float margin = resources.getDimension(R.dimen.infowindow_margin);
float tipViewOffset = resources.getDimension(R.dimen.infowindow_tipview_width) / 2;
float tipViewMarginLeft = mView.getMeasuredWidth() / 2 - tipViewOffset;
// fit screen on right
if (right > mMapView.getRight()) {
x -= right - mapRight;
tipViewMarginLeft += right - mapRight + tipViewOffset;
right = x + mView.getMeasuredWidth();
}
// fit screen left
if (left < mMapView.getLeft()) {
x += mapLeft - left;
tipViewMarginLeft -= mapLeft - left + tipViewOffset;
left = x;
}
// Add margin right
if (mapRight - right < margin) {
x -= margin - (mapRight - right);
tipViewMarginLeft += margin - (mapRight - right) - tipViewOffset;
left = x;
}
// Add margin left
if (left - mapLeft < margin) {
x += margin - (left - mapLeft);
tipViewMarginLeft -= (margin - (left - mapLeft)) - tipViewOffset;
}
// Adjust tipView
InfoWindowView infoWindowView = (InfoWindowView) mView;
infoWindowView.setTipViewMarginLeft((int) tipViewMarginLeft);
}
// set anchor popupwindowview
mView.setX(x);
mView.setY(y);
close(); //if it was already opened
mMapView.addView(mView, lp);

View File

@@ -0,0 +1,36 @@
package com.mapbox.mapboxsdk.annotations;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import com.mapbox.mapboxsdk.R;
public class InfoWindowView extends RelativeLayout{
private InfoWindowTipView mTipView;
public InfoWindowView(Context context) {
this(context, null);
}
public InfoWindowView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public InfoWindowView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize(context);
}
private void initialize(Context context){
LayoutInflater.from(context).inflate(R.layout.infowindow_content, this);
mTipView = (InfoWindowTipView) findViewById(R.id.infowindow_tipview);
}
public void setTipViewMarginLeft(int marginLeft){
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mTipView.getLayoutParams();
layoutParams.leftMargin = marginLeft;
}
}

View File

@@ -202,7 +202,7 @@ public class Marker extends Annotation {
*/
private InfoWindow getInfoWindow() {
if (infoWindow == null) {
infoWindow = new InfoWindow(R.layout.infowindow, mapView);
infoWindow = new InfoWindow(R.layout.infowindow_view, mapView);
}
return infoWindow;
}

View File

@@ -2,4 +2,5 @@
<resources>
<!--Add references to exposed resources-->
<public name="AttributionAlertDialogStyle" type="style" />
<public name="infowindow_margin" type="dimen" />
</resources>

View File

@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/infowindow_content"
android:background="@color/white"
android:padding="5dp"
android:layout_width="wrap_content"
@@ -45,8 +45,10 @@
</LinearLayout>
<com.mapbox.mapboxsdk.annotations.InfoWindowTipView
android:layout_width="fill_parent"
android:layout_height="10dp"/>
android:id="@+id/infowindow_tipview"
android:layout_below="@+id/infowindow_content"
android:layout_width="@dimen/infowindow_tipview_width"
android:layout_height="8dp"/>
</LinearLayout>
</RelativeLayout>

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<com.mapbox.mapboxsdk.annotations.InfoWindowView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="infowindow_tipview_width">16dp</dimen>
<dimen name="infowindow_margin">8dp</dimen>
</resources>