Recently there are students to do graduation design, want to use the effect of suspension window, in fact, we can do this through the system service WindowManager, this chapter we will try to create a suspended view above the current activity.
First step: Know WindowManager
L This interface is used to interact with Window Manager (Windows Manager, application framework layer).
L can obtain an instance of WM by Getsystemservice (Context.window_service).
L Inheritance Relationship
Public interface WindowManager implements Viewmanager
L belongs to the package
Android.view.WindowManager
L Important Methods
AddView () Add view
Removeview () Delete view
Updateviewlayout () Change the view parameters
Window Manager Serviceis global and unique. It takes the user's actions, translates them into instructions, and sends them to the variousWindow. ActivityThe top-level controls are registered to theWindow Managerwhen the user really touches the screen or keyboard,Window ManagerWill be notified, and when the control has some requests, it will beviewparentsent back toWindow Managerthe. Thus completing the entire communication process
Step two: Rewrite the Ontouchevent method of ImageView
The previous step we know WindowManager can add, delete, change view, then want to achieve the drag effect of the suspended window we will get the coordinates of the ImageView position.
Gets the coordinates of the relative screen, which is the origin point in the upper-left corner of the screen
float x = event.getrawx ();
Float y = Event.getrawy ()-25; 25 is the height of the system status bar
L set X, y through Windowmanager.layoutparams wmparams
wmparams.x= (int) (X-MTOUCHSTARTX);
wmparams.y= (int) (y-mtouchstarty);
L Then set the current position of the suspended window through the Updateviewlayout () method
Step three: Join Permissions
Add the following permissions to the Androidmanifest.xml:
<android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>
The effect is as follows:
Important Code : Create myapplication
Importandroid.app.Application;ImportAndroid.view.WindowManager; Public classMyApplicationextendsApplication {/*** Create global variables * Note the application node in Androidmanifest.xml add Android:name= ". MyApplication "Properties **/ PrivateWindowmanager.layoutparams wmparams=NewWindowmanager.layoutparams (); Publicwindowmanager.layoutparams Getmywmparams () {returnWmparams; }}
Create a custom view inheritance ImageView
ImportAndroid.content.Context;ImportAndroid.util.Log;Importandroid.view.MotionEvent;ImportAndroid.view.WindowManager;ImportAndroid.widget.ImageView; Public classMyfloatviewextendsImageView {Private floatMtouchstartx; Private floatMtouchstarty; Private floatx; Private floaty; PrivateWindowManager wm=(WindowManager) GetContext (). Getapplicationcontext (). Getsystemservice (Context.window_service); //This wmparams is the obtained global variable that holds the properties of the suspended window PrivateWindowmanager.layoutparams Wmparams =((MyApplication) GetContext (). Getapplicationcontext ()). Getmywmparams (); PublicMyfloatview (Context context) {Super(context); //TODO auto-generated Constructor stub} @Override Public Booleanontouchevent (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 ()-25;//25 is the height of the system status barLOG.I ("CURRP", "Currx" +x+ "====curry" +y); 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 (); Break; CaseMotionEvent.ACTION_MOVE:updateViewPosition (); Break; CaseMotionEvent.ACTION_UP:updateViewPosition (); Mtouchstartx=mtouchstarty=0; Break; } return true; } Private voidupdateviewposition () {//Update floating window position ParametersWmparams.x= (int) (X-mtouchstartx); Wmparams.y=(int) (Ymtouchstarty); Wm.updateviewlayout ( This, Wmparams); }}
Create activity
Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.graphics.PixelFormat;ImportAndroid.os.Bundle;ImportAndroid.util.Log;Importandroid.view.Gravity;ImportAndroid.view.View;ImportAndroid.view.WindowManager;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.WindowManager.LayoutParams; Public classMyfloatviewactivityextendsactivity{PrivateWindowManager wm=NULL; PrivateWindowmanager.layoutparams wmparams=NULL; PrivateMyfloatview myfv=NULL; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); //Create a hover windowCreateView (); } Private voidCreateView () {MYFV=NewMyfloatview (Getapplicationcontext ()); Myfv.setimageresource (r.drawable.angry_birds); //Get WindowManagerwm=(WindowManager) Getapplicationcontext (). Getsystemservice (Context.window_service); //set Layoutparams (global variables) related parametersWmparams =( (MyApplication) getapplication ()). Getmywmparams (); Wmparams.type=layoutparams.type_phone;//Set Window typewmparams.format=pixelformat.rgba_8888;//format picture, effect is transparent background//Set window Flagwmparams.flags=Layoutparams.flag_not_touch_modal|layoutparams.flag_not_focusable; Wmparams.gravity=gravity.left| Gravity.top;//Adjust the hover window to the upper left corner//set X, y initial values as the origin in the upper-left corner of the screenWmparams.x=0; Wmparams.y=0; //Setting the hover window length-width dataWmparams.width=40; Wmparams.height=40; //Show Myfloatview ImagesWm.addview (MYFV, wmparams); } @Override Public voidOnDestroy () {Super. OnDestroy (); //destroys a suspended window when program exits (activity destruction)Wm.removeview (MYFV); }}
L finally modify the program permissions on the phone during program installation-"Hover window is available
Jerry Education
Source:http://www.cnblogs.com/jerehedu/
Copyright Notice: The copyright of this article belongs to cigarettes DeskJerry EducationSection Technology Co., Ltd. and blog Park are shared, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Technical Consultation:
Android WindowManager for floating windows (i)--binding with current activity