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


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


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

 

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

 


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



虛線區域介紹: 



View獲取屏幕參數值方法:


 

Display對象獲取屏幕高寬 :


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

由display對象設置DisplayMetrics高寬值,通過DisplayMetrics對象獲取屏幕高寬,有點多此一舉 :


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

 


常用一些值計算:


屏幕高寬 
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(windows.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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM