From b81d2b8693cd080e23abd0513f49c6b81ad1102b Mon Sep 17 00:00:00 2001 From: adwait Date: Sat, 14 Aug 2021 12:08:19 +0530 Subject: [PATCH] Fixed misalignment of GuideView and Skip on orientation change --- .../eram/showcaseview/CircleView.java | 3 +- .../ir/eram/showcaseviewlib/GuideView.java | 38 ++++++++++++++++--- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/ir/smartdevelop/eram/showcaseview/CircleView.java b/app/src/main/java/ir/smartdevelop/eram/showcaseview/CircleView.java index ca9bfa9..d13dd89 100644 --- a/app/src/main/java/ir/smartdevelop/eram/showcaseview/CircleView.java +++ b/app/src/main/java/ir/smartdevelop/eram/showcaseview/CircleView.java @@ -87,8 +87,7 @@ protected void onDraw(Canvas canvas) _paint.setColor(_circleColor); canvas.drawCircle(cx, cy, radius, _paint); - - getLocationOnScreen(locationTarget); + getLocationInWindow(locationTarget); int centerX = pl + halfUsableWidth + locationTarget[0]; int centerY = pt + halfUsableHeight + locationTarget[1]; _guidePath.reset(); diff --git a/showcaseviewlib/src/main/java/smartdevelop/ir/eram/showcaseviewlib/GuideView.java b/showcaseviewlib/src/main/java/smartdevelop/ir/eram/showcaseviewlib/GuideView.java index 1f1d2f7..a6baab7 100644 --- a/showcaseviewlib/src/main/java/smartdevelop/ir/eram/showcaseviewlib/GuideView.java +++ b/showcaseviewlib/src/main/java/smartdevelop/ir/eram/showcaseviewlib/GuideView.java @@ -20,10 +20,13 @@ import android.graphics.Xfermode; import android.os.Build; import android.text.Spannable; +import android.view.Display; import android.view.MotionEvent; +import android.view.Surface; import android.view.View; import android.view.ViewGroup; import android.view.ViewTreeObserver; +import android.view.WindowManager; import android.view.animation.AlphaAnimation; import android.widget.FrameLayout; import android.widget.TextView; @@ -133,7 +136,6 @@ private GuideView(Context context, View view) { skipParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); ((LayoutParams)skipParams).setMargins(0,0,10,140); ((LayoutParams)skipParams).gravity = android.view.Gravity.RIGHT | android.view.Gravity.BOTTOM; - skipButton.setLayoutParams(skipParams); mMessageView.setPadding( messageViewPadding, @@ -157,9 +159,6 @@ public void onClick(View view) { dismiss(lastTargetView); } }); - if(setSkip) { - addView(skipButton, skipParams); - } setMessageLocation(resolveMessageViewLocation()); ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() { @@ -177,7 +176,7 @@ public void onGlobalLayout() { targetRect = ((Targetable) target).boundingRect(); } else { int[] locationTarget = new int[2]; - target.getLocationOnScreen(locationTarget); + target.getLocationInWindow(locationTarget); targetRect = new RectF( locationTarget[0], locationTarget[1], @@ -281,6 +280,17 @@ private boolean isLandscape() { return display_mode != Configuration.ORIENTATION_PORTRAIT; } + private int checkOrientation(){ + try{ + Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); + int rotation = display.getRotation(); + return rotation; + } + catch (Exception e){ + return Surface.ROTATION_0; + } + } + @Override protected void onDraw(final Canvas canvas) { super.onDraw(canvas); @@ -475,6 +485,24 @@ public void show() { this.startAnimation(startAnimation); mIsShowing = true; if(setSkip) { + + switch(checkOrientation()){ + case Surface.ROTATION_0: + ((LayoutParams)skipParams).setMargins(0,0,10,140); + ((LayoutParams)skipParams).gravity = android.view.Gravity.RIGHT | android.view.Gravity.BOTTOM; + break; + + case Surface.ROTATION_90: + ((LayoutParams)skipParams).setMargins(10,0,0,0); + ((LayoutParams)skipParams).gravity = android.view.Gravity.LEFT | android.view.Gravity.BOTTOM; + break; + + case Surface.ROTATION_270: + ((LayoutParams)skipParams).setMargins(0,0,10,0); + ((LayoutParams)skipParams).gravity = android.view.Gravity.RIGHT | android.view.Gravity.BOTTOM; + break; + } + addView(skipButton, skipParams); } }