mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-02-10 22:45:24 +08:00
Expose screen metrics and window metrics
Summary: public https://github.com/facebook/react-native/pull/4935 changed the window dimensions for android by replacing them with the actual screen dimensions. This changes the window dimensions back to their original values and adds `Dimensions.get('screen')` for the actual screen dimensions of the device. Reviewed By: astreet Differential Revision: D2921584 fb-gh-sync-id: 5d2677029c71d50691691dc651a11e9c8b115e8f shipit-source-id: 5d2677029c71d50691691dc651a11e9c8b115e8f
This commit is contained in:
committed by
facebook-github-bot-3
parent
6f1417c849
commit
228a1fe7d4
@@ -308,8 +308,11 @@ import static com.facebook.react.bridge.ReactMarkerConstants.*;
|
||||
}
|
||||
|
||||
private static void setDisplayMetrics(Context context) {
|
||||
DisplayMetrics displayMetrics = new DisplayMetrics();
|
||||
displayMetrics.setTo(context.getResources().getDisplayMetrics());
|
||||
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
|
||||
DisplayMetricsHolder.setWindowDisplayMetrics(displayMetrics);
|
||||
|
||||
DisplayMetrics screenDisplayMetrics = new DisplayMetrics();
|
||||
screenDisplayMetrics.setTo(displayMetrics);
|
||||
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
Display display = wm.getDefaultDisplay();
|
||||
|
||||
@@ -317,9 +320,8 @@ import static com.facebook.react.bridge.ReactMarkerConstants.*;
|
||||
// The real metrics include system decor elements (e.g. soft menu bar).
|
||||
//
|
||||
// See: http://developer.android.com/reference/android/view/Display.html#getRealMetrics(android.util.DisplayMetrics)
|
||||
if (Build.VERSION.SDK_INT >= 17){
|
||||
display.getRealMetrics(displayMetrics);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 17) {
|
||||
display.getRealMetrics(screenDisplayMetrics);
|
||||
} else {
|
||||
// For 14 <= API level <= 16, we need to invoke getRawHeight and getRawWidth to get the real dimensions.
|
||||
// Since react-native only supports API level 16+ we don't have to worry about other cases.
|
||||
@@ -330,13 +332,13 @@ import static com.facebook.react.bridge.ReactMarkerConstants.*;
|
||||
try {
|
||||
Method mGetRawH = Display.class.getMethod("getRawHeight");
|
||||
Method mGetRawW = Display.class.getMethod("getRawWidth");
|
||||
displayMetrics.widthPixels = (Integer) mGetRawW.invoke(display);
|
||||
displayMetrics.heightPixels = (Integer) mGetRawH.invoke(display);
|
||||
screenDisplayMetrics.widthPixels = (Integer) mGetRawW.invoke(display);
|
||||
screenDisplayMetrics.heightPixels = (Integer) mGetRawH.invoke(display);
|
||||
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
|
||||
throw new RuntimeException("Error getting real dimensions for API level < 17", e);
|
||||
}
|
||||
}
|
||||
DisplayMetricsHolder.setDisplayMetrics(displayMetrics);
|
||||
DisplayMetricsHolder.setScreenDisplayMetrics(screenDisplayMetrics);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user