del*_*116 21 android android-layout android-screen android-resolution
我试过这个......
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int fullscreenheight = metrics.heightPixels;
int fullscreenwidth = metrics.widthPixels;
Run Code Online (Sandbox Code Playgroud)
和....
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Run Code Online (Sandbox Code Playgroud)
Gnex的显示屏为720×1280.宽度/高度的返回结果(取决于当然的方向)永远不会是1280.我认为这可能与冰淇淋三明治中的屏幕导航栏有关,所以我隐藏了:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
Run Code Online (Sandbox Code Playgroud)
...然后启动一个连续记录屏幕尺寸高度/宽度的线程.即使在ICS导航栏完全消失后......屏幕大小也永远不会报告1280值.我会得到这样的数字:
width = 720
height = 1184
Run Code Online (Sandbox Code Playgroud)
如何在ICS中获得真正的设备屏幕分辨率?
我需要这个值的原因是因为我的应用程序播放视频,我希望视频在设备处于横向时占据整个屏幕.现在我知道你在想什么,为什么不给视频视图增加高度/宽度的"match_parent"值?原因是因为我的视频有多个宽高比,我希望能够根据屏幕的整个宽度计算正确的视频大小.
del*_*116 19
我只为ICS找到了这个隐藏的宝藏......如果你的目标API高于ICS,请参阅Chris Schmich的评论.
如果链接死了....这里是如何获得实际的设备屏幕大小.....
Display display = getWindowManager().getDefaultDisplay();
Method mGetRawH = Display.class.getMethod("getRawHeight");
Method mGetRawW = Display.class.getMethod("getRawWidth");
int rawWidth = (Integer) mGetRawW.invoke(display);
int rawHeight = (Integer) mGetRawH.invoke(display);
Run Code Online (Sandbox Code Playgroud)
编辑(11/19/12)
以上代码将无法在Android 4.2上运行,并且会抛出异常....我还没有找到一种方法来获取Android 4.2中设备的全屏大小.当我这样做时,我会编辑这个答案,但是现在,你应该为android 4.2进行应急编码!
Fra*_*yen 19
从Ahmed的答案来看,这是完整的代码,没有错误:
int width = 0, height = 0;
final DisplayMetrics metrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
Method mGetRawH = null, mGetRawW = null;
try {
// For JellyBean 4.2 (API 17) and onward
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
display.getRealMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
} else {
mGetRawH = Display.class.getMethod("getRawHeight");
mGetRawW = Display.class.getMethod("getRawWidth");
try {
width = (Integer) mGetRawW.invoke(display);
height = (Integer) mGetRawH.invoke(display);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (NoSuchMethodException e3) {
e3.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
您可以使用它getRealSize来获取屏幕分辨率.它在API 17及更高版本中得到官方支持,但它实际上存在于自API 14开始的SDK中.如果您将SDK平台用于API 17或更高版本,则可以正常访问该方法,并绕过仅在API中可用的警告17.
/**
* Gets the device's native screen resolution rotated
* based on the device's current screen orientation.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) //To remove warning about using
//getRealSize prior to API 17
private Point screenResolution() {
WindowManager windowManager =
(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point screenResolution = new Point();
if (Build.VERSION.SDK_INT < 14)
throw new RuntimeException("Unsupported Android version.");
display.getRealSize(screenResolution);
return screenResolution;
}
Run Code Online (Sandbox Code Playgroud)
我已经在真正的Android 5.0.2设备和运行4.1.1(可能还有其他设备)的模拟器中测试了这个.
由于BENC为在评论中指出了这一点.
试试这个
final DisplayMetrics metrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
Method mGetRawH = null,mGetRawW = null;
try {
// For JellyBeans and onward
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN){
display.getRealMetrics(metrics);
realWidth = metrics.widthPixels;
realHeight = metrics.heightPixels;
}else{
mGetRawH = Display.class.getMethod("getRawHeight");
mGetRawW = Display.class.getMethod("getRawWidth");
try {
realWidth = (Integer) mGetRawW.invoke(display);
realHeight = (Integer) mGetRawH.invoke(display);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)