package com.yongdaimi.android.androidapitest;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testCode();
} @Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Log.i("xp.chen", "getContentViewHeight(): "+getContentViewHeight(MainActivity.this));
Log.i("xp.chen", "getStatusBarHeight2(): "+getStatusBarHeight2(this));
} private void testCode() {
Log.i("xp.chen", "getResources().getDisplayMetrics():: widthPixels: "+getScreenWidth(this));
Log.i("xp.chen", "getResources().getDisplayMetrics():: heightPixels: "+getScreenHeight(this));
Log.i("xp.chen", "WindowManager:: widthPixels: "+getScreenWidth2(this));
Log.i("xp.chen", "WindowManager:: heightPixels: "+getScreenHeight2(this));
Log.i("xp.chen", "WindowManager getRealSize() :: widthPixels: "+getScreenWidth3(this));
Log.i("xp.chen", "WindowManager getRealSize() :: heightPixels: "+getScreenHeight3(this));
Log.i("xp.chen", "getResources() navigation_bar_height: "+getNavigationBarHeight(this));
Log.i("xp.chen", "getResources() getStatusBarHeight: "+getStatusBarHeight(this));
} public static int getScreenWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
} /**
* 不包含虚拟导航栏高度
* @param context
* @return
*/
public static int getScreenHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
} public static int getScreenWidth2(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
return dm.widthPixels;
} public static int getScreenHeight2(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
return dm.heightPixels;
} /**
* 包含虚拟导航栏高度
* @param context
* @return
*/
public static int getScreenWidth3(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display defaultDisplay = windowManager.getDefaultDisplay();
Point outPoint = new Point();
defaultDisplay.getRealSize(outPoint);
return outPoint.x;
} public static int getScreenHeight3(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display defaultDisplay = windowManager.getDefaultDisplay();
Point outPoint = new Point();
defaultDisplay.getRealSize(outPoint);
return outPoint.y;
} public static int getNavigationBarHeight(Context context) {
int navigationBarHeight = -1;
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android");
if (resourceId > 0) {
navigationBarHeight = resources.getDimensionPixelSize(resourceId);
}
return navigationBarHeight;
} public int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
} /**
* 注意:这个方法的值必须等到页面创建完成后才能获取到。推荐写在onWindowFocusChanged()方法中
* @param activity
* @return
*/
public int getStatusBarHeight2(Activity activity) {
Rect rectangle = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
return rectangle.top;
} /**
* 注意:这个方法的值必须等到页面创建完成后才能获取到。推荐写在onWindowFocusChanged()方法中
* @param activity
* @return
*/
public static int getContentViewHeight(Activity activity) {
Rect rectangle= new Rect();
activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rectangle);
return rectangle.height();
} /**
* 可以利用getScreenHeight() 与 getScreenHeight3() 返回值的差异来判断是否存在虚拟导航栏
* @param context
* @return
*/
public static boolean hasNavigationBar(Context context) {
return getScreenHeight(context) != getScreenHeight3(context);
} }

测试结果:

【三星S6 无虚拟导航栏】

2019-05-07 15:58:08.157 11483-11483/com.yongdaimi.android.androidapitest I/xp.chen: getResources().getDisplayMetrics():: widthPixels: 1440
2019-05-07 15:58:08.157 11483-11483/com.yongdaimi.android.androidapitest I/xp.chen: getResources().getDisplayMetrics():: heightPixels: 2560
2019-05-07 15:58:08.157 11483-11483/com.yongdaimi.android.androidapitest I/xp.chen: WindowManager:: widthPixels: 1440
2019-05-07 15:58:08.158 11483-11483/com.yongdaimi.android.androidapitest I/xp.chen: WindowManager:: heightPixels: 2560
2019-05-07 15:58:08.158 11483-11483/com.yongdaimi.android.androidapitest I/xp.chen: WindowManager getRealSize() :: widthPixels: 1440
2019-05-07 15:58:08.158 11483-11483/com.yongdaimi.android.androidapitest I/xp.chen: WindowManager getRealSize() :: heightPixels: 2560
2019-05-07 15:58:08.159 11483-11483/com.yongdaimi.android.androidapitest I/xp.chen: getResources() navigation_bar_height: 192
2019-05-07 15:58:08.159 11483-11483/com.yongdaimi.android.androidapitest I/xp.chen: getResources() getStatusBarHeight: 96
2019-05-07 15:58:08.224 11483-11483/com.yongdaimi.android.androidapitest I/xp.chen: getContentViewHeight(): 2240
2019-05-07 15:58:08.225 11483-11483/com.yongdaimi.android.androidapitest I/xp.chen: getStatusBarHeight2(): 96

【Google Nexus6 带虚拟导航栏】

2019-05-07 15:59:02.894 30818-30818/com.yongdaimi.android.androidapitest I/xp.chen: getResources().getDisplayMetrics():: widthPixels: 1440
2019-05-07 15:59:02.894 30818-30818/com.yongdaimi.android.androidapitest I/xp.chen: getResources().getDisplayMetrics():: heightPixels: 2392
2019-05-07 15:59:02.895 30818-30818/com.yongdaimi.android.androidapitest I/xp.chen: WindowManager:: widthPixels: 1440
2019-05-07 15:59:02.895 30818-30818/com.yongdaimi.android.androidapitest I/xp.chen: WindowManager:: heightPixels: 2392
2019-05-07 15:59:02.895 30818-30818/com.yongdaimi.android.androidapitest I/xp.chen: WindowManager getRealSize() :: widthPixels: 1440
2019-05-07 15:59:02.896 30818-30818/com.yongdaimi.android.androidapitest I/xp.chen: WindowManager getRealSize() :: heightPixels: 2560
2019-05-07 15:59:02.896 30818-30818/com.yongdaimi.android.androidapitest I/xp.chen: getResources() navigation_bar_height: 168
2019-05-07 15:59:02.896 30818-30818/com.yongdaimi.android.androidapitest I/xp.chen: getResources() getStatusBarHeight: 84
2019-05-07 15:59:02.967 30818-30818/com.yongdaimi.android.androidapitest I/xp.chen: getContentViewHeight(): 2112
2019-05-07 15:59:02.968 30818-30818/com.yongdaimi.android.androidapitest I/xp.chen: getStatusBarHeight2(): 84

参考链接:

1.Android屏幕各尺寸获取

04-08 13:29