// Create a global variable class
1 public class myapplication extends application {
2
3 /**
4 * create a global variable
5 * global variables generally tend to create a separate data class file and use static variables.
6 *
7 * the method of adding data to the application is used to implement global variables.
8 * Add the Android: Name = ". myapplication" attribute to the application node in androidmanifest. xml.
9 *
10 */
11 private windowmanager. layoutparams wmparams = new windowmanager. layoutparams ();
12
13 public windowmanager. layoutparams getmywmparams (){
14 return wmparams;
15}
// Activity Class
1 public class myfloatviewactivity extends activity {
2
3
4/** called when the activity is first created .*/
5
6 private windowmanager WM = NULL;
7 private windowmanager. layoutparams wmparams = NULL;
8
9 private myfloatview myfv = NULL;
10
11
12 @ override
13 public void oncreate (bundle savedinstancestate ){
14 super. oncreate (savedinstancestate );
15 setcontentview (R. layout. Main );
16
18}
19
22 private void createview (){
23 myfv = new myfloatview (getapplicationcontext ());
24 myfv. setimageresource (R. drawable. Icon );
25 // obtain windowmanager
26 WM = (windowmanager) getapplicationcontext (). getsystemservice ("window ");
27 // set parameters related to layoutparams (global variable)
28 wmparams = (myapplication) getapplication (). getmywmparams ();
29
30 /**
31 * The following are all related properties of windowmanager. layoutparams.
32 * for specific purposes, refer to the SDK documentation
33 */
34 wmparams. type = layoutparams. type_phone; // set the window type.
35 wmparams. format = pixelformat. rgba_8888; // set the image format. The effect is transparent to the background.
36
37 // set window flag
38 wmparams. Flags = layoutparams. flag_not_touch_modal
39 | layoutparams. flag_not_focusable;
40 /*
The flags attribute under 41 * has the same effect as "locked ".
42 * The floating window cannot be touched, and no event is accepted, and the subsequent event response is not affected.
43 wmparams. Flags = layoutparams. flag_not_touch_modal
44 | layoutparams. flag_not_focusable
45 | layoutparams. flag_not_touchable;
46 */
47
48
49 wmparams. Gravity = gravity. Left | gravity. Top; // adjust the floating window to the upper left corner.
50 // set the starting value of X and Y in the upper left corner of the screen
51 wmparams. x = 0;
52 wmparams. Y = 0;
53
54 // set the long and wide data of the floating window
55 wmparams. width = 40;
56 wmparams. Height = 40;
57
58 // display the myfloatview Image
59 WM. addview (myfv, wmparams );
60
61}
62
63 @ override
64 public void ondestroy (){
65 super. ondestroy ();
66 // destroy the floating window when the program exits (activity destruction)
67 WM. removeview (myfv );
68}
69
70 @ override
71 protected void onrestart (){
72 // todo auto-generated method stub
73 WM. removeview (myfv );
74 super. onrestart ();
75}
76
79 @ override
80 protected void onstop (){
81 // todo auto-generated method stub
82 // create a floating window
83 createview ();
84 super. onstop ();
85}
// Floating window content class
1 public class myfloatview extends imageview {
2 Private float mtouchstartx;
3 private float mtouchstarty;
4 private float X;
5 private float y;
6
8 private windowmanager WM = (windowmanager) getcontext (). getapplicationcontext (). getsystemservice ("window ");
9
10 // This wmparams is the obtained global variable used to save the properties of the floating window
11 private windowmanager. layoutparams wmparams = (myapplication) getcontext (). getapplicationcontext (). getmywmparams ();
12
13 public myfloatview (context ){
14 super (context );
15 // todo auto-generated constructor stub
16}
17
18 @ override
19 public Boolean ontouchevent (motionevent event ){
20
21 // getrawx () gets the coordinates of the relative screen, that is, starting from the upper left corner of the screen
22 x = event. getrawx ();
23 y = event. getrawy ()-25; // 25 is the height of the system status bar
24 log. I ("currp", "currx" + x + "=== Curry" + y );
25 switch (event. getaction ()){
26 case motionevent. action_down:
27 // getx () gets the coordinates of the relative view, that is, the origin of the upper-left corner of the View
28 mtouchstartx = event. getx ();
29 mtouchstarty = event. Gety ();
30
31 log. I ("startp", "startx" + mtouchstartx + "=== starty" + mtouchstarty );
32
33 break;
34 case motionevent. action_move:
35 updateviewposition ();
36 break;
37
38 case motionevent. action_up:
39 updateviewposition ();
40 mtouchstartx = mtouchstarty = 0;
41 break;
42}
43 return true;
44}
45
46 private void updateviewposition (){
47 // update the floating window position parameter. X indicates the mouse position on the screen, and mtouchstartx indicates the mouse position on the image.
48 wmparams. x = (INT) (X-mtouchstartx );
49 system. Out. println (mtouchstartx );
50 wmparams. Y = (INT) (Y-mtouchstarty );
51 WM. updateviewlayout (this, wmparams );
52
53}
54
55}
Set in androidmanifest:
<Uses-Permission Android: Name = "android. Permission. system_alert_window"/>