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怎样正确的获取屏幕的高度 - 简书

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TDSSS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值