Today, we found that some software can display some floating small windows on the android desktop. After some searches, we finally found a solution,CodeAs follows:
Floatingfunc. Java
Package hrxcframe. Comm;
Import Android. App. activity;
Import Android. content. context;
Import Android. Graphics. rect;
Import Android. Test. R;
Import Android. util. log;
Import Android. View. gravity;
Import Android. View. layoutinflater;
Import Android. View. motionevent;
Import Android. View. view;
Import Android. View. window;
Import Android. View. windowmanager;
Import Android. View. windowmanager. layoutparams;
/**
* The floating menu is always displayed at the top of the android screen.
*
* @ Author liujl V1.0 you need to add <uses-Permission
* Android: Name = "android. Permission. system_alert_window"
*/> <! -- System pop-up window permission --> permission, otherwise an error will be reported
*/
Public class floatingfunc {
/**
* X coordinates of floating windows on the screen
*/
Private Static float x = 0;
/**
* Y coordinates of floating windows on the screen
*/
Private Static float y = 200;
/**
* Screen touch status, not used for the moment
*/
Private Static float state = 0;
/**
* Start position of the mouse touch
*/
Private Static float mtouchstartx = 0;
/**
* Mouse touch end position
*/
Private Static float mtouchstarty = 0;
/**
* Windows Window Manager
*/
Private Static windowmanager WM = NULL;
/**
* Floating display object
*/
Private Static view floatingviewobj = NULL;
/**
* Parameter setting class
*/
Public static windowmanager. layoutparams Params = new windowmanager. layoutparams ();
Public static int tool_bar_high = 0;
/**
* Objects to be displayed at the beginning of the window
*/
Private Static view view_obj = NULL;
/**
* Method to display at the beginning of the window
*
* @ Param Context
* Call the context getapplicationcontext () object ()
* @ Param window
* Call the window getwindow () object ()
* @ Param floatingviewobj
* Floating object view to be displayed
*/
Public static void show (context, window, view floatingviewobj ){
// Load the sample code of the style in the XML file
// ******************************** Start ***** *********************
// Layoutinflater Inflater =
// Layoutinflater. From (getapplicationcontext ());
// View = Inflater. Inflate (R. layout. topframe, null );
// WM =
// (Windowmanager) Context. getapplicationcontext (). getsystemservice (context. window_service );
// Load the sample code of the style in the XML file
// ********************************* End **** ***********************
//
// Close the floating display object and then display it again
Close (context );
Floatingfunc. floatingviewobj = floatingviewobj;
View_obj = floatingviewobj;
Rect frame = new rect ();
// This sentence is the key to display it on the top layer
// Getwindow ()
Window. getdecorview (). getwindowvisibledisplayframe (FRAME );
Tool_bar_high = frame. Top;
WM = (windowmanager) Context // getapplicationcontext ()
. getsystemservice (activity. window_service);
Params. type = windowmanager. layoutparams. type_system_alert
| windowmanager. layoutparams. type_system_overlay;
Params. flags = layoutparams. flag_not_touch_modal
| layoutparams. flag_not_focusable;
// Set the long and wide data of the floating window
Params. width = windowmanager. layoutparams. wrap_content;
Params. Height = windowmanager. layoutparams. wrap_content;
// Sets the transparency.
Params. Alpha = 80;
// Set the internal text alignment
Params. Gravity = gravity. Left | gravity. Top;
// Set the initial values of X and Y to the starting point in the upper left corner of the screen
Params. x = (INT) X;
Params. Y = (INT) y;
// TV = new mytextview (topframe. This );
WM. addview (floatingviewobj, Params );
}
/**
* Follow whom to move
*
* @ Param event
* Event object
* @ Param View
* Pop-up object instance (view)
* @ Return
*/
Public static Boolean ontouchevent (motionevent event, view ){
// Obtain the coordinates of the relative screen, that is, starting from the upper-left corner of the screen
X = event. getrawx ();
Y = event. getrawy ()-25; // 25 indicates the height of the system status bar.
Log. I ("currp", "currx" + x + "=== Curry" + Y); // debug information
Switch (event. getaction ()){
Case motionevent. action_down:
State = motionevent. action_down;
// Pantime ();
// Obtain the coordinates of the relative view, that is, the upper-left corner of the view is the origin.
Mtouchstartx = event. getx ();
Mtouchstarty = event. Gety ();
Log. I ("startp", "startx" + mtouchstartx + "=== starty"
+ Mtouchstarty); // debug information
Break;
Case motionevent. action_move:
State = motionevent. action_move;
Updateviewposition (View );
Break;
Case motionevent. action_up:
State = motionevent. action_up;
Updateviewposition (View );
Mtouchstartx = mtouchstarty = 0;
Break;
}
Return true;
}
/**
* Disable floating display objects
*/
Public static void close (context ){
If (view_obj! = NULL & view_obj.isshown () {
windowmanager WM = (windowmanager) Context
. getsystemservice (activity. window_service);
WM. removeview (view_obj);
}< BR >}
/**
* Update the position of the pop-up window
*/
Private Static void updateviewposition (view ){
// Update the floating window position Parameter
Params. x = (INT) (X-mtouchstartx );
Params. Y = (INT) (Y-mtouchstarty );
WM. updateviewlayout (floatingfunc. floatingviewobj, Params );
}
}
Test code, which can be placed in any activity code. You can drag it by hand.
Final textview TV = new textview (getapplicationcontext ());
TV. settext ("http://www.my400800.cn ");
View. setontouchlistener (New View. ontouchlistener (){
@ Override
Public Boolean ontouch (view arg0, motionevent arg1 ){
Floatingfunc. ontouchevent (arg1, TV );
Return true;
}
});
The effect is as follows:
Note:
Must be added in androidmanifest. xml
<Uses-Permission Android: Name = "android. Permission. system_alert_window"/>
System permission. Otherwise, an error occurs .............