Skip to content

Commit 8cdf4e6

Browse files
committed
Fixed overlay position inside Dialog
1 parent 9d8d3ff commit 8cdf4e6

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

library/src/main/java/io/github/douglasjunior/androidSimpleTooltip/OverlayView.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ public class OverlayView extends View {
4949

5050
private View mAnchorView;
5151
private Bitmap bitmap;
52+
private float offset = 0;
5253

5354
OverlayView(Context context, View anchorView) {
5455
super(context);
5556
this.mAnchorView = anchorView;
57+
this.offset = context.getResources().getDimension(mDefaultOverlayCircleOffsetRes);
5658
}
5759

5860
@Override
@@ -67,10 +69,10 @@ protected void dispatchDraw(Canvas canvas) {
6769
}
6870

6971
private void createWindowFrame() {
70-
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
72+
bitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
7173
Canvas osCanvas = new Canvas(bitmap);
7274

73-
RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight());
75+
RectF outerRectangle = new RectF(0, 0, getMeasuredWidth(), getMeasuredHeight());
7476

7577
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
7678
paint.setColor(Color.BLACK);
@@ -81,10 +83,14 @@ private void createWindowFrame() {
8183
paint.setColor(Color.TRANSPARENT);
8284
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
8385

84-
RectF rect = SimpleTooltipUtils.calculeRectOnScreen(mAnchorView);
85-
float offset = getResources().getDimensionPixelSize(mDefaultOverlayCircleOffsetRes);
86-
rect.set(rect.left - offset, rect.top - offset, rect.right + offset, rect.bottom + offset);
87-
osCanvas.drawOval(rect, paint);
86+
RectF anchorRecr = SimpleTooltipUtils.calculeRectInWindow(mAnchorView);
87+
RectF overlayRecr = SimpleTooltipUtils.calculeRectInWindow(this);
88+
89+
float left = anchorRecr.left - overlayRecr.left;
90+
float top = anchorRecr.top - overlayRecr.top;
91+
RectF oval = new RectF(left - offset, top - offset, left + mAnchorView.getMeasuredWidth() + offset, top + mAnchorView.getMeasuredHeight() + offset);
92+
93+
osCanvas.drawOval(oval, paint);
8894
}
8995

9096
@Override

library/src/main/java/io/github/douglasjunior/androidSimpleTooltip/SimpleTooltip.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,6 @@ private void configPopupWindow() {
159159
public void show() {
160160
verifyDismissed();
161161

162-
createOverlay();
163-
164162
mContentLayout.getViewTreeObserver().addOnGlobalLayoutListener(mLocationLayoutListener);
165163
mContentLayout.getViewTreeObserver().addOnGlobalLayoutListener(mAutoDismissLayoutListener);
166164

@@ -180,7 +178,7 @@ private void verifyDismissed() {
180178

181179
private void createOverlay() {
182180
mOverlay = mTransparentOverlay ? new View(mContext) : new OverlayView(mContext, mAnchorView);
183-
mOverlay.setLayoutParams(new ViewGroup.LayoutParams(mRootView.getMeasuredWidth(), mRootView.getMeasuredHeight()));
181+
mOverlay.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
184182
mOverlay.setOnTouchListener(mOverlayTouchListener);
185183
mRootView.addView(mOverlay);
186184
}
@@ -363,6 +361,7 @@ public void onGlobalLayout() {
363361
mPopupWindow.setClippingEnabled(true);
364362
mPopupWindow.update((int) location.x, (int) location.y, mPopupWindow.getWidth(), mPopupWindow.getHeight());
365363
mPopupWindow.getContentView().requestLayout();
364+
createOverlay();
366365
}
367366
};
368367

library/src/main/java/io/github/douglasjunior/androidSimpleTooltip/SimpleTooltipUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ private SimpleTooltipUtils() {
4949
public static RectF calculeRectOnScreen(View view) {
5050
int[] location = new int[2];
5151
view.getLocationOnScreen(location);
52-
return new RectF(location[0], location[1], location[0] + view.getWidth(), location[1] + view.getHeight());
52+
return new RectF(location[0], location[1], location[0] + view.getMeasuredWidth(), location[1] + view.getMeasuredHeight());
5353
}
5454

5555
public static RectF calculeRectInWindow(View view) {
5656
int[] location = new int[2];
5757
view.getLocationInWindow(location);
58-
return new RectF(location[0], location[1], location[0] + view.getWidth(), location[1] + view.getHeight());
58+
return new RectF(location[0], location[1], location[0] + view.getMeasuredWidth(), location[1] + view.getMeasuredHeight());
5959
}
6060

6161
public static float dpFromPx(float px) {

sample/src/main/java/io/github/douglasjunior/androidSimpleTooltip/sample/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ public void onClick(View v) {
227227
.text(getString(R.string.btn_in_dialog))
228228
.gravity(Gravity.BOTTOM)
229229
.animated(true)
230+
.transparentOverlay(false)
230231
.build()
231232
.show();
232233
}

0 commit comments

Comments
 (0)