【Android】【基础】获取屏幕宽高

代码很简单,就不多说了 ,直接上源代码


public static Point getRealScreenSize() {
    if (screenSize != null) {
        return screenSize;
    }
    Point size = new Point();
    try {
        WindowManager windowManager = (WindowManager) ApplicationLoader.context.getSystemService(Context.WINDOW_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            windowManager.getDefaultDisplay().getRealSize(size);
        } else {
            try {
                Method mGetRawW = Display.class.getMethod("getRawWidth");
                Method mGetRawH = Display.class.getMethod("getRawHeight");
                size.set((Integer) mGetRawW.invoke(windowManager.getDefaultDisplay()), (Integer) mGetRawH.invoke(windowManager.getDefaultDisplay()));
            } catch (Exception e) {
                size.set(windowManager.getDefaultDisplay().getWidth(), windowManager.getDefaultDisplay().getHeight());
                Logger.e(TAG, e);
            }
        }
    } catch (Exception e) {
        Logger.e(TAG, e);
    }
    screenSize = size;
    return screenSize;
}

你可能感兴趣的:(Android)