当前位置: 移动技术网 > 移动技术>移动开发>Android > Android获取手机屏幕宽高、状态栏高度以及字符串宽高信息的方法

Android获取手机屏幕宽高、状态栏高度以及字符串宽高信息的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了android获取手机屏幕宽高、状态栏高度以及字符串宽高信息的方法。分享给大家供大家参考。具体如下:

首先定义textview对象commenttext

获取文字的宽高:

textpaint textpaint = new textpaint(paint.anti_alias_flag);
textpaint.settextsize(commenttext.gettextsize());
textpaint.setcolor(color.white);
fontmetrics fontmetrics = textpaint.getfontmetrics();
float ftop = fontmetrics.top;
float fbottom = fontmetrics.bottom;
float textheight = (int)(fbottom - ftop);
float textwidth = (int)textpaint.measuretext(commenttext.gettext());

获取手机屏幕上方状态栏高度:

复制代码 代码如下:
displaymetrics dm = new displaymetrics(); 
getwindowmanager().getdefaultdisplay().getmetrics(dm); 
int width = dm.widthpixels;  //屏幕宽
int height = dm.heightpixels;  //屏幕高
rect frame = new rect();   
getwindow().getdecorview().getwindowvisibledisplayframe(frame);   
int statusbarheight = frame.top;  //状态栏高
int contenttop = getwindow().findviewbyid(window.id_android_content).gettop();
int titlebarheight = contenttop - statusbarheight; //标题栏高

获取手机屏幕宽高:

复制代码 代码如下:
windowmanager wm = (windowmanager) this.getsystemservice(context.window_service);
int width = wm.getdefaultdisplay().getwidth();//屏幕宽度
int height = wm.getdefaultdisplay().getheight();//屏幕高度

获取textview宽度

textpaint paint = textview.getpaint();
float len = paint.measuretext(string);

获取屏幕尺寸:

displaymetrics dm = new displaymetrics(); 
getwindowmanager().getdefaultdisplay().getmetrics(dm); 
double x = math.pow(dm.widthpixels/dm.xdpi,2); 
double y = math.pow(dm.heightpixels/dm.ydpi,2); 
double screeninches = math.sqrt(x+y); //屏幕尺寸(英寸)

希望本文所述对大家的android程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网