Browse Source

Fixed screen size calculations when in multi-window view (#1432)

Brendon Sled 7 years ago
parent
commit
932c3c63c0

+ 3
- 5
android/app/src/main/java/com/reactnativenavigation/screens/ScreenAnimator.java View File

@@ -8,16 +8,14 @@ import android.view.View;
8 8
 import android.view.animation.AccelerateInterpolator;
9 9
 import android.view.animation.DecelerateInterpolator;
10 10
 import android.view.animation.LinearInterpolator;
11
-
12 11
 import com.reactnativenavigation.NavigationApplication;
13 12
 import com.reactnativenavigation.utils.ViewUtils;
14 13
 import com.reactnativenavigation.views.sharedElementTransition.SharedElementsAnimator;
15 14
 
15
+import javax.annotation.Nullable;
16 16
 import java.util.ArrayList;
17 17
 import java.util.List;
18 18
 
19
-import javax.annotation.Nullable;
20
-
21 19
 class ScreenAnimator {
22 20
     private final float translationY;
23 21
     private final float translationX;
@@ -25,8 +23,8 @@ class ScreenAnimator {
25 23
 
26 24
     ScreenAnimator(Screen screen) {
27 25
         this.screen = screen;
28
-        translationY = 0.08f * ViewUtils.getScreenHeight();
29
-        translationX = 0.08f * ViewUtils.getScreenWidth();
26
+        translationY = 0.08f * ViewUtils.getWindowHeight(screen.activity);
27
+        translationX = 0.08f * ViewUtils.getWindowWidth(screen.activity);
30 28
     }
31 29
 
32 30
     public void show(boolean animate, final Runnable onAnimationEnd) {

+ 6
- 10
android/app/src/main/java/com/reactnativenavigation/utils/ViewUtils.java View File

@@ -1,6 +1,6 @@
1 1
 package com.reactnativenavigation.utils;
2 2
 
3
-import android.content.Context;
3
+import android.app.Activity;
4 4
 import android.content.res.Resources;
5 5
 import android.graphics.PorterDuff;
6 6
 import android.graphics.PorterDuffColorFilter;
@@ -16,10 +16,8 @@ import android.view.View;
16 16
 import android.view.ViewGroup;
17 17
 import android.view.ViewParent;
18 18
 import android.view.ViewTreeObserver;
19
-import android.view.WindowManager;
20 19
 import android.widget.RelativeLayout;
21 20
 import android.widget.TextView;
22
-
23 21
 import com.reactnativenavigation.NavigationApplication;
24 22
 import com.reactnativenavigation.params.AppStyle;
25 23
 import com.reactnativenavigation.screens.Screen;
@@ -74,17 +72,15 @@ public class ViewUtils {
74 72
         }
75 73
     }
76 74
 
77
-    public static float getScreenHeight() {
78
-        WindowManager wm = (WindowManager) NavigationApplication.instance.getSystemService(Context.WINDOW_SERVICE);
75
+    public static float getWindowWidth(Activity activity) {
79 76
         DisplayMetrics metrics = new DisplayMetrics();
80
-        wm.getDefaultDisplay().getMetrics(metrics);
81
-        return metrics.heightPixels;
77
+        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
78
+        return metrics.widthPixels;
82 79
     }
83 80
 
84
-    public static float getScreenWidth() {
85
-        WindowManager wm = (WindowManager) NavigationApplication.instance.getSystemService(Context.WINDOW_SERVICE);
81
+    public static float getWindowHeight(Activity activity) {
86 82
         DisplayMetrics metrics = new DisplayMetrics();
87
-        wm.getDefaultDisplay().getMetrics(metrics);
83
+        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
88 84
         return metrics.widthPixels;
89 85
     }
90 86
 

+ 14
- 2
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java View File

@@ -2,7 +2,9 @@ package com.reactnativenavigation.views;
2 2
 
3 3
 import android.animation.Animator;
4 4
 import android.animation.AnimatorListenerAdapter;
5
+import android.app.Activity;
5 6
 import android.content.Context;
7
+import android.content.ContextWrapper;
6 8
 import android.graphics.drawable.Drawable;
7 9
 import android.support.annotation.Nullable;
8 10
 import android.support.v7.widget.ActionMenuView;
@@ -12,7 +14,6 @@ import android.view.View;
12 14
 import android.view.animation.AccelerateDecelerateInterpolator;
13 15
 import android.view.animation.AccelerateInterpolator;
14 16
 import android.widget.TextView;
15
-
16 17
 import com.reactnativenavigation.params.BaseScreenParams;
17 18
 import com.reactnativenavigation.params.BaseTitleBarButtonParams;
18 19
 import com.reactnativenavigation.params.StyleParams;
@@ -84,6 +85,17 @@ public class TitleBar extends Toolbar {
84 85
         setVisibility(titleBarHidden ? GONE : VISIBLE);
85 86
     }
86 87
 
88
+    private Activity getActivity() {
89
+        Context context = getContext();
90
+        while (context instanceof ContextWrapper) {
91
+            if (context instanceof Activity) {
92
+                return (Activity)context;
93
+            }
94
+            context = ((ContextWrapper)context).getBaseContext();
95
+        }
96
+        return null;
97
+    }
98
+
87 99
     private void centerTitle(final StyleParams params) {
88 100
         final View titleView = getTitleView();
89 101
         if (titleView == null) {
@@ -93,7 +105,7 @@ public class TitleBar extends Toolbar {
93 105
             @Override
94 106
             public void run() {
95 107
                 if (params.titleBarTitleTextCentered) {
96
-                    titleView.setX(ViewUtils.getScreenWidth() / 2 - titleView.getWidth() / 2);
108
+                    titleView.setX(ViewUtils.getWindowWidth(getActivity()) / 2 - titleView.getWidth() / 2);
97 109
                 }
98 110
                 
99 111
             }