This article was reproduced from: http://www.cnblogs.com/xqxacm/p/4918470.html
First of all, we need to know that there are two types of suspended Windows: Activity-level suspended windows, system-level suspended windows
The activity-level suspension window changes with the life cycle of the activity, while the system-level hover window can be detached from the activity.
As a result, to achieve the 360 mobile phone guard as the suspension window effect, you need to use the system-level suspension window
Below you learn the code steps to implement the Desktop hover window effect:
1, configure the list file Androidmanifest.xml to add the system suspended Windows permissions
<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>
2. Start writing the Activity code
First look at the member variables:
Private Windowmanager.layoutparams LP = new Windowmanager.layoutparams (); private static WindowManager WindowManager; private static ImageView ImageView;
OnCreate () Method:
Gets the Windwomanager object, which is a system-level
WindowManager = (WindowManager) getapplication (). Getsystemservice (Window_service);
Use WindowManager to display the top-level display of other applications, even the top of the phone's desktop.
3, add a UI space, as the contents of the suspended window, of course, the demo is a imageview as a suspended window content, the actual project needs to use complex view,viewgroup to expand the function
Notice that there is only one suspension window, and when the application is opened, the suspended window is generated, so to determine whether the suspended window already exists,
if (ImageView! = null) { windowmanager.removeview (ImageView); } Use application context to create UI controls that prevent activity destruction from causing context problems because the current hover window is system-level and does not depend on activity presence ImageView = new ImageView (Getapplicationcontext ()); Imageview.setimageresource (R.mipmap.normal);
4, set the system-level parameters of the suspended window, to ensure that the suspended window hanging in the mobile phone on the desktop
Lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; Lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
// Type_system_alert system prompts, it always appears on the application window //type_system_overlay System top-level window. is displayed on top of all other content. This window can not get input focus, otherwise affect the lock screen // flag_not_focusable suspended window is small, the app icon after the non-long press to long press, do not set this FLAG, the home page screen will have a problem // Flag_not_touch_modal do not block event delivery to a later window
For an explanation of Windowmanager.layoutparams, please refer to:Android Windowmanager.layoutparams class
5, the location of the default display of the suspended window
Lp.gravity = gravity.left| Gravity.top; displayed in the upper left corner of the screen
6, the suspension window relative to 5 the default position of the position difference and the suspended window width height setting
The relative position difference between the display position and the specified position lp.x = 0; lp.y = 0; The width and height of the suspended window lp.width = WindowManager.LayoutParams.WRAP_CONTENT; Lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
7. Set the background of the suspended window transparent
Lp.format = pixelformat.transparent;
8. Add the suspended window to the WindowManager object
Windowmanager.addview (IMAGEVIEW,LP);
9. Setting the response event for a suspended window
Here for mobile Hover window operation, you can expand your own to add clicks and other response events
Imageview.setontouchlistener (New View.ontouchlistener () {private float lastx;//X.Y coordinates of the previous location private float lasty; private float nowx; The x.y coordinates of the current moving position are private float Nowy; private float Tranx; The relative value of the moving position of the floating window private float trany; @Override public boolean OnTouch (View V, motionevent event) {Boolean ret = false; Switch (event.getaction ()) {case Motionevent.action_down://Gets the x, y coordinates when pressed LASTX = Event.getrawx (); Lasty = Event.getrawy (); ret = true; Break Case Motionevent.action_move://Gets the x, y coordinates of the move nowx = EVENT.GETRAWX (); Nowy = Event.getrawy (); Calculates the xy coordinate offset tranx = NOWX-LASTX; Trany = Nowy -Lasty; Mobile Suspension window lp.x + = Tranx; Lp.y + = Trany; Update the hover window position windowmanager.updateviewlayout (IMAGEVIEW,LP); Records the current coordinates as the position coordinates of the last movement of the next calculation lastx = NOWX; Lasty = Nowy; Break Case MotionEvent.ACTION_UP:break; } return ret; } });
10.:
Full code:
Note Add Permissions!!
PackageCom.xqx.window.app;Importandroid.app.Activity;ImportAndroid.graphics.PixelFormat;ImportAndroid.os.Bundle;Importandroid.view.*;ImportAndroid.widget.ImageView;/*** System-level hover window, which can be displayed on the mobile desktop*/ Public classFloatwindowactivityextendsActivity {PrivateWindowmanager.layoutparams LP =NewWindowmanager.layoutparams (); Private StaticWindowManager WindowManager; Private StaticImageView ImageView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_float_window); //1. Get system-level WindowManagerWindowManager =(WindowManager) getapplication (). Getsystemservice (Window_service); //determines whether the UI control exists, is removed, and ensures that any application opens with only one hover window if(ImageView! =NULL) {Windowmanager.removeview (ImageView); } //2. Use application context to create UI controls to avoid activity destruction causing context problemsImageView =NewImageView (Getapplicationcontext ()); Imageview.setimageresource (R.mipmap.normal); //3, set the system-level parameters of the suspension window, to ensure that the suspended window hanging on the mobile desktop//The system level needs to specify the Type property//Type_system_alert allow events to be received//Type_system_overlay suspended on the system//Note Add permissions to the manifest file//system prompts. It always appears on top of the application window. Lp.type =WindowManager.LayoutParams.TYPE_SYSTEM_ALERT|WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; //Flag_not_touch_modal does not block events from passing to subsequent Windows//flag_not_focusable when the suspension window is small, the application icon behind it can be long-pressed without long press, and if you do not set this FLAG, the screen of the home page will have a problem .Lp.flags =WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; //the default display location of the suspended windowLp.gravity = gravity.left|Gravity.top; //relative position difference between the display position and the specified positionlp.x = 0; LP.Y= 0; //width and height of suspended windowLp.width =WindowManager.LayoutParams.WRAP_CONTENT; Lp.height=WindowManager.LayoutParams.WRAP_CONTENT; Lp.format=pixelformat.transparent; Windowmanager.addview (IMAGEVIEW,LP); //setting up a hover window listener eventImageview.setontouchlistener (NewView.ontouchlistener () {Private floatLASTX;//x.y coordinates of the last position Private floatlasty; Private floatNOWX;//x.y coordinates of the current moving position Private floatNowy; Private floatTranx;//relative value of moving position of suspended window Private floatTrany; @Override Public BooleanOnTouch (View V, motionevent event) {BooleanRET =false; Switch(Event.getaction ()) { CaseMotionevent.action_down://gets the x, y coordinates of the pressedLASTX =event.getrawx (); Lasty=Event.getrawy (); RET=true; Break; CaseMotionevent.action_move://gets the x, y coordinates of the moveNOWX =event.getrawx (); Nowy=Event.getrawy (); //Calculate XY coordinate offsetTranx = nowx-Lastx; Trany= Nowy-lasty; //moving the suspended windowlp.x + =Tranx; LP.Y+=Trany; //Update the hover window positionwindowmanager.updateviewlayout (IMAGEVIEW,LP); //record the current coordinates as the position coordinates of the last move of the next calculationLASTX =nowx; Lasty=Nowy; Break; Casemotionevent.action_up: Break; } returnret; } }); }}
(reproduced) Simple and easy to drag desktop suspension window effect