Android获取手机屏幕、状态栏、Activity显示区域的宽高的获取

来源:互联网 发布:app拍照软件排行 编辑:程序博客网 时间:2024/05/16 19:18
1、手机屏幕宽高的获取
1.1DisplayMetrics metric = new DisplayMetrics();  getWindowManager().getDefaultDisplay().getMetrics(metric);  int width = metric.widthPixels;     // 屏幕宽度(像素)  int height = metric.heightPixels;   // 屏幕高度(像素)1.2DisplayMetrics metrics = getResources().getDisplayMetrics();        int width=metrics.widthPixels;        int height=metrics.heightPixels;1.3Display defaultDisplay = getWindowManager().getDefaultDisplay();        defaultDisplay.getWidth();        defaultDisplay.getHeight();

2、状态栏宽高的获取

Rect outRect = new Rect();  activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);

outRect.top即是状态栏高度

3、应用区域(Activity)的宽高

Rect outRect = new Rect();  activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
w=outRect.rirht-outRect.left

h=outRect.bottom-outRect.top


ps:如果像妹子手机有smartBar、那么smartBar的高度=屏幕的高度-outRect.bottom(?guess)

注:在onCreate生命周期中有些尺寸或是无效的是无效的最好是在onWindowFocusChanged中获取

@SuppressLint("NewApi")private void showMetres() {int screenW,screenH;int statusBarH;int contentH;int smartBarH;                //获取屏幕的宽高                DisplayMetrics outMetrics=new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(outMetrics);screenW=outMetrics.widthPixels;screenH=outMetrics.heightPixels;//获取状态栏的高Rect outRect=new Rect();getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);statusBarH=outRect.top;//内容显示区的高contentH=outRect.bottom-outRect.top;//smartBarH=screenH-outRect.bottom;Rect rect=new Rect();getWindow().findViewById(Window.ID_ANDROID_CONTENT).getDrawingRect(rect);int i=rect.bottom-rect.top;smartBarH=screenH-rect.bottom;//这个可能不正确……!!!!StringBuilder sb=new StringBuilder("屏幕的宽为:"+screenW+"\n").append("屏幕的高为:"+screenH+"\n").append("状态栏的高为:"+statusBarH+"\n").append("标题栏的高为:"+getActionBar().getHeight()+"\n")//标题栏的高.append("内容显示区的高为(包含SmartBar):"+contentH+"\n").append("内容显示区的高为(不包含SmartBar):"+i+"\n").append("smartBar的高为:"+smartBarH);TextView tv = (TextView) findViewById(R.id.tv);tv.setText(sb.toString());}



0 0
原创粉丝点击