[android] - #2658 improve window background PopupWindowView

This commit is contained in:
Tobrun
2015-11-13 17:26:53 +01:00
parent b5f7cafab4
commit 566b108248
7 changed files with 79 additions and 42 deletions

View File

@@ -92,45 +92,52 @@ final class InfoWindow {
float y = coords.y - mView.getMeasuredHeight() + offsetY;
// get right/left popup window
float right = x + mView.getMeasuredWidth();
float left = x;
float rightSideInfowWindow = x + mView.getMeasuredWidth();
float leftSideInfoWindow = x;
// get right/left map view
float mapRight = mMapView.get().getRight();
float mapLeft = mMapView.get().getLeft();
final float mapRight = mMapView.get().getRight();
final float mapLeft = mMapView.get().getLeft();
if (mView instanceof InfoWindowView) {
// only apply repositioning/margin for InfoWindowView
Resources resources = mMapView.get().getContext().getResources();
float margin = resources.getDimension(R.dimen.infowindow_margin);
float marginHorizontal = 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.get().getRight()) {
x -= right - mapRight;
tipViewMarginLeft += right - mapRight + tipViewOffset;
right = x + mView.getMeasuredWidth();
boolean outOfBoundsLeft = false;
boolean outOfBoundsRight = false;
// if out of bounds right
if (rightSideInfowWindow > mapRight) {
outOfBoundsRight = true;
x -= rightSideInfowWindow - mapRight;
tipViewMarginLeft += rightSideInfowWindow - mapRight + tipViewOffset;
rightSideInfowWindow = x + mView.getMeasuredWidth();
}
// fit screen left
if (left < mMapView.get().getLeft()) {
x += mapLeft - left;
tipViewMarginLeft -= mapLeft - left + tipViewOffset;
left = x;
if (leftSideInfoWindow < mapLeft) {
outOfBoundsLeft = true;
x += mapLeft - leftSideInfoWindow;
tipViewMarginLeft -= mapLeft - leftSideInfoWindow + tipViewOffset;
leftSideInfoWindow = x;
}
// Add margin right
if (mapRight - right < margin) {
x -= margin - (mapRight - right);
tipViewMarginLeft += margin - (mapRight - right) - tipViewOffset;
left = x;
if (outOfBoundsRight && mapRight - rightSideInfowWindow < marginHorizontal) {
x -= marginHorizontal - (mapRight - rightSideInfowWindow);
tipViewMarginLeft += marginHorizontal - (mapRight - rightSideInfowWindow) - tipViewOffset;
leftSideInfoWindow = x;
}
// Add margin left
if (left - mapLeft < margin) {
x += margin - (left - mapLeft);
tipViewMarginLeft -= (margin - (left - mapLeft)) - tipViewOffset;
if (outOfBoundsLeft && leftSideInfoWindow - mapLeft < marginHorizontal) {
x += marginHorizontal - (leftSideInfoWindow - mapLeft);
tipViewMarginLeft -= (marginHorizontal - (leftSideInfoWindow - mapLeft)) - tipViewOffset;
}
// Adjust tipView

View File

@@ -8,33 +8,55 @@ import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import com.mapbox.mapboxsdk.R;
final class InfoWindowTipView extends View {
private Paint mPaint;
private Path mPath;
private int mLineWidth;
public InfoWindowTipView(Context context, AttributeSet attrs) {
super(context, attrs);
mPath = new Path();
this.mPaint = new Paint();
this.mPaint.setColor(Color.WHITE);
this.mPaint.setAntiAlias(true);
this.mPaint.setStrokeWidth(0.0f);
this.mPaint.setStyle(Paint.Style.FILL);
mLineWidth = (int) context.getResources().getDimension(R.dimen.infowindow_line_width);
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(0.0f);
mPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int height = getMeasuredHeight();
int width = getMeasuredWidth();
mPath.rewind();
mPath.moveTo((width / 2) - height, 0);
mPath.lineTo((width / 2) + height, 0);
this.mPaint.setColor(Color.WHITE);
this.mPaint.setAntiAlias(true);
this.mPaint.setStrokeWidth(0.0f);
this.mPaint.setStyle(Paint.Style.FILL);
mPath.moveTo(0, 0);
mPath.lineTo(width, 0);
mPath.lineTo((width / 2), height);
mPath.lineTo((width / 2) - height, 0);
mPath.lineTo(0, 0);
canvas.drawPath(mPath, this.mPaint);
mPath.rewind();
this.mPaint.setColor(Color.parseColor("#C2C2C2"));
this.mPaint.setAntiAlias(true);
this.mPaint.setStrokeWidth(mLineWidth);
this.mPaint.setStyle(Paint.Style.STROKE);
mPath.moveTo(0, 0);
mPath.lineTo(width / 2, height);
mPath.lineTo(width, 0);
canvas.drawPath(mPath, this.mPaint);
}
}

View File

@@ -32,8 +32,7 @@ class InfoWindowView extends RelativeLayout {
void setTipViewMarginLeft(int marginLeft) {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mTipView.getLayoutParams();
layoutParams.leftMargin = marginLeft;
// This is a bit of a hack but prevents an occasional 1 pixel gap between the InfoWindow and
// the tip
layoutParams.topMargin = -1;
// This is a bit of a hack but prevents an occasional gap between the InfoWindow
layoutParams.topMargin = (int) getResources().getDimension(R.dimen.infowindow_offset);
}
}