我想拿到手機的屏幕尺寸,但它一直給我錯誤的價值觀,我已經使用獲取屏幕的大小不使用getMetrics
WindowManager windowmanager = (WindowManager)
getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
Display display = windowmanager.getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);
float deviceWidth = displayMetrics.widthPixels;
float deviceHeight = displayMetrics.ydpi;
我想這太代碼:
Resources resources = getResources();
Configuration config = resources.getConfiguration();
DisplayMetrics dm = resources.getDisplayMetrics();
// Note, screenHeightDp isn't reliable
// (it seems to be too small by the height of the status bar),
// but we assume screenWidthDp is reliable.
// Note also, dm.widthPixels,dm.heightPixels aren't reliably pixels
// (they get confused when in screen compatibility mode, it seems),
// but we assume their ratio is correct.
double screenWidthInPixels = (double)config.screenWidthDp *dm.density;
double screenHeightInPixels = screenWidthInPixels * dm.heightPixels /dm.widthPixels;
deviceWidth = (int)(screenWidthInPixels + .5);
deviceHeight = (int)(screenHeightInPixels + .5);
而且還認爲:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Point realSize = new Point();
display.getRealSize(realSize);
deviceWidth= realSize.x;
deviceHeight = realSize.y;
}
但在我的三星S7在SDK 7.0我得到了1920×1080這是錯誤的,因爲在S7在sdk 6.0.1我得到了2560x1440這是真正的價值。 我嘗試了很多解決方案,但沒有發現任何好的東西。
感謝
謝謝,沒有看到,我改變了設置,並得到了正確的結果,但我仍然有一個模擬的Nexus 5更新到7.1.1的問題,我得到1794x1080而不是1920x1080,你有什麼想法爲什麼? –
找到我的答案,問題是按鈕欄不被視爲屏幕的一部分。所以如果你用pysical按鈕創建一個虛擬設備,你不會再有這個錯誤了。與使用按鈕欄的真實設備相同,如華爲P9。 –