Skip to content
This repository was archived by the owner on Nov 10, 2024. It is now read-only.

Commit 63c9a04

Browse files
committed
Bugfix finding position of target view with Navigation component
1 parent ebc8a47 commit 63c9a04

File tree

2 files changed

+55
-49
lines changed

2 files changed

+55
-49
lines changed

showcaseviewlib/src/main/java/smartdevelop/ir/eram/showcaseviewlib/GuideView.java

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ public class GuideView extends FrameLayout {
6060
private final Paint paintCircleInner = new Paint();
6161
private final Paint targetPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
6262
private final Xfermode X_FER_MODE_CLEAR = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
63+
private final Path arrowPath = new Path();
6364

6465
private final View target;
6566
private RectF targetRect;
66-
private final Rect selfRect = new Rect();
67+
private final Rect backgroundRect = new Rect();
6768

6869
private final float density;
6970
private float stopY;
@@ -127,8 +128,6 @@ private GuideView(Context context, View view) {
127128
)
128129
);
129130

130-
setMessageLocation(resolveMessageViewLocation());
131-
132131
ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
133132
@Override
134133
public void onGlobalLayout() {
@@ -138,8 +137,6 @@ public void onGlobalLayout() {
138137
getViewTreeObserver().removeGlobalOnLayoutListener(this);
139138
}
140139

141-
setMessageLocation(resolveMessageViewLocation());
142-
143140
if (target instanceof Targetable) {
144141
targetRect = ((Targetable) target).boundingRect();
145142
} else {
@@ -151,20 +148,29 @@ public void onGlobalLayout() {
151148
locationTarget[0] + target.getWidth(),
152149
locationTarget[1] + target.getHeight()
153150
);
151+
if (isLandscape()) {
152+
targetRect.offset(-getStatusBarHeight(), 0);
153+
}
154154
}
155155

156-
selfRect.set(
156+
backgroundRect.set(
157157
getPaddingLeft(),
158158
getPaddingTop(),
159159
getWidth() - getPaddingRight(),
160160
getHeight() - getPaddingBottom()
161161
);
162+
if (isLandscape()) {
163+
backgroundRect.offset(-getNavigationBarSize(), 0);
164+
} else {
165+
backgroundRect.offset(0, -getNavigationBarSize());
166+
}
162167

168+
isTop = !((targetRect.top + (indicatorHeight)) > getHeight() / 2f);
163169
marginGuide = (int) (isTop ? marginGuide : -marginGuide);
170+
setMessageLocation(resolveMessageViewLocation());
164171
startYLineAndCircle = (isTop ? targetRect.bottom : targetRect.top) + marginGuide;
165-
stopY = yMessageView + indicatorHeight;
172+
stopY = yMessageView + indicatorHeight + (isTop ? -marginGuide : marginGuide);
166173
startAnimationSize();
167-
getViewTreeObserver().addOnGlobalLayoutListener(this);
168174
}
169175
};
170176
getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
@@ -176,25 +182,19 @@ private void startAnimationSize() {
176182
0f,
177183
circleIndicatorSizeFinal
178184
);
179-
circleSizeAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
180-
@Override
181-
public void onAnimationUpdate(ValueAnimator valueAnimator) {
182-
circleIndicatorSize = (float) circleSizeAnimator.getAnimatedValue();
183-
circleInnerIndicatorSize = (float) circleSizeAnimator.getAnimatedValue() - density;
184-
postInvalidate();
185-
}
185+
circleSizeAnimator.addUpdateListener(valueAnimator -> {
186+
circleIndicatorSize = (float) circleSizeAnimator.getAnimatedValue();
187+
circleInnerIndicatorSize = (float) circleSizeAnimator.getAnimatedValue() - density;
188+
postInvalidate();
186189
});
187190

188191
final ValueAnimator linePositionAnimator = ValueAnimator.ofFloat(
189192
stopY,
190193
startYLineAndCircle
191194
);
192-
linePositionAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
193-
@Override
194-
public void onAnimationUpdate(ValueAnimator valueAnimator) {
195-
startYLineAndCircle = (float) linePositionAnimator.getAnimatedValue();
196-
postInvalidate();
197-
}
195+
linePositionAnimator.addUpdateListener(valueAnimator -> {
196+
startYLineAndCircle = (float) linePositionAnimator.getAnimatedValue();
197+
postInvalidate();
198198
});
199199

200200
linePositionAnimator.setDuration(SIZE_ANIMATION_DURATION);
@@ -234,15 +234,24 @@ private void init() {
234234
circleIndicatorSizeFinal = CIRCLE_INDICATOR_SIZE * density;
235235
}
236236

237-
private int getNavigationBarSize() {
238-
Resources resources = getContext().getResources();
239-
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
240-
if (resourceId > 0) {
241-
return resources.getDimensionPixelSize(resourceId);
237+
public int getNavigationBarSize() {
238+
Resources resources = getResources();
239+
int id = resources.getIdentifier("navigation_bar_height_landscape", "dimen", "android");
240+
if (id > 0) {
241+
return resources.getDimensionPixelSize(id);
242242
}
243243
return 0;
244244
}
245245

246+
public int getStatusBarHeight() {
247+
int result = 0;
248+
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
249+
if (resourceId > 0) {
250+
result = getResources().getDimensionPixelSize(resourceId);
251+
}
252+
return result;
253+
}
254+
246255
private boolean isLandscape() {
247256
int display_mode = getResources().getConfiguration().orientation;
248257
return display_mode != Configuration.ORIENTATION_PORTRAIT;
@@ -256,7 +265,7 @@ protected void onDraw(final Canvas canvas) {
256265
selfPaint.setColor(BACKGROUND_COLOR);
257266
selfPaint.setStyle(Paint.Style.FILL);
258267
selfPaint.setAntiAlias(true);
259-
canvas.drawRect(selfRect, selfPaint);
268+
canvas.drawRect(backgroundRect, selfPaint);
260269

261270
paintLine.setStyle(Paint.Style.FILL);
262271
paintLine.setColor(LINE_INDICATOR_COLOR);
@@ -277,25 +286,27 @@ protected void onDraw(final Canvas canvas) {
277286

278287
switch (pointerType) {
279288
case circle:
280-
canvas.drawLine(x,startYLineAndCircle,x,stopY,paintLine);
289+
canvas.drawLine(x, startYLineAndCircle, x, stopY, paintLine);
281290
canvas.drawCircle(x, startYLineAndCircle, circleIndicatorSize, paintCircle);
282-
canvas.drawCircle(x, startYLineAndCircle, circleInnerIndicatorSize, paintCircleInner);
291+
canvas.drawCircle(
292+
x,
293+
startYLineAndCircle,
294+
circleInnerIndicatorSize,
295+
paintCircleInner
296+
);
283297
break;
284298
case arrow:
285-
canvas.drawLine(x,startYLineAndCircle,x,stopY,paintLine);
286-
Path path = new Path();
299+
canvas.drawLine(x, startYLineAndCircle, x, stopY, paintLine);
300+
arrowPath.reset();
287301
if (isTop) {
288-
path.moveTo(x, startYLineAndCircle - (circleIndicatorSize * 2));
289-
path.lineTo(x + circleIndicatorSize, startYLineAndCircle);
290-
path.lineTo(x - circleIndicatorSize, startYLineAndCircle);
291-
path.close();
302+
arrowPath.moveTo(x, startYLineAndCircle - (circleIndicatorSize * 2));
292303
} else {
293-
path.moveTo(x, startYLineAndCircle + (circleIndicatorSize * 2));
294-
path.lineTo(x + circleIndicatorSize, startYLineAndCircle);
295-
path.lineTo(x - circleIndicatorSize, startYLineAndCircle);
296-
path.close();
304+
arrowPath.moveTo(x, startYLineAndCircle + (circleIndicatorSize * 2));
297305
}
298-
canvas.drawPath(path, paintCircle);
306+
arrowPath.lineTo(x + circleIndicatorSize, startYLineAndCircle);
307+
arrowPath.lineTo(x - circleIndicatorSize, startYLineAndCircle);
308+
arrowPath.close();
309+
canvas.drawPath(arrowPath, paintCircle);
299310
break;
300311
case none:
301312
//draw no line and no pointer
@@ -362,7 +373,7 @@ public boolean onTouchEvent(MotionEvent event) {
362373
break;
363374

364375
case outsideTargetAndMessage:
365-
if(!(targetRect.contains(x, y) || isViewContains(mMessageView, x, y))){
376+
if (!(targetRect.contains(x, y) || isViewContains(mMessageView, x, y))) {
366377
dismiss();
367378
}
368379
}
@@ -401,7 +412,7 @@ private Point resolveMessageViewLocation() {
401412
xMessageView = (int) (targetRect.right) - mMessageView.getWidth();
402413
}
403414

404-
if (isLandscape()) {
415+
if (isLandscape() && (xMessageView + mMessageView.getWidth()) > backgroundRect.right) {
405416
xMessageView -= getNavigationBarSize();
406417
}
407418

@@ -436,7 +447,6 @@ public void show() {
436447
ViewGroup.LayoutParams.MATCH_PARENT
437448
));
438449
this.setClickable(false);
439-
440450
((ViewGroup) ((Activity) getContext()).getWindow().getDecorView()).addView(this);
441451
AlphaAnimation startAnimation = new AlphaAnimation(0.0f, 1.0f);
442452
startAnimation.setDuration(APPEARING_ANIMATION_DURATION);
@@ -662,6 +672,7 @@ public Builder setPointerType(PointerType pointerType) {
662672
this.pointerType = pointerType;
663673
return this;
664674
}
675+
665676
public GuideView build() {
666677
GuideView guideView = new GuideView(context, targetView);
667678
guideView.mGravity = gravity != null ? gravity : Gravity.auto;
@@ -710,5 +721,4 @@ public GuideView build() {
710721
return guideView;
711722
}
712723
}
713-
}
714-
724+
}

showcaseviewlib/src/main/res/values/styles.xml

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)