Java Code Examples for android.view.Display#getRealMetrics()

The following examples show how to use android.view.Display#getRealMetrics() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: U.java    From Taskbar with Apache License 2.0 6 votes vote down vote up
public static DisplayInfo getExternalDisplayInfo(Context context) {
    Display display = getExternalDisplay(context);
    if(display == null)
        return new DisplayInfo(0, 0, 0, 0);

    DisplayMetrics metrics = new DisplayMetrics();
    display.getRealMetrics(metrics);

    int defaultDensity;
    try {
        defaultDensity = getDefaultDensity(display.getDisplayId());
    } catch (Exception e) {
        defaultDensity = 0;
    }

    return new DisplayInfo(metrics.widthPixels, metrics.heightPixels, metrics.densityDpi, defaultDensity);
}
 
Example 2
Source File: AccessibilityNodeInfoDumper.java    From android-uiconductor with Apache License 2.0 6 votes vote down vote up
public static Point getDevicePhysicalSize() {
  int width;
  int height;
  try {
    UiDevice mDevice = UiDevice.getInstance(getInstrumentation());
    height = mDevice.getDisplayHeight();
    width = mDevice.getDisplayWidth();
    return new Point(width, height);
  } catch (NullPointerException e) {
    Log.e(TAG, "Failed get display size, using getRealMetrics instead. " + e.getMessage());
  }
  WindowManager windowManager = (WindowManager) getInstrumentation().getContext()
      .getSystemService(Service.WINDOW_SERVICE);
  Display display =  windowManager.getDefaultDisplay();
  DisplayMetrics metrics = new DisplayMetrics();
  display.getRealMetrics(metrics);
  height = metrics.heightPixels;
  width = metrics.widthPixels;
  return new Point(width, height);

}
 
Example 3
Source File: OsUtils.java    From PopupWindowCompat with Apache License 2.0 6 votes vote down vote up
public static int getScreenHeight(Activity activity) {
    if (activity == null) {
        return 0;
    }
    Display display = activity.getWindowManager().getDefaultDisplay();
    int realHeight = 0;
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        final DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        realHeight = metrics.heightPixels;
    } else {
        try {
            Method mGetRawH = Display.class.getMethod("getRawHeight");
            realHeight = (Integer) mGetRawH.invoke(display);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return realHeight;
}
 
Example 4
Source File: OsUtils.java    From PopupWindowCompat with Apache License 2.0 6 votes vote down vote up
public static int getScreenWidth(Activity activity) {
    if (activity == null) {
        return 0;
    }
    Display display = activity.getWindowManager().getDefaultDisplay();
    int realWidth = 0;
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        final DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        realWidth = metrics.widthPixels;
    } else {
        try {
            Method mGetRawW = Display.class.getMethod("getRawWidth");
            realWidth = (Integer) mGetRawW.invoke(display);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return realWidth;
}
 
Example 5
Source File: AndroidBarUtils.java    From AndroidBarUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 是否有NavigationBar
 *
 * @param activity 上下文
 * @return 是否有NavigationBar
 */
private static boolean hasNavBar(Activity activity) {
    WindowManager windowManager = activity.getWindowManager();
    Display d = windowManager.getDefaultDisplay();

    DisplayMetrics realDisplayMetrics = new DisplayMetrics();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        d.getRealMetrics(realDisplayMetrics);
    }

    int realHeight = realDisplayMetrics.heightPixels;
    int realWidth = realDisplayMetrics.widthPixels;

    DisplayMetrics displayMetrics = new DisplayMetrics();
    d.getMetrics(displayMetrics);

    int displayHeight = displayMetrics.heightPixels;
    int displayWidth = displayMetrics.widthPixels;

    return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
}
 
Example 6
Source File: DisplayUtils.java    From NoHttp with Apache License 2.0 6 votes vote down vote up
public static void initScreen(Activity activity) {
    if (isInitialize) return;
    isInitialize = true;
    Display display = activity.getWindowManager().getDefaultDisplay();
    DisplayMetrics metric = new DisplayMetrics();
    if (VERSION.SDK_INT >= 17) {
        display.getRealMetrics(metric);
    } else {
        display.getMetrics(metric);
    }

    screenWidth = metric.widthPixels;
    screenHeight = metric.heightPixels;
    screenDpi = metric.densityDpi;
    density = metric.density;
    scaledDensity = metric.scaledDensity;
}
 
Example 7
Source File: Utils.java    From imsdk-android with MIT License 5 votes vote down vote up
/**
 * 判断手机是否含有虚拟按键  99%
 */
public static boolean hasVirtualNavigationBar(Context context) {
    boolean hasSoftwareKeys = true;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Display d = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        d.getRealMetrics(realDisplayMetrics);

        int realHeight = realDisplayMetrics.heightPixels;
        int realWidth = realDisplayMetrics.widthPixels;

        DisplayMetrics displayMetrics = new DisplayMetrics();
        d.getMetrics(displayMetrics);

        int displayHeight = displayMetrics.heightPixels;
        int displayWidth = displayMetrics.widthPixels;

        hasSoftwareKeys = (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        hasSoftwareKeys = !hasMenuKey && !hasBackKey;
    }

    return hasSoftwareKeys;
}
 
Example 8
Source File: Utils.java    From FilterTabView with Apache License 2.0 5 votes vote down vote up
/**
 * 获取屏幕的真实高度 如果有虚拟按键会算上虚拟按键的高度
 * @param context
 * @return
 */
public static int getScreenHeightWidthBar(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    DisplayMetrics dm = new DisplayMetrics();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        display.getRealMetrics(dm);
    } else {
        display.getMetrics(dm);
    }
    int realHeight = dm.heightPixels;

    return realHeight;
}
 
Example 9
Source File: NavbarUtils.java    From BottomBar with Apache License 2.0 5 votes vote down vote up
/**
 * http://stackoverflow.com/a/14871974
 */
private static boolean hasSoftKeys(@NonNull Context context) {
    boolean hasSoftwareKeys = true;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Display d = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        d.getRealMetrics(realDisplayMetrics);

        int realHeight = realDisplayMetrics.heightPixels;
        int realWidth = realDisplayMetrics.widthPixels;

        DisplayMetrics displayMetrics = new DisplayMetrics();
        d.getMetrics(displayMetrics);

        int displayHeight = displayMetrics.heightPixels;
        int displayWidth = displayMetrics.widthPixels;

        hasSoftwareKeys = (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        hasSoftwareKeys = !hasMenuKey && !hasBackKey;
    }

    return hasSoftwareKeys;
}
 
Example 10
Source File: NavigationUtil.java    From UIWidget with Apache License 2.0 5 votes vote down vote up
/**
 * 获取屏幕宽度,不包括右侧导航栏
 */
public static int getRealScreenWidth(Activity activity) {
    WindowManager windowManager = activity.getWindowManager();
    Display display = windowManager.getDefaultDisplay();
    DisplayMetrics displayMetrics = new DisplayMetrics();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        display.getRealMetrics(displayMetrics);
    } else {
        display.getMetrics(displayMetrics);
    }
    return displayMetrics.widthPixels;
}
 
Example 11
Source File: DisplayUtil.java    From FriendBook with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 判断手机是否含有虚拟按键  99%
 *
 * @return
 */
public static boolean hasVirtualNavigationBar(Context context) {
    boolean hasSoftwareKeys = true;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Display d = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        d.getRealMetrics(realDisplayMetrics);

        int realHeight = realDisplayMetrics.heightPixels;
        int realWidth = realDisplayMetrics.widthPixels;

        DisplayMetrics displayMetrics = new DisplayMetrics();
        d.getMetrics(displayMetrics);

        int displayHeight = displayMetrics.heightPixels;
        int displayWidth = displayMetrics.widthPixels;

        hasSoftwareKeys = (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        hasSoftwareKeys = !hasMenuKey && !hasBackKey;
    }

    return hasSoftwareKeys;
}
 
Example 12
Source File: ScreenResolution.java    From NetEasyNews with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets real resolution via the new getRealMetrics API.
 * */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static Pair<Integer,Integer> getRealResolution(Context ctx) {
  WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
  Display display = wm.getDefaultDisplay();
  DisplayMetrics metrics = new DisplayMetrics();
  display.getRealMetrics(metrics);
  return new Pair<Integer, Integer>(metrics.widthPixels, metrics.heightPixels);
}
 
Example 13
Source File: U.java    From SecondScreen with Apache License 2.0 5 votes vote down vote up
public static boolean runSizeCommand(Context context, String requestedRes) {
    if(isDesktopModeActive(context))
        return true;

    DisplayMetrics metrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display disp = wm.getDefaultDisplay();
    disp.getRealMetrics(metrics);

    SharedPreferences prefMain = getPrefMain(context);
    String currentRes = " ";
    String nativeRes = Integer.toString(prefMain.getInt("width", 0))
            + "x"
            + Integer.toString(prefMain.getInt("height", 0));

    if(prefMain.getBoolean("debug_mode", false)) {
        SharedPreferences prefCurrent = getPrefCurrent(context);
        currentRes = prefCurrent.getString("size", "reset");

        if("reset".equals(currentRes))
            currentRes = nativeRes;
    } else {
        if((context.getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT && !prefMain.getBoolean("landscape", false))
                || (context.getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE && prefMain.getBoolean("landscape", false))) {
            currentRes = Integer.toString(metrics.widthPixels)
                    + "x"
                    + Integer.toString(metrics.heightPixels);
        } else if((context.getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE && !prefMain.getBoolean("landscape", false))
                || (context.getApplicationContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT && prefMain.getBoolean("landscape", false))) {
            currentRes = Integer.toString(metrics.heightPixels)
                    + "x"
                    + Integer.toString(metrics.widthPixels);
        }
    }

    if(requestedRes.equals("reset"))
        requestedRes = nativeRes;

    return !requestedRes.equals(currentRes);
}
 
Example 14
Source File: DeviceUtils.java    From JumpGo with Mozilla Public License 2.0 5 votes vote down vote up
public static int getAvailableScreenWidth(@NonNull Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getRealMetrics(metrics);
        return metrics.widthPixels;
    } else {
        return getScreenWidth(context);
    }
}
 
Example 15
Source File: ScreenResolution.java    From video-player with MIT License 5 votes vote down vote up
/**
 * Gets real resolution via the new getRealMetrics API.
 * */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static Pair<Integer,Integer> getRealResolution(Context ctx) {
  WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
  Display display = wm.getDefaultDisplay();
  DisplayMetrics metrics = new DisplayMetrics();
  display.getRealMetrics(metrics);
  return new Pair<Integer, Integer>(metrics.widthPixels, metrics.heightPixels);
}
 
Example 16
Source File: BarConfig.java    From ImmersionBar with Apache License 2.0 5 votes vote down vote up
@TargetApi(14)
private boolean hasNavBar(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        //判断小米手机是否开启了全面屏,开启了,直接返回false
        if (Settings.Global.getInt(activity.getContentResolver(), IMMERSION_MIUI_NAVIGATION_BAR_HIDE_SHOW, 0) != 0) {
            return false;
        }
        //判断华为手机是否隐藏了导航栏,隐藏了,直接返回false
        if (OSUtils.isEMUI()) {
            if (OSUtils.isEMUI3_x() || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                if (Settings.System.getInt(activity.getContentResolver(), IMMERSION_EMUI_NAVIGATION_BAR_HIDE_SHOW, 0) != 0) {
                    return false;
                }
            } else {
                if (Settings.Global.getInt(activity.getContentResolver(), IMMERSION_EMUI_NAVIGATION_BAR_HIDE_SHOW, 0) != 0) {
                    return false;
                }
            }
        }
    }
    //其他手机根据屏幕真实高度与显示高度是否相同来判断
    WindowManager windowManager = activity.getWindowManager();
    Display d = windowManager.getDefaultDisplay();

    DisplayMetrics realDisplayMetrics = new DisplayMetrics();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        d.getRealMetrics(realDisplayMetrics);
    }

    int realHeight = realDisplayMetrics.heightPixels;
    int realWidth = realDisplayMetrics.widthPixels;

    DisplayMetrics displayMetrics = new DisplayMetrics();
    d.getMetrics(displayMetrics);

    int displayHeight = displayMetrics.heightPixels;
    int displayWidth = displayMetrics.widthPixels;

    return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
}
 
Example 17
Source File: RNDetectNavbarAndroidModule.java    From react-native-detect-navbar-android with MIT License 5 votes vote down vote up
@SuppressLint ("NewApi")
private boolean hasImmersive() {

    if (!cached) {
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            hasImmersive = false;
            cached = true;
            return false;
        }
        Display d = ((WindowManager) reactContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        d.getRealMetrics(realDisplayMetrics);

        int realHeight = realDisplayMetrics.heightPixels;
        int realWidth = realDisplayMetrics.widthPixels;

        DisplayMetrics displayMetrics = new DisplayMetrics();
        d.getMetrics(displayMetrics);

        int displayHeight = displayMetrics.heightPixels;
        int displayWidth = displayMetrics.widthPixels;

        hasImmersive = (realWidth > displayWidth) || (realHeight > displayHeight);
        cached = true;
    }

    return hasImmersive;
}
 
Example 18
Source File: DisplayInfoAndroid.java    From Accessibility-Test-Framework-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Derives an instance from a {@link Display}
 *
 * @param display The {@link Display} instance from which to construct
 */
public DisplayInfoAndroid(Display display) {
  super();
  DisplayMetrics tempMetrics = new DisplayMetrics();
  display.getMetrics(tempMetrics);
  this.metricsWithoutDecoration = new MetricsAndroid(tempMetrics);
  tempMetrics.setToDefaults();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    display.getRealMetrics(tempMetrics);
    this.realMetrics = new MetricsAndroid(tempMetrics);
  } else {
    this.realMetrics = null;
  }
}
 
Example 19
Source File: DeviceDimensionsManager.java    From fullscreen-video-view with Apache License 2.0 5 votes vote down vote up
private DisplayMetrics getRealDisplayMetrics(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    if (windowManager == null) {
        return null;
    }

    Display display = windowManager.getDefaultDisplay();
    DisplayMetrics realMetrics = new DisplayMetrics();
    display.getRealMetrics(realMetrics);

    return realMetrics;
}
 
Example 20
Source File: ScreenResolution.java    From MyHearts with Apache License 2.0 5 votes vote down vote up
/**
 * Gets real resolution via the new getRealMetrics API.
 * */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static Pair<Integer,Integer> getRealResolution(Context ctx) {
  WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
  Display display = wm.getDefaultDisplay();
  DisplayMetrics metrics = new DisplayMetrics();
  display.getRealMetrics(metrics);
  return new Pair<Integer, Integer>(metrics.widthPixels, metrics.heightPixels);
}