A ndroid 獲取屏幕高度、標題高度、狀態欄高度詳解

Android獲取屏幕高度的方法主要由view提供 android


經過View提供的方法獲取高度方式有兩種: canvas

 

1, 當前顯示的view中直接獲取當前view高寬
2, 經過Activity的getWindow().findViewById(Window.ID_ANDROID_CONTENT)獲取系統當前顯示的view根(是一個framelayout對象),android繪製會將要繪製的view放置在framelayout中繪製。 測試

 


下面分別介紹獲取屏幕的高寬度方法 this



虛線區域介紹: 



View獲取屏幕參數值方法: spa


 

Display對象獲取屏幕高寬 : 對象


獲取display對象 Activity中getWindowManager().getDefaultDisplay()
getWidth() 返回顯示界面寬度即屏幕寬度
getHeight() 返回顯示界面高度即屏幕高度

由display對象設置DisplayMetrics高寬值,經過DisplayMetrics對象獲取屏幕高寬,有點畫蛇添足 : get


getWidth() 返回顯示界面寬度即屏幕寬度
getHeight() 返回顯示界面高度即屏幕高度 it

 


經常使用一些值計算: scroll


屏幕高寬 
Canvas對象 、display對象和DisplayMetrics可獲取屏幕的高寬 方法


狀態欄高度 
View的getWindowVisibleDisplayFrame(Rect outRect)附值outRect後,outRect.top()便是狀態欄高度


標題高度 
View的getWindowVisibleDisplayFrame(Rect outRect1)附值outRect後,outRect.height()-view.getheight()便是標題高度。


繪製區域高寬 
方法諸多 隨便用。


測試:


測試代碼


scrollTo(10, 10);
super.draw(canvas);
Display d = bReader.getWindowManager().getDefaultDisplay();
Log.e("====DisPlay size==", "Height--"+d.getHeight()+"  Width--"+d.getWidth());
DisplayMetrics dm = new DisplayMetrics();
d.getMetrics(dm);
Log.e("====DisPlayMetrics size==", "Height--"+d.getHeight()+"  Width--"+d.getWidth());
Log.e("====View size==", "Height--"+getHeight()+"  Width--"+getWidth()+" Top--"+getTop()+"  Left--"+getLeft());
View v = bReader.getWindow().findViewById(Window.ID_ANDROID_CONTENT);
Log.e("====CONTENTView size==", "Height--"+v.getHeight()+"  Width--"+v.getWidth()+" Top--"+v.getTop()+"  Left--"+v.getLeft());
Log.e("======canvas size==", "height--"+canvas.getHeight()+" width--"+canvas.getWidth());
Rect rect = new Rect();
this.getDrawingRect(rect);
Log.e("====view Drawing Rect==", "height--"+rect.height()+" width--"+rect.width()+" Top--"+rect.top+"  Left--"+rect.left+" scrollx--"+getScrollX()+" scrollY--"+getScrollY());
this.getWindowVisibleDisplayFrame(rect);
Log.e("====view WindowVisible rect==", "height--"+rect.height()+" width--"+rect.width()+" Top--"+rect.top+"  Left--"+rect.left);


測試結果輸出:


====DisPlay size==(3032): Height--480  Width--320
====DisPlayMetrics size==(3032): Height--480  Width--320
====View size==(3032): Height--430  Width--320 Top--0  Left--0
====CONTENTView size==(3032): Height--430  Width--320 Top--50  Left--0
======canvas size==(3032): height--480 width--320
====view Drawing Rect==(3032): height--430 width--320 Top--10  Left--10 scrollx--10 scrollY--10
====view WindowVisible rect==(3032): height--455 width--320 Top--25  Left--0

界面: 


 

全屏顯示輸出:  ====DisPlay size==(3235): Height--480  Width--320 ====DisPlayMetrics size==(3235): Height--480  Width--320 ====View size==(3235): Height--480  Width--320 Top--0  Left--0 ====CONTENTView size==(3235): Height--480  Width--320 Top--0  Left--0 ======canvas size==(3235): height--480 width--320 ====view Drawing Rect==(3235): height--480 width--320 Top--10  Left--10 scrollx--10 scrollY--10 ====view WindowVisible rect==(3235): height--455 width--320 Top--25  Left--0