本文实例讲述了android编程获取屏幕宽高与获取控件宽高的方法。分享给大家供大家参考,具体如下:
获取屏幕宽高
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
// 获取屏幕宽高(方法1)
int screenwidth = getwindowmanager().getdefaultdisplay().getwidth(); // 屏幕宽(像素,如:480px)
int screenheight = getwindowmanager().getdefaultdisplay().getheight(); // 屏幕高(像素,如:800p)
log.e(tag + " getdefaultdisplay" , "screenwidth=" + screenwidth + "; screenheight=" + screenheight);
// 获取屏幕密度(方法2)
displaymetrics dm = new displaymetrics();
dm = getresources().getdisplaymetrics();
float density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
int densitydpi = dm.densitydpi; // 屏幕密度(每寸像素:120/160/240/320)
float xdpi = dm.xdpi;
float ydpi = dm.ydpi;
log.e(tag + " displaymetrics" , "xdpi=" + xdpi + "; ydpi=" + ydpi);
log.e(tag + " displaymetrics" , "density=" + density + "; densitydpi=" + densitydpi);
screenwidth = dm.widthpixels; // 屏幕宽(像素,如:480px)
screenheight = dm.heightpixels; // 屏幕高(像素,如:800px)
log.e(tag + " displaymetrics(111)" , "screenwidth=" + screenwidth + "; screenheight=" + screenheight);
// 获取屏幕密度(方法3)
dm = new displaymetrics();
getwindowmanager().getdefaultdisplay().getmetrics(dm);
density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
densitydpi = dm.densitydpi; // 屏幕密度(每寸像素:120/160/240/320)
xdpi = dm.xdpi;
ydpi = dm.ydpi;
log.e(tag + " displaymetrics" , "xdpi=" + xdpi + "; ydpi=" + ydpi);
log.e(tag + " displaymetrics" , "density=" + density + "; densitydpi=" + densitydpi);
int screenwidthdip = dm.widthpixels; // 屏幕宽(dip,如:320dip)
int screenheightdip = dm.heightpixels; // 屏幕宽(dip,如:533dip)
log.e(tag + " displaymetrics(222)" , "screenwidthdip=" + screenwidthdip + "; screenheightdip=" + screenheightdip);
screenwidth = ( int )(dm.widthpixels * density + 0 .5f); // 屏幕宽(px,如:480px)
screenheight = ( int )(dm.heightpixels * density + 0 .5f); // 屏幕高(px,如:800px)
log.e(tag + " displaymetrics(222)" , "screenwidth=" + screenwidth + "; screenheight=" + screenheight);
|
获取控件的宽高
一般来说,我们在oncreate里面得到的控件的宽高全是0.采用下面的方法,可以得到真实的宽高
方法一:
1
2
3
4
5
6
|
int w = view.measurespec.makemeasurespec( 0 ,view.measurespec.unspecified);
int h = view.measurespec.makemeasurespec( 0 ,view.measurespec.unspecified);
imageview.measure(w, h);
int height =imageview.getmeasuredheight();
int width =imageview.getmeasuredwidth();
textview.append( "\n" +height+ "," +width);
|
此方法会加载onmeasure三次
方法二:
1
2
3
4
5
6
7
8
9
|
viewtreeobserver vto = imageview.getviewtreeobserver();
vto.addonpredrawlistener( new viewtreeobserver.onpredrawlistener() {
public boolean onpredraw() {
int height = imageview.getmeasuredheight();
int width = imageview.getmeasuredwidth();
textview.append( "\n" +height+ "," +width);
return true ;
}
});
|
此方法会加载onmeasure二次,但是回调函数会回调很多次
方法三:
1
2
3
4
5
6
7
8
|
viewtreeobserver vto2 = imageview.getviewtreeobserver();
vto2.addongloballayoutlistener( new ongloballayoutlistener() {
@override
public void ongloballayout() {
imageview.getviewtreeobserver().removeglobalonlayoutlistener( this );
textview.append( "\n\n" +imageview.getheight()+ "," +imageview.getwidth());
}
});
|
此方法会加载onmeasure二次,但是回调函数只回调一次
希望本文所述对大家android程序设计有所帮助。