Android 获取屏幕宽高的正确姿势

前言

在开发时,我们经常需要根据屏幕的宽高来进行对view的适配,无论是自定义view还是andorid自带的一些控件,比如说需要占当前屏幕高度的30%,就需要获取到屏幕的宽高,但在获取宽高时我遇到了一些坑,总结如下

获取高度

下面两种方法都是安卓自带方法可以获取到屏幕宽高的

但是!

这两种方法获取的高度都是省略了手机最上方系统状态栏的高度

系统方法

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

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;
}

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

这种方法可以获取到完整的屏幕高度

/***
 * 获取屏幕的高度,全面屏和非全面屏
 * @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
            : getApplicationContext().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) getApplicationContext().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;
}

参考博客:Android怎样正确的获取屏幕的高度 - 简书

你可能感兴趣的:(Android,工具类,android,android,studio,ide)