From: http://blog.csdn.net/guolin_blog/article/details/8689140
Hello, everybody. Today I'm going to give you a tutorial that imitates the effect of 360 mobile phone guard suspension window. Before we start, please allow me to say a few irrelevant nonsense.
Unconsciously I found myself in contact. Android It has been nearly three years. During this period, all kinds of growth can not be achieved without the help of your masters. There are always many masters who like to write their own experience on the Internet for you to learn. I have also benefited a lot from it. Here I am deeply grateful. But I find that I have never shared some of my usual experience with you, learning together, too devoted. So I decided to start blogging today, hoping to point out the developers behind me and get into the Android developer ranks more quickly.
Okay, so much nonsense, let's start with today's topic.
360 Mobile Guardian I believe you all know that many mobile phones will be equipped with this software, then we will surely be familiar with the effect of a desktop suspension window. See the following picture:
First, a small suspension window shows how much memory is currently used. Clicking on a small suspension window will pop up a large suspension window, which can be accelerated by one click. Well, let's now simulate a similar effect.
Let's start with the basic implementation principle. This desktop suspension window works much like Widget, but it's much more flexible than Widget. Mainly through the Windows Manager class, call the addView method of this class to add a suspension window, updateViewLayout method to update the parameters of the suspension window, removeView to remove the suspension window. The parameters of the suspension window need to be explained in detail.
The Windows Manager.LayoutParams class is used to provide the parameters needed for the suspension window. There are several variables that are often used:
The type value is used to determine the type of suspension window, usually set to 2002, which is above all applications, but below the status bar.
flags values are used to determine the behavior of suspended windows, such as unfocusable, non-modal dialog boxes, etc. There are many attributes, you can view documents.
gravity value is used to determine the alignment mode of the suspension window, which is generally set to the upper left corner alignment, so that coordinates can be easily calculated when the suspension window is dragged.
The x value is used to determine the position of the suspension window. If you want to move the suspension window horizontally, you need to change this value.
The y value is used to determine the position of the suspension window, which needs to be changed if the suspension window is to be moved vertically.
The width value is used to specify the width of the suspension window.
The height value is used to specify the height of the suspension window.
Creating a suspended window is a form that requires permission from the user, so you need to add <uses-permission and roid: name="android.permission.SYSTEM_ALERT_WINDOW"/> in Android Manifest.xml.
This is the end of the introduction. Let's start with code. First, create a new Android project in Eclipse called 360Float Windows Demo. Then write the layout file. The layout file is very simple. There is only one button. Open or create activity_main.xml. Add the following code:
Then a new layout file named float_window_small.xml is created for the layout of the small suspension window, in which the following code is added:
A new layout file named float_window_big.xml is created for the layout of the large suspension window. The following code is added to it:
Two floating window layout files used in the image resources, you can easily find some pictures to replace, at the same time I will give the source code, you can also extract from the source code.
Then open or create MainActivity, which is the main interface of the project, and add the following code in it:
As you can see here, MainActivity's code is not simple, it registers a click event for the button that opens the suspension window to open a service and then closes the current Activity. The logic for creating suspended windows is left to the service. Okay, now let's create this service. Create a new class named Float Windows Service, which inherits from Service and adds the following code:
In Float Windows Service's onStartCommand method, a timer is turned on and RefreshTask is executed every 500 milliseconds. In RefreshTask, if the mobile phone is currently on the desktop, it should display the suspension window. If the mobile phone opens an application, it should remove the suspension window. If the mobile phone is on the desktop, it should update the data of the percentage of memory usage. When the Float Windows Service is destroyed, the timer should be stopped, otherwise it will still be running.
From the above code, we can also see that creating and removing suspension windows and updating data in suspension windows are managed by MyWindows Manager class, which is much better than writing these codes directly in Activity or Service, using a special tool class to manage them. However, to create a suspended window, you need to write out the View of the suspended window first.
Create a new class called Float Windows SmallView, which inherits from LinearLayout. A new class called Float Windows BigView is also inherited from LinearLayout.
Add the following code in Float Windows SmallView:
Among them, the onTouchEvent event of the View is rewritten to achieve drag and click effects. If the ACTION_DOWN event is found to have been triggered by the user, data such as coordinates when pressed will be recorded. If it is found that the user triggered the ACTION_MOVE event, the position of the suspension window in the screen is updated according to the currently moving coordinates. If the ACTION_UP event is triggered by the user, it will be compared with the coordinates recorded in ACTION_DOWN. If it is found to be the same, it will be considered that the user clicked on the suspension window. Click on the small suspension window to open the large suspension window, and then we can realize the view of the large suspension window.
Add the following code in Float Windows BigView:
Compared with Float Windows SmallView, Float Windows BigView is much simpler. There are only two buttons, click the close button, remove all the suspended windows and terminate the Service. Clicking the back button removes the large suspension window and recreates the small suspension window.
Now that both views of the suspension windows have been written, let's create MyWindows Manager. The code is as follows:
This class is responsible for controlling the creation and removal of large suspension windows, small suspension windows, and the calculation of the percentage of system memory usage.
At this point, almost all the code has been written. Then let's take a look at the AndroidManifest.xml file, which contains the following code:
Simple, remember to register Activity and Service in it. There is also a permission declaration to add android.permission.SYSTEM_ALERT_WINDOW, indicating that user authorization is required to allow the creation of system prompt window, that is, our desktop suspension window.
Now let's run the project. The effect is as follows. There is only one simple button in the main interface. After clicking the button, Activity is closed and the small suspension window is displayed on the desktop. It shows the percentage of current memory usage.
The small suspension window can be dragged freely. If other applications are opened, the small suspension window will be hidden automatically, and the small suspension window will appear again after returning to the desktop.
If you click on the small suspension window, it will pop up the large suspension window. Here, the large suspension window is easy to do. There are only two buttons. All other applications of the phone are not available when the large suspension window is displayed, because the focus is on the suspension window. Clicking the return button will reappear the small suspension window, clicking the close suspension window button, and the Service will stop together.
The 360 mobile phone guard's one-click acceleration function will not be done, just like the lonely nine swords, the important thing is swordsmanship rather than swordsmanship. I believe that after you learn the basic principles of creating suspension windows, you can make something more creative than 360.
If you have any questions, please leave a message below.
Friends interested in desktop suspension windows can continue reading. Android Desktop Suspension Window Advancement, QQ Mobile Housekeeping Rocket Effect Realization .
Download the source code, please click here