方法一:
display defaultdisplay =
getwindowmanager()
.getdefaultdisplay()
; point point = new point()
; defaultdisplay.
getsize
(point)
;int x = point.x;
int y = point.y;
log.
i(tag,
"x = "
+ x +
",y = "
+ y)
;//x = 1440,y = 2768
方法二:
rect outsize = new rect()
;getwindowmanager()
.getdefaultdisplay()
.getrectsize
(outsize)
;int left = outsize.left;
int top = outsize.top;
int right = outsize.right;
int bottom = outsize.bottom;
log.
d(tag,
"left = "
+ left +
",top = "
+ top +
",right = "
+ right +
",bottom = "
+ bottom)
;//left = 0,top = 0,right = 1440,bottom = 2768
方法三:
displaymetrics outmetrics = new displaymetrics()
;getwindowmanager()
.getdefaultdisplay()
.getmetrics
(outmetrics)
;int widthpixels = outmetrics.widthpixels;
int heightpixels = outmetrics.heightpixels;
log.
i(tag,
"widthpixels = "
+ widthpixels +
",heightpixels = "
+ heightpixels)
;//widthpixels = 1440, heightpixels = 2768
方法2和方法3檢視原始碼可知其實是一樣的邏輯。
public void
getsize
(point outsize)
} public void
getmetrics
(displaymetrics outmetrics)
}
方法四:
point outsize = new point()
;getwindowmanager()
.getdefaultdisplay()
.getrealsize
(outsize)
;int x = outsize.x;
int y = outsize.y;
log.
w(tag,
"x = "
+ x +
",y = "
+ y)
;//x = 1440,y = 2960
方法五:
displaymetrics outmetrics = new displaymetrics()
;getwindowmanager()
.getdefaultdisplay()
.getrealmetrics
(outmetrics)
;int widthpixel = outmetrics.widthpixels;
int heightpixel = outmetrics.heightpixels;
log.
w(tag,
"widthpixel = "
+ widthpixel +
",heightpixel = "
+ heightpixel)
;//widthpixel = 1440,heightpixel = 2960
我們注意到方法1,2,3顯示螢幕的解析度是 1440x2768,而方法4,5顯示的螢幕的解析度是1440x2960。為什麼是這樣了?
android.view
windowmanager
應用程式用於與視窗管理器通訊的介面。
display
提供有關邏輯顯示的大小和密度的資訊。
android.util
displaymetrics
描述有關顯示的一般資訊的結構,例如其大小,密度和字型縮放。
要訪問displaymetrics成員,請初始化如下物件:
displaymetrics outmetrics = new displaymetrics()
;getwindowmanager()
.getdefaultdisplay()
.getrealmetrics
(outmetrics)
; displaymetrics displaymetrics =
getresources()
.getdisplaymetrics()
;
工具類:
public class displayutil
}public static
void
getrealscreenrelatedinformation
(context context)
}}
Android獲取螢幕寬度高度
方法一 windowmanager wm windowmanager this getsystemservice context.window service int width wm.getdefaultdisplay getwidth int height wm.getdefaultdispla...
Android開發 獲取螢幕高度和寬度
在開發的某些時候,需要獲取螢幕的高度和寬度,下面有兩種方法,大家可以試一下。方法一 windowmanager wm windowmanager this.getsystemservice context.window service int width wm.getdefaultdisplay g...
android 獲取螢幕寬高
發現專案中有兩種獲取螢幕寬高的方法 一 通過windowmanager獲取display,通過display獲取metrics,通過metrics獲取寬高。最後的單位是px。public static final int getscreenwidth 二 通過windowmanager獲取displ...