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

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

首先定义TextView对象commentText

获取文字的宽高:

?
1
2
3
4
5
6
7
8
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宽度

?
1
2
TextPaint paint = textView.getPaint();
float len = paint.measureText(string);

获取屏幕尺寸:

?
1
2
3
4
5
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程序设计有所帮助。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值