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

Commit

Permalink
Bugfix finding position of target view with Navigation component
Browse files Browse the repository at this point in the history
  • Loading branch information
mreram committed Aug 16, 2022
1 parent ebc8a47 commit 63c9a04
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ public class GuideView extends FrameLayout {
private final Paint paintCircleInner = new Paint();
private final Paint targetPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Xfermode X_FER_MODE_CLEAR = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
private final Path arrowPath = new Path();

private final View target;
private RectF targetRect;
private final Rect selfRect = new Rect();
private final Rect backgroundRect = new Rect();

private final float density;
private float stopY;
Expand Down Expand Up @@ -127,8 +128,6 @@ private GuideView(Context context, View view) {
)
);

setMessageLocation(resolveMessageViewLocation());

ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Expand All @@ -138,8 +137,6 @@ public void onGlobalLayout() {
getViewTreeObserver().removeGlobalOnLayoutListener(this);
}

setMessageLocation(resolveMessageViewLocation());

if (target instanceof Targetable) {
targetRect = ((Targetable) target).boundingRect();
} else {
Expand All @@ -151,20 +148,29 @@ public void onGlobalLayout() {
locationTarget[0] + target.getWidth(),
locationTarget[1] + target.getHeight()
);
if (isLandscape()) {
targetRect.offset(-getStatusBarHeight(), 0);
}
}

selfRect.set(
backgroundRect.set(
getPaddingLeft(),
getPaddingTop(),
getWidth() - getPaddingRight(),
getHeight() - getPaddingBottom()
);
if (isLandscape()) {
backgroundRect.offset(-getNavigationBarSize(), 0);
} else {
backgroundRect.offset(0, -getNavigationBarSize());
}

isTop = !((targetRect.top + (indicatorHeight)) > getHeight() / 2f);
marginGuide = (int) (isTop ? marginGuide : -marginGuide);
setMessageLocation(resolveMessageViewLocation());
startYLineAndCircle = (isTop ? targetRect.bottom : targetRect.top) + marginGuide;
stopY = yMessageView + indicatorHeight;
stopY = yMessageView + indicatorHeight + (isTop ? -marginGuide : marginGuide);
startAnimationSize();
getViewTreeObserver().addOnGlobalLayoutListener(this);
}
};
getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
Expand All @@ -176,25 +182,19 @@ private void startAnimationSize() {
0f,
circleIndicatorSizeFinal
);
circleSizeAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
circleIndicatorSize = (float) circleSizeAnimator.getAnimatedValue();
circleInnerIndicatorSize = (float) circleSizeAnimator.getAnimatedValue() - density;
postInvalidate();
}
circleSizeAnimator.addUpdateListener(valueAnimator -> {
circleIndicatorSize = (float) circleSizeAnimator.getAnimatedValue();
circleInnerIndicatorSize = (float) circleSizeAnimator.getAnimatedValue() - density;
postInvalidate();
});

final ValueAnimator linePositionAnimator = ValueAnimator.ofFloat(
stopY,
startYLineAndCircle
);
linePositionAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
startYLineAndCircle = (float) linePositionAnimator.getAnimatedValue();
postInvalidate();
}
linePositionAnimator.addUpdateListener(valueAnimator -> {
startYLineAndCircle = (float) linePositionAnimator.getAnimatedValue();
postInvalidate();
});

linePositionAnimator.setDuration(SIZE_ANIMATION_DURATION);
Expand Down Expand Up @@ -234,15 +234,24 @@ private void init() {
circleIndicatorSizeFinal = CIRCLE_INDICATOR_SIZE * density;
}

private int getNavigationBarSize() {
Resources resources = getContext().getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
public int getNavigationBarSize() {
Resources resources = getResources();
int id = resources.getIdentifier("navigation_bar_height_landscape", "dimen", "android");
if (id > 0) {
return resources.getDimensionPixelSize(id);
}
return 0;
}

public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}

private boolean isLandscape() {
int display_mode = getResources().getConfiguration().orientation;
return display_mode != Configuration.ORIENTATION_PORTRAIT;
Expand All @@ -256,7 +265,7 @@ protected void onDraw(final Canvas canvas) {
selfPaint.setColor(BACKGROUND_COLOR);
selfPaint.setStyle(Paint.Style.FILL);
selfPaint.setAntiAlias(true);
canvas.drawRect(selfRect, selfPaint);
canvas.drawRect(backgroundRect, selfPaint);

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

switch (pointerType) {
case circle:
canvas.drawLine(x,startYLineAndCircle,x,stopY,paintLine);
canvas.drawLine(x, startYLineAndCircle, x, stopY, paintLine);
canvas.drawCircle(x, startYLineAndCircle, circleIndicatorSize, paintCircle);
canvas.drawCircle(x, startYLineAndCircle, circleInnerIndicatorSize, paintCircleInner);
canvas.drawCircle(
x,
startYLineAndCircle,
circleInnerIndicatorSize,
paintCircleInner
);
break;
case arrow:
canvas.drawLine(x,startYLineAndCircle,x,stopY,paintLine);
Path path = new Path();
canvas.drawLine(x, startYLineAndCircle, x, stopY, paintLine);
arrowPath.reset();
if (isTop) {
path.moveTo(x, startYLineAndCircle - (circleIndicatorSize * 2));
path.lineTo(x + circleIndicatorSize, startYLineAndCircle);
path.lineTo(x - circleIndicatorSize, startYLineAndCircle);
path.close();
arrowPath.moveTo(x, startYLineAndCircle - (circleIndicatorSize * 2));
} else {
path.moveTo(x, startYLineAndCircle + (circleIndicatorSize * 2));
path.lineTo(x + circleIndicatorSize, startYLineAndCircle);
path.lineTo(x - circleIndicatorSize, startYLineAndCircle);
path.close();
arrowPath.moveTo(x, startYLineAndCircle + (circleIndicatorSize * 2));
}
canvas.drawPath(path, paintCircle);
arrowPath.lineTo(x + circleIndicatorSize, startYLineAndCircle);
arrowPath.lineTo(x - circleIndicatorSize, startYLineAndCircle);
arrowPath.close();
canvas.drawPath(arrowPath, paintCircle);
break;
case none:
//draw no line and no pointer
Expand Down Expand Up @@ -362,7 +373,7 @@ public boolean onTouchEvent(MotionEvent event) {
break;

case outsideTargetAndMessage:
if(!(targetRect.contains(x, y) || isViewContains(mMessageView, x, y))){
if (!(targetRect.contains(x, y) || isViewContains(mMessageView, x, y))) {
dismiss();
}
}
Expand Down Expand Up @@ -401,7 +412,7 @@ private Point resolveMessageViewLocation() {
xMessageView = (int) (targetRect.right) - mMessageView.getWidth();
}

if (isLandscape()) {
if (isLandscape() && (xMessageView + mMessageView.getWidth()) > backgroundRect.right) {
xMessageView -= getNavigationBarSize();
}

Expand Down Expand Up @@ -436,7 +447,6 @@ public void show() {
ViewGroup.LayoutParams.MATCH_PARENT
));
this.setClickable(false);

((ViewGroup) ((Activity) getContext()).getWindow().getDecorView()).addView(this);
AlphaAnimation startAnimation = new AlphaAnimation(0.0f, 1.0f);
startAnimation.setDuration(APPEARING_ANIMATION_DURATION);
Expand Down Expand Up @@ -662,6 +672,7 @@ public Builder setPointerType(PointerType pointerType) {
this.pointerType = pointerType;
return this;
}

public GuideView build() {
GuideView guideView = new GuideView(context, targetView);
guideView.mGravity = gravity != null ? gravity : Gravity.auto;
Expand Down Expand Up @@ -710,5 +721,4 @@ public GuideView build() {
return guideView;
}
}
}

}
4 changes: 0 additions & 4 deletions showcaseviewlib/src/main/res/values/styles.xml

This file was deleted.

0 comments on commit 63c9a04

Please sign in to comment.