Suddenly interested in the suspension form, check the data to do a small demo, the effect is to click the button, close the current activity, display the suspended window, the window can be dragged, double-click disappears. as follows:
Its use principle is very simple, is to borrow the WindowManager this management class to realize.
1. First add the usage permissions in Androidmanifest.xml:
<android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>
2. floating Window Layout implementation
public class desktoplayout Span style= "Color:rgb (0,0,255)" >extends linearlayout {public Desktoplayout (Context context) {super (context); SetOrientation (linearlayout.vertical); // horizontal arrangement // set wide height this . Setlayoutparams (new Layoutparams (Layoutparams.wrap_content, layoutparams.wrap_content)); View view = Layoutinflater.from (context). Inflate (R.layout.desklayout, n Ull ); this . AddView (view); }
3. Let it show up in the activity.
//Get System FormMwindowmanager = (WindowManager) getapplicationcontext (). Getsystemservice ("window");//layout styles for formsMlayout =NewWindowmanager.layoutparams ();//Set the form display Type--type_system_alert (System tip)Mlayout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;//set the form focus and touch://flag_not_focusable (Cannot get key input focus)Mlayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;//Set the display modeMlayout.format = pixelformat.rgba_8888;//to set the alignment methodmlayout.gravity = Gravity.top | Gravity.left;//Set form width and heightMlayout.width = WindowManager.LayoutParams.WRAP_CONTENT; Mlayout.height = WindowManager.LayoutParams.WRAP_CONTENT;
The detailed mainactivity code is as follows:
PackageCom.yc.yc_suspendingform;Importandroid.app.Activity;ImportAndroid.graphics.PixelFormat;ImportAndroid.graphics.Rect;ImportAndroid.os.Bundle;ImportAndroid.util.Log;Importandroid.view.Gravity;ImportAndroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.View.OnTouchListener;ImportAndroid.view.WindowManager;ImportAndroid.widget.Button;ImportCom.yc.yc_floatingform. R Public classMainactivityextendsActivity {PrivateWindowManager Mwindowmanager;PrivateWindowmanager.layoutparams mlayout;PrivateDesktoplayout mdesktoplayout;Private LongStartTime;//declaring the width height of the screen floatx, y;intTop @Overrideprotected voidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Createwindowmanager (); Createdesktoplayout (); Button btn = (button) Findviewbyid (R.ID.BTN); Btn.setonclicklistener (NewOnclicklistener () { Public voidOnClick (View v) {showdesk (); } }); }/*** Create a suspended form*/ Private voidCreatedesktoplayout () {mdesktoplayout =NewDesktoplayout ( This); Mdesktoplayout.setontouchlistener (NewOntouchlistener () {floatMtouchstartx;floatMtouchstarty; @Override Public BooleanOnTouch (View V, motionevent event) {//gets the coordinates of the relative screen, which is the origin point in the upper-left corner of the screenx = Event.getrawx (); y = Event.getrawy ()-top;//25 is the height of the system status barLOG.I ("STARTP", "StartX" + mtouchstartx + "====starty" + mtouchstarty);Switch(Event.getaction ()) { CaseMotionevent.action_down://gets the coordinates of the relative view, which is the origin point in the upper left corner of the viewMtouchstartx = Event.getx (); Mtouchstarty = Event.gety (); LOG.I ("STARTP", "StartX" + mtouchstartx + "====starty" + mtouchstarty);LongEnd = System.currenttimemillis ()-startTime;//double-click the interval below 300ms if(End < 300) {Closedesk (); } startTime = System.currenttimemillis (); Break; CaseMotionevent.action_move://Update floating window position ParametersMlayout.x = (int) (X-MTOUCHSTARTX); Mlayout.y = (int) (Y-mtouchstarty); Mwindowmanager.updateviewlayout (V, mlayout); Break; CaseMOTIONEVENT.ACTION_UP://Update floating window position ParametersMlayout.x = (int) (X-MTOUCHSTARTX); Mlayout.y = (int) (Y-mtouchstarty); Mwindowmanager.updateviewlayout (V, mlayout);//the last position can be recorded hereMtouchstartx = Mtouchstarty = 0; Break; }return true; } }); } @Override Public voidOnwindowfocuschanged (BooleanHasfocus) {Super. onwindowfocuschanged (Hasfocus); Rect rect =NewRect ();///Get the entire view section, note that if you want to set the heading style, this must appear after the heading style, otherwise it will be wrongGetWindow (). Getdecorview (). Getwindowvisibledisplayframe (rect); top = Rect.top;//the height of the status bar, so the rect.height,rect.width is the width of the system's height, respectivelyLOG.I ("Top", "+top"); }/*** Show Desktoplayout*/ Private voidShowdesk () {Mwindowmanager.addview (mdesktoplayout, mlayout); Finish (); }/*** Close Desktoplayout*/ Private voidClosedesk () {Mwindowmanager.removeview (mdesktoplayout); Finish (); }/*** Set WindowManager*/ Private voidCreatewindowmanager () {//Get System FormMwindowmanager = (WindowManager) getapplicationcontext (). Getsystemservice ("window");//layout styles for formsMlayout =NewWindowmanager.layoutparams ();//Set the form display Type--type_system_alert (System tip)Mlayout.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;//set the form focus and touch://flag_not_focusable (Cannot get key input focus)Mlayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;//Set the display modeMlayout.format = pixelformat.rgba_8888;//to set the alignment methodmlayout.gravity = Gravity.top | Gravity.left;//Set form width and heightMlayout.width = WindowManager.LayoutParams.WRAP_CONTENT; Mlayout.height = WindowManager.LayoutParams.WRAP_CONTENT; }}View Code
Source code Address:https://github.com/YeXiaoChao/Yc_suspendingform
Resources:
Android Learning Note (60) use bundles to change the teaching connection between activityAndroid Learning Notes ($) Android development start and close activityAndroid Learning Notes (+) activity learning processMore Android Development Tutorials
Android Development realizes suspension form