欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android获取屏幕宽高与获取控件宽高(三种方法)

程序员文章站 2023-12-14 21:22:46
1.获取屏幕宽高 方法1: int screenwidth = getwindowmanager().getdefaultdisplay().getwidth(...

1.获取屏幕宽高

方法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); 

2.获取控件的宽高,一般来说,我们在oncreate里面得到的控件的宽高全是0.采用下面的方法,可以得到真实的宽高

方法一 :

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三次

方法二 :

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二次,但是回调函数会回调很多次

方法三:

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二次,但是回调函数只回调一次  

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!

上一篇:

下一篇: