In Android applications, it is sometimes necessary to calculate the location of a view, resulting in the need to calculate status bar height, title bar height and other information. For the convenience of the future, make a simple record here.
Before drying the code, understand the division of the Android screen area, such as (the figure is referenced from this article http://www.iteye.com/topic/828830)
1 , capture of the screen area
[Java]View Plaincopy
- Activity.getwindowmanager (). Getdefaultdisplay ();
2 , access to the application area
[Java]View Plaincopy
- Rect outrect = new Rect ();
- Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (Outrect);
Where Outrect.top is the height of the status bar.
3. View Drawing Area Acquisition
[Java]View Plaincopy
- Rect outrect = new Rect ();
- Activity.getwindow (). Findviewbyid (window.id_android_content). Getdrawingrect (Outrect);
With the outrect.top of the drawing area-the outrect.top of the applied area is the height of the title bar.
Note: If you are just starting the activity to calculate this data, it is best to do in the onwindowfocuschanged function, otherwise the resulting data may be wrong, for example, the application area of high-wide acquisition.
The detailed code is as follows:
[Java]View Plaincopy
- Public class ScreenSize extends Activity {
- private TextView Mscreensizeview;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (r.layout.activity_screen_size);
- Mscreensizeview = (TextView) Findviewbyid (r.id.screen_size);
- }
- @Override
- public void Onwindowfocuschanged (boolean hasfocus) {
- super.onwindowfocuschanged (Hasfocus);
- if (hasfocus) {
- System.out.println ("second");
- StringBuilder sb = new StringBuilder ();
- Dimension dimen1 = Getareaone (this);
- Dimension Dimen2 = Getareatwo (this);
- Dimension Dimen3 = Getareathree (this);
- Sb.append ("area one: \n\twidth:" +dimen1.mwidth + "; \theight:" +dimen1.mheight);
- Sb.append ("\narea: \n\twidth:" +dimen2.mwidth + "; \theight:" +dimen2.mheight);
- Sb.append ("\narea Three: \n\twidth:" +dimen3.mwidth + "; \theight:" +dimen3.mheight);
- Mscreensizeview.settext (Sb.tostring ());
- }
- }
- @Override
- Public Boolean oncreateoptionsmenu (Menu menu) {
- Getmenuinflater (). Inflate (r.menu.activity_screen_size, menu);
- return true;
- }
- Private Dimension Getareaone (activity activity) {
- Dimension dimen = new Dimension ();
- Display disp = Activity.getwindowmanager (). Getdefaultdisplay ();
- Point outp = new Point ();
- Disp.getsize (OUTP);
- Dimen.mwidth = outp.x;
- Dimen.mheight = OUTP.Y;
- return dimen;
- }
- Private Dimension getareatwo (activity activity) {
- Dimension dimen = new Dimension ();
- Rect outrect = new Rect ();
- Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (Outrect);
- System.out.println ("Top:" +outrect.top +"; Left: "+outrect.left");
- Dimen.mwidth = Outrect.width ();
- Dimen.mheight = Outrect.height ();
- return dimen;
- }
- Private Dimension Getareathree (activity activity) {
- Dimension dimen = new Dimension ();
- //user Draw area
- Rect outrect = new Rect ();
- Activity.getwindow (). Findviewbyid (window.id_android_content). Getdrawingrect (Outrect);
- Dimen.mwidth = Outrect.width ();
- Dimen.mheight = Outrect.height ();
- //End
- return dimen;
- }
[Java]View Plaincopy
- Private class Dimension {
- public int mwidth;
- public int mheight;
- Public Dimension () {}
- }