Android怎样正确的获取屏幕的高度

今天项目里遇到一个奇怪的问题,获取到的屏幕高度和实际的不一样,就觉得奇怪,然后仔细的看了下,因为我的测试机是小米10,全面屏,所以获取的高度有误差。

获取屏幕高度的几种方法

1、系统方法


int screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;//屏幕高度
int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;//屏幕宽度

2、通过WINDOW_SERVICE 获取WindowManager窗口管理类

/**
 * 屏幕高度
 * @return the height of screen, in pixel
 */
public static int getScreenHeight(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Point point = new Point();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        //noinspection ConstantConditions
        wm.getDefaultDisplay().getRealSize(point);
    } else {
        //noinspection ConstantConditions
        wm.getDefaultDisplay().getSize(point);
    }
    return point.y;
}

/**
 * 屏幕宽度
 * @return the width of screen, in pixel
 */
public static int getScreenWidth(Context context) {
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Point point = new Point();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        //noinspection ConstantConditions
        wm.getDefaultDisplay().getRealSize(point);
    } else {
        //noinspection ConstantConditions
        wm.getDefaultDisplay().getSize(point);
    }
    return point.x;
}

以上2种方式获取到的屏幕高度,都是缺省的,缺省的部分就是没包含系统状态栏

3、根据屏幕判断,获取完整的屏幕宽高

/***
 * 获取屏幕的高度,全面屏和非全面屏
 * @param context
 * @return
 */
public static int getFullActivityHeight(@Nullable Context context) {
    if (!isAllScreenDevice()) {
        return getScreenHeight(context);
    }
    return getScreenRealHeight(context);
}

private static final int PORTRAIT = 0;
private static final int LANDSCAPE = 1;
private volatile static boolean mHasCheckAllScreen;
private volatile static boolean mIsAllScreenDevice;

@NonNull
private volatile static Point[] mRealSizes = new Point[2];

public static int getScreenRealHeight(@Nullable Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return getScreenHeight(context);
    }

    int orientation = context != null
            ? context.getResources().getConfiguration().orientation
            : AppManager.getAppManager().getNowContext().getResources().getConfiguration().orientation;
    orientation = orientation == Configuration.ORIENTATION_PORTRAIT ? PORTRAIT : LANDSCAPE;

    if (mRealSizes[orientation] == null) {
        WindowManager windowManager = context != null
                ? (WindowManager) context.getSystemService(Context.WINDOW_SERVICE)
                : (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (windowManager == null) {
            return getScreenHeight(context);
        }
        Display display = windowManager.getDefaultDisplay();
        Point point = new Point();
        display.getRealSize(point);
        mRealSizes[orientation] = point;
    }
    return mRealSizes[orientation].y;
}

public static int getScreenHeight(@Nullable Context context) {
    if (context != null) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }
    return 0;
}

/***
 * 获取当前手机是否是全面屏
 * @return
 */

public static boolean isAllScreenDevice() {
    if (mHasCheckAllScreen) {
        return mIsAllScreenDevice;
    }
    mHasCheckAllScreen = true;
    mIsAllScreenDevice = false;
    // 低于 API 21的,都不会是全面屏。。。
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return false;
    }
    WindowManager windowManager = (WindowManager) AppManager.getAppManager().getNowContext().getSystemService(Context.WINDOW_SERVICE);
    if (windowManager != null) {
        Display display = windowManager.getDefaultDisplay();
        Point point = new Point();
        display.getRealSize(point);
        float width, height;
        if (point.x < point.y) {
            width = point.x;
            height = point.y;
        } else {
            width = point.y;
            height = point.x;
        }
        if (height / width >= 1.97f) {
            mIsAllScreenDevice = true;
        }
    }
    return mIsAllScreenDevice;
}

总结

以上就是关于获取屏幕高度的方法,具体使用根据具体情况,希望能帮到大家

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容