我曾尝试不同的方法来获取设备的屏幕尺寸,但它总是返回我的错误尺寸( 791x480
,而不是854x480
)。 这可能是由于导航栏。 我的设备正在运行杰利贝恩4.1.1。
我试过了:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
OpenGLRenderer.widthScreen = size.x;
OpenGLRenderer.heightScreen = size.y;
和:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
String resolution = dm.widthPixels + "x" + dm.heightPixels;
我使用下面的代码,以使应用程序全屏:
setContentView(mGLSurfaceView);
mGLSurfaceView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
我注意到,在logcat中,将返回正确的大小:
01-01 03:05:06.967: I/InputReader(1961):
Device reconfigured: id=8, name='cyttsp-spi',
surface size is now 480x854, mode is 1
在我AndroidManifest
:
uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"
我用了:
- 得到Ice Cream Sandwich的实际屏幕分辨率
- HTC感觉真实分辨率
- 获取像素的屏幕尺寸
为什么我不能够得到真正的大小( 480x854
)? 或者我如何可以使用由“重新配置设备”返回的面?
尝试调用
display.getRealSize();
免责声明:我没有意识到这一点,但这只适用于API 17(安卓4.2 /豆形软糖)和后续工作。
试试这个
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{
// Below Jellybeans you can use reflection method
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();
}
}
我正面临着在Android 3.2平板电脑同样的问题。 我测试了getRealSize和getDisplayMetrics。 他们预计将崩溃,因为他们的意思是可以用API 17.但奇怪的是getDisplayMetrics我3.2不会崩溃并返回正确的尺寸。 getRealSize崩溃。
如果你想获得活动中的大小,可以通过下面的代码做到这一点。
findViewById(android.R.id.content).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View view = findViewById(android.R.id.content);
Log.e("test", "root view height is " + view.getHeight());
}
});