Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992
In the previous article, we introduced how to implement the desktop hover window, in the implementation of this effect, the most important class is WindowManager, today this article, will introduce the use of WindowManager, and implement a use WindowManager to enable the user to open the app, showing the first use of the teaching mask effect.
The WindowManager class implements the Viewmanager interface, and theViewmanager interface allows us to add or remove view from the activity, so WindowManager also allows us to add and remove view actions on the activity.
We can get a WindowManager object by the following method
Context.getsystemservice (Context.window_service)
in the activity, we can go directly through Getwindowmanager () gets to a WindowManager object.
Each WindowManager instance is bound to a unique display object, and if we want to get a different display WindowManager object, we can go through the Createdisplaycontext (display) Gets to the context object of this display, and then uses the method above, and can also get to a WindowManager object.
When we use the WindowManager class, we usually use the following methods:
WindowManager. AddView (View,windowmanager.layoutparam);
WindowManager. Removeview ();
WindowManager. Getdefaultdisplay ();
WindowManager . AddView () method is used to add a view object to the current window, you need to accept two parameters, view is the View object to add to the window, and Windowmanager.layoutparam is the parameter of the added window, in the previous action to add a suspended window, you need to Layoutparam set a lot of parameters, let's look at the common settings
Set the Layoutparams parameter layoutparams params = new Windowmanager.layoutparams ();//Set the display type, type_phone refers to the time when the call will be overwritten, Other times will be at the front end, show location under Statebar, other more values please consult document Params.type = windowmanager.layoutparams.type_phone;//setting display format Params.format = pixelformat.rgba_8888;//Set Alignment params.gravity = Gravity.left | gravity.top;//Set Width Height params.width = screenutils.getscreenwidth (this);p arams.height = Screenutils.getscreenheight (This );//Set the display position params.x;params.y;
After setting the Layoutparam, we canWindowManager.AddView(View,windowmanager.layoutparam) to add a View to the window, but we need to declare the permissions
< uses-permission android:name="Android.permission.SYSTEM_ALERT_WINDOW"/>
< Span class= "S1" style= "font-size:11.8181819915771px" > is added, we can see the view object we added on the window. If we want to remove the added view, we only need to call WindowManager removeview (), parameter is the View object we used earlier, the use is very simple. In addition to this method, there is a windowmanager. removeviewimmediate (), you can also remove the view, but the document says that this method is not intended to be called by the general program, so we need to use it carefully, we have developed a general program, we recommend that you do not use this method.
In addition to these two methods, one of our most commonly used methods is WindowManager. Getdefaultdisplay (), in this way, we can get an object to the display of the current interface, Then we can get some parameters to the current screen, such as the width of the height.
Here is a tool class that I often use.
Package Com.qust.teachmask;import android.content.context;import android.view.windowmanager;/** * Screen Help class * * @ Author Zhaokaiqiang * */public class Screenutils {/** * get screen width * * @return */@SuppressWarnings ("deprecation") public static int Getscreenwidth (context context) {return (WindowManager) Context.getsystemservice (context.window_ SERVICE). Getdefaultdisplay (). GetWidth ();} /** * Get Screen width * * @return */@SuppressWarnings ("deprecation") public static int Getscreenheight (context context) { Return ((WindowManager) Context.getsystemservice (Context.window_service)). Getdefaultdisplay (). GetHeight ();}}
Knowing the above, we can achieve the teaching template effect, first look.
Here is the code implementation
< Span class= "S1" style= "font-size:11.8181819915771px" >
Package Com.qust.teachmask;import Android.app.activity;import Android.graphics.pixelformat;import Android.os.bundle;import Android.view.gravity;import Android.view.view;import Android.view.View.OnClickListener; Import Android.view.windowmanager;import Android.view.windowmanager.layoutparams;import Android.widget.ImageView; Import Android.widget.imageview.scaletype;public class Mainactivity extends Activity {private ImageView img;private WindowManager WindowManager; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); WindowManager = Getwindowmanager ();//dynamic initialization layer IMG = new ImageView (this); Img.setlayoutparams (New Layoutparams (Android.view.ViewGroup.LayoutParams.MATCH_PARENT, Android.view.ViewGroup.LayoutParams.MATCH_PARENT)); Img.setscaletype (SCALETYPE.FIT_XY); Img.setimageresource ( R.drawable.guide);//Set layoutparams parameter layoutparams params = new Windowmanager.layoutparams ();//Set the type of display, Type_ Phone refers to the time of the call will be overwritten, other times in the mostFront end, display location under Statebar, other more values please refer to document Params.type = windowmanager.layoutparams.type_phone;//setting display format Params.format = pixelformat.rgba_8888;//Set Alignment params.gravity = Gravity.left | gravity.top;//Set Width Height params.width = screenutils.getscreenwidth (this);p arams.height = Screenutils.getscreenheight (This );//Add to the current Window Windowmanager.addview (IMG, params);//After clicking on the layer, remove the layer Img.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View arg0) {Windowmanager.removeview (IMG);}});}}
GitHub Project Address: Https://github.com/ZhaoKaiQiang/TeachMask
"Anroid Interface Implementation" WindowManager class use--the user first open the app using the teaching mask effect implementation