android获取其他屏幕方向的屏幕大小
自从安卓3.0以来,屏幕上有一个带有返回和主页按钮的导航栏。现在如果我得到这样的屏幕大小android获取其他屏幕方向的屏幕大小,android,screen-orientation,screen-size,Android,Screen Orientation,Screen Size,自从安卓3.0以来,屏幕上有一个带有返回和主页按钮的导航栏。现在如果我得到这样的屏幕大小 ClockLwp.ScreenHeight = getWindowManager().getDefaultDisplay().getHeight(); ClockLwp.ScreenWidth = getWindowManager().getDefaultDisplay().getWidth(); 这个 getResources().getDisplayMetrics().widthPixels getR
ClockLwp.ScreenHeight = getWindowManager().getDefaultDisplay().getHeight();
ClockLwp.ScreenWidth = getWindowManager().getDefaultDisplay().getWidth();
这个
getResources().getDisplayMetrics().widthPixels
getResources().getDisplayMetrics().heightPixels
我得到了不同的风景和肖像价值。下面是一个例子,我的意思是:
Acer Iconia A500肖像模式:800 x 1232
Acer Iconia A500横向模式:1280 x 752
AFAIK这适用于所有带有导航栏的设备。(如三星Galaxy Nexus、GalaxyTab 10.1等)
现在我的问题是,如果我处于纵向模式,我如何获得横向模式的值,反之亦然?由于android 3,公共api无法检索原始高度和宽度。你可以通过反思。查找getRawHeigth/Width这里有一个适合我的解决方案。我得到了真实的屏幕大小,然后减去导航栏的高度
// get navigation bar height for landscape and portrait modes
Resources resources = activity.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
int resourceIdLandscape = resources.getIdentifier("navigation_bar_height_landscape", "dimen", "android");
int portraitNavBarHeight = 0;
int landscapeNavBarHeight = 0;
boolean hasPermanentMenuKey = ViewConfiguration.get(activity).hasPermanentMenuKey();
if (resourceId > 0 && !hasPermanentMenuKey) {
portraitNavBarHeight = resources.getDimensionPixelSize(resourceId);
}
if (resourceIdLandscape > 0 && !hasPermanentMenuKey) {
landscapeNavBarHeight = resources.getDimensionPixelSize(resourceIdLandscape);
}
// get real screen size
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
Display disp = wm.getDefaultDisplay();
int API_LEVEL = android.os.Build.VERSION.SDK_INT;
if (API_LEVEL >= 17) {
disp.getRealMetrics(displayMetrics);
} else {
disp.getMetrics(displayMetrics);
}
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
// size for orientation
int portraitHeight;
int portraitWidth;
int landscapeHeight;
int landscapeWidth;
if(width > height) {
portraitHeight = width - portraitNavBarHeight;
portraitWidth = height;
landscapeHeight = height - landscapeNavBarHeight;
landscapeNavBarHeight = width;
} else {
portraitHeight = height - portraitNavBarHeight;
portraitWidth = width;
landscapeHeight = width - landscapeNavBarHeight;
landscapeNavBarHeight = height;
}
如果您的应用程序具有状态栏
public static int getStatusBarHeight(Context context) {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}
编辑(2014年9月26日):我重新制定了我的文本,以包含完整的答案,而不仅仅是对Dmitri解决方案的评论
下面是我们用来计算旋转前导航栏高度的部分:
DisplayMetrics metrics = new DisplayMetrics();
mFragment.getActivity().getWindowManager().getDefaultDisplay()
.getMetrics(metrics);
/*
* We need to take system resources here to verify navigation
* bar height. Application resources also contains navigation bar
* height but this value is not valid for some devices e.g. Nexus 7 (2012)!
*/
Resources appRes = getResources();
Resources sysRes = Resources.getSystem();
boolean landscapeMode = (metrics.widthPixels > metrics.heightPixels);
/* Check if the device has navigation bar. There is no direct API for
* that so we use some hacking that may not always work,
* (see http://stackoverflow.com/questions/16092431/check-for-navigation-bar)
* but this method should be enough for most devices.
* TODO: Find a more consistent way. For our needs it is currently enough as we
* support limited set of devices.
*/
boolean hasNavigationBar = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
int navBarHeight = 0;
int navBarHeightRotated = 0;
if (hasNavigationBar) {
int barHeightDimId = 0;
int barHeightDimIdRotated = 0;
if (landscapeMode) {
barHeightDimId = sysRes.getIdentifier(
"navigation_bar_height_landscape", "dimen", "android");
barHeightDimIdRotated = sysRes.getIdentifier(
"navigation_bar_height", "dimen", "android");
} else {
barHeightDimIdRotated = sysRes.getIdentifier(
"navigation_bar_height_landscape", "dimen", "android");
barHeightDimId = sysRes.getIdentifier("navigation_bar_height",
"dimen", "android");
}
if (barHeightDimId != 0) {
navBarHeight = appRes.getDimensionPixelSize(barHeightDimId);
}
if (barHeightDimIdRotated != 0) {
navBarHeightRotated = appRes
.getDimensionPixelSize(barHeightDimIdRotated);
}
NCLogs.i("MyApp", String.format("Android navigation bar height: %d (current), %d (after rotation)", navBarHeight, navBarHeightRotated));
}
int realScreenHeight = metrics.heightPixels + navBarHeight;
// We expect to not have decor views near the left or right borders
int realScreenWidth = metrics.widthPixels;
/*
* Height of decor views: system status bar + any application addtional elements +
* + system navigation bar
*/
int decorHeightRotated = realScreenHeight - getHeight() - navBarHeight + navBarHeightRotated;
int viewWidthRotated = realScreenHeight;
int viewHeightRotated = (realScreenWidth - decorHeightRotated);
NCLogs.i("MyApp", String.format("Width after rotation: %d, Height after rotation: %d", viewWidthRotated, viewHeightRotated));
然后我们可以计算旋转后的视图宽度和高度:
DisplayMetrics metrics = new DisplayMetrics();
mFragment.getActivity().getWindowManager().getDefaultDisplay()
.getMetrics(metrics);
/*
* We need to take system resources here to verify navigation
* bar height. Application resources also contains navigation bar
* height but this value is not valid for some devices e.g. Nexus 7 (2012)!
*/
Resources appRes = getResources();
Resources sysRes = Resources.getSystem();
boolean landscapeMode = (metrics.widthPixels > metrics.heightPixels);
/* Check if the device has navigation bar. There is no direct API for
* that so we use some hacking that may not always work,
* (see http://stackoverflow.com/questions/16092431/check-for-navigation-bar)
* but this method should be enough for most devices.
* TODO: Find a more consistent way. For our needs it is currently enough as we
* support limited set of devices.
*/
boolean hasNavigationBar = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
int navBarHeight = 0;
int navBarHeightRotated = 0;
if (hasNavigationBar) {
int barHeightDimId = 0;
int barHeightDimIdRotated = 0;
if (landscapeMode) {
barHeightDimId = sysRes.getIdentifier(
"navigation_bar_height_landscape", "dimen", "android");
barHeightDimIdRotated = sysRes.getIdentifier(
"navigation_bar_height", "dimen", "android");
} else {
barHeightDimIdRotated = sysRes.getIdentifier(
"navigation_bar_height_landscape", "dimen", "android");
barHeightDimId = sysRes.getIdentifier("navigation_bar_height",
"dimen", "android");
}
if (barHeightDimId != 0) {
navBarHeight = appRes.getDimensionPixelSize(barHeightDimId);
}
if (barHeightDimIdRotated != 0) {
navBarHeightRotated = appRes
.getDimensionPixelSize(barHeightDimIdRotated);
}
NCLogs.i("MyApp", String.format("Android navigation bar height: %d (current), %d (after rotation)", navBarHeight, navBarHeightRotated));
}
int realScreenHeight = metrics.heightPixels + navBarHeight;
// We expect to not have decor views near the left or right borders
int realScreenWidth = metrics.widthPixels;
/*
* Height of decor views: system status bar + any application addtional elements +
* + system navigation bar
*/
int decorHeightRotated = realScreenHeight - getHeight() - navBarHeight + navBarHeightRotated;
int viewWidthRotated = realScreenHeight;
int viewHeightRotated = (realScreenWidth - decorHeightRotated);
NCLogs.i("MyApp", String.format("Width after rotation: %d, Height after rotation: %d", viewWidthRotated, viewHeightRotated));
这不是我想要的原始高度和宽度。这是减去导航栏高度后的宽度和高度值,但是其他方向的值。我不知道如何获得导航栏的高度。(包含back和home按钮,不确定如何正确调用此栏。)RawHeigth-Display.getHeight()应该为您提供栏高不起作用。该栏位于屏幕右侧的某些设备上,而不是其底部。