Technical Summary of the android project: obtain the status bar and title bar and calculate the length and width to limit the activity range of the floating window.

Source: Internet
Author: User

Obtain the height of the status bar:

Rect frame = new rect (); (moveviewdemo) context ). getwindow (). getdecorview (). getwindowvisibledisplayframe (FRAME); // all areas except the display area that comes with the system are obtained here. statusbarheight = frame is located in all areas except the top one that shows power. top; // you can obtain the height of the status bar, that is, the top line shows power and signal.

Obtain the height of the title bar:

Int contentviewtop = (moveviewdemo) context ). getwindow (). findviewbyid (window. id_android_content ). gettop (); // The black part of the following two images is displayed, that is, the area where the code shows int titlebarheight = contentviewtop-statusbarheight; // you can obtain the height of the title bar, that is, the second display application name.

I want to create a floating window. The top of the window can reach the status bar, overwrite the title bar, and the bottom and left reach the border respectively. For example:

 

Because you know the height of the status bar and title bar, let's take a look at the rewrite code in the dialog box.

Private Static class searchdialog extends dialog {private int lastx, lasty, screenwidth, screenheight, statusbarheight; private window; private int lleft, lright, ltop, lbottom; private Static final int offset_dist_y = 80; private windowmanager. layoutparams Wl; Public searchdialog (context) {super (context);} public searchdialog (context, int theme, int viewresid) {super (context, theme ); setcontentview (viewresid); rect frame = new rect (); (moveviewdemo) context ). getwindow (). getdecorview (). getwindowvisibledisplayframe (FRAME); statusbarheight = frame. top; // 25int contentviewtop = (moveviewdemo) context ). getwindow (). findviewbyid (window. id_android_content ). gettop (); // 50 displaymetrics dm = context. getresources (). getdisplaymetrics (); screenwidth = DM. widthpixels; // 320 screenheight = DM. heightpixels; // 480log. I ("test", "statusbarheight:" + statusbarheight + ", contentviewtop:" + contentviewtop + ".......... screenwidth: "+ screenwidth +", screenheight: "+ screenheight); window = getwindow (); WL = Window. getattributes (); WL. gravity = gravity. center; WL. width = (INT) (screenwidth * 0.88); // 281wl. height = (INT) (screenheight * 0.47); // 225wl. Y + = offset_dist_y; // 80window. setattributes (WL); lleft = lright = (screenwidth-WL. width)/2; // 19 ltop = (screenheight-WL. height-statusbarheight)/2; // 115 lbottom = (screenheight-WL. height-statusbarheight)/2; log. I ("test", (INT) (screenwidth * 0.88) + "......... "+ (INT) (screenheight * 0.47) + "....... "+ lleft + "........ "+ ltop + "........ w. y: "+ WL. y) ;}@ overridepublic Boolean ontouchevent (motionevent event) {int Ea = event. getaction (); Switch (EA) {Case motionevent. action_down: lastx = (INT) event. getrawx (); // obtain the original X coordinate lasty = (INT) event of the touch position of the touch event. getrawy (); log. I ("test", "lastx:" + lastx + ", lasty:" + lasty); break; Case motionevent. action_move: int dx = (INT) event. getrawx ()-lastx; // The offset of X // absolute rawx: 319 rawy: 479int DY = (INT) event. getrawy ()-lasty; // offset of Y int Cx = (INT) event. getx (); // relative to xint Cy = (INT) event. gety (); // relative to ylog. I ("test", "rawx:" + event. getrawx () + ", rawy:" + event. getrawy () + ", DX:" + dx + ", DY:" + dy + ", CX:" + cx + ", Cy:" + cy ); if (CY> 0 & CX> 0 & CX <WL. width & Cy <WL. height) {WL. Y + = Dy; // y is smaller than 0 and greater than 0. X + = DX; If (WL. x <=-lleft) {WL. X =-lleft;} If (WL. x> = lright) {WL. X = lright;} If (WL. Y <=-ltop) {WL. y =-ltop;} If (WL. y> = lbottom) {WL. y = lbottom;} window. setattributes (WL);} lastx = (INT) event. getrawx (); lasty = (INT) event. getrawy (); break; Case motionevent. action_up: log. I ("up", "lleft:" + lleft + ", lright:" + lright + ", ltop:" + ltop + ", lbottom:" + lbottom); log. I ("X + Y", "WL. x: "+ WL. X + ", WL. y: "+ WL. y); break;} return true;} @ overridepublic void setcontentview (INT layoutresid) {super. setcontentview (layoutresid );}}

Decorview is the top-level view in the window. You can obtain the decorview from the window. Then, decorview has a getwindowvisibledisplayframe method to obtain the display area of the program, including the title bar, but not the status bar.

My screen is 320*480, density: 160. I can see a lot of moving a thing or image is written in the ontouchevent method v. layout (INT, Int, Int, INT) and other methods. The window is used here, so the window parameter setting is different from other examples.

, With code: http://download.csdn.net/detail/duancanmeng/4095727

 

 

 

 

------------------------------------------------------------------------ Gorgeous split line ---------------------------------------------------------------------------------------------

 

 

Through the latest test, I found that my previous understanding was not deep enough. I specially wrote a code to enhance my understanding of what was previously used to obtain the status bar and title bar. You can first look:

The Code is as follows:

Rect frame = new rect (); this. getwindow (). getdecorview (). getwindowvisibledisplayframe (FRAME); view = This. getwindow (). findviewbyid (window. id_android_content); window = getwindow (). getattributes (); string STR = "view. getwidth (): "+ view. getwidth () + "\ r \ n" + ", view. getheight: "+ view. getheight () + "\ r \ n" + "view. gettop (): "+ view. gettop () + "\ r \ n" + ", view. getbottom (): "+ view. getbottom () + "\ r \ n" + ", view. getleft (): "+ view. getleft () + "\ r \ n" + ", view. getright (): "+ view. getright () + "\ r \ n"; string framestr = "frame. getwidth (): "+ frame. width () + "\ r \ n" + ", frame. getheight: "+ frame. height () + "\ r \ n" + "frame. gettop (): "+ frame. top + "\ r \ n" + ", frame. getbottom (): "+ frame. bottom + "\ r \ n" + ", frame. getleft (): "+ frame. left + "\ r \ n" + ", frame. getright (): "+ frame. right + "\ r \ n"; Hello. settext ("when the dialog box is not displayed, the frame attribute is:" + "\ r \ n" + "frame. tostring (): "+ framestr +" \ r \ n "+" view. tostring (): "+ Str); Hello. setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {// todo auto-generated method stub showdialog (R. id. hello) ;}}) ;}@ override protected dialog oncreatedialog (int id) {If (ID = R. id. hello) {rect frame = new rect (); this. getwindow (). getdecorview (). getwindowvisibledisplayframe (FRAME); dialog = new dialog (mainactivity. this); dialog. settitle ("test"); textview text = new textview (mainactivity. this); view = mainactivity. this. getwindow (). findviewbyid (window. id_android_content); string STR = "view. getwidth (): "+ view. getwidth () + "\ r \ n" + ", view. getheight: "+ view. getheight () + "\ r \ n" + "view. gettop (): "+ view. gettop () + "\ r \ n" + ", view. getbottom (): "+ view. getbottom () + "\ r \ n" + ", view. getleft (): "+ view. getleft () + "\ r \ n" + ", view. getright (): "+ view. getright () + "\ r \ n"; string framestr = "frame. getwidth (): "+ frame. width () + "\ r \ n" + ", frame. getheight: "+ frame. height () + "\ r \ n" + "frame. gettop (): "+ frame. top + "\ r \ n" + ", frame. getbottom (): "+ frame. bottom + "\ r \ n" + ", frame. getleft (): "+ frame. left + "\ r \ n" + ", frame. getright (): "+ frame. right + "\ r \ n"; text. settext ("after the pop-up dialog box, the attributes of the frame:" + "\ r \ n" + "frame. tostring (): "+ framestr +" \ r \ n "+" view. tostring (): "+ Str); dialog. setcontentview (text); dialog. show ();} return Super. oncreatedialog (ID );}

It can be seen that the key of the above Code is to test the scope of the Code:

this.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);View view = this.getWindow().findViewById(Window.ID_ANDROID_CONTENT);

For the moment, we will use frame to represent the scope of the first sentence, and view to represent the scope of the second sentence.

From the first figure above, we can see that when an activity is fully displayed and all are in the clickable status, the frame function ranges from the entire screen, for example, A + B + C, the view is like an empty area in the upper left corner.

When we click the text in this section, a dialog box is displayed. The Code shows that an attribute output is made for the above two sentences. The attributes can be seen through analysis, the scope of frame is all the gray parts except the white status bar above, such as the B + C part. The scope of view is the part of the title bar after the frame, for example, F.

Because decorview is the top-level view in the window, the top-level view is the activity and the above status bar before clicking. After clicking, the displayed view layer overwrites the original activity, which is the E + G part. In addition to the dialog box, the G part also contains the surrounding black part, the essence is the entire f Part, except that the F layer is under G.

Some people said: The view obtained by the getwindow (). findviewbyid (window. id_android_content) method is the part of the program that does not include the title bar. Then you can know the height of the title bar.

In this case, why can't I get a region before I click it?

I have not understood the changes here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.