Android imitation 360 desktop mobile guard floating window effect

Hello, everyone. Today I will bring you a tutorial imitating the effect of 360 mobile guard floating window. Before starting, please allow me to say a few irrelevant words.

Unknowingly, I found that I have been in contact with Android for nearly three years. During this period, all kinds of growth is inseparable from the help of all experts. There are always many experts who like to write their experience online for everyone to learn. I have also benefited a lot from it. I am deeply grateful here. But I found that I never shared some of my usual experiences with you and learned together. It's too devoid of dedication. So I learned from the pain and decided to start blogging today, hoping to guide the developers behind me to enter the ranks of Android developers faster.

Well, that's all the nonsense. Let's start today's topic.

360 mobile guard, I believe we all know that many people will install this software on their mobile phones, so we must be familiar with its desktop floating window effect. See the figure below:

First, a small floating window shows how much memory is currently used. Click the small floating window and a large floating window will pop up, which can be accelerated with one click. OK, let's simulate and achieve a similar effect now.

Let's talk about the basic implementation principle first. The effect of this desktop floating window is very similar to that of a widget, but it is much more flexible than a widget. It is mainly implemented through the WindowManager class. The addview method of this class is called to add a floating window, the updateviewlayout method is used to update the parameters of the floating window, and the removeview is used to remove the floating window. The parameters of the suspended window need to be described in detail.

Windowmanager.layoutparams is used to provide the parameters required by the suspended window, including several frequently used variables:

The type value is used to determine the type of floating window, which is generally set to 2002, which means that it is above all applications, but below the status bar.

The flags value is used to determine the behavior of the floating window, such as non focusing, non modal dialog boxes, etc. there are many properties, and you can view the document.

The gravity value is used to determine the alignment of the suspended window. It is generally set to align the upper left corner, so that it is convenient to calculate the coordinates when dragging the suspended window.

The x value is used to determine the position of the suspension window. If you want to move the suspension window laterally, you need to change this value.

The y value is used to determine the position of the suspension window. If you want to move the suspension window longitudinally, you need to change this value.

The width value specifies the width of the suspended window.

The height value specifies the height of the suspended window.

To create a suspended window, you need to apply for permission from the user, so you also need to add it in androidmanifest.xml

<uses-permission android:name="android.permission.SYstem_ALERT_WINDOW" />

After introducing the principle, let's start to implement it in code. First, create an Android project in eclipse. The project name is 360floatwindowdemo. Then write the layout file. The layout file is very simple. There is only one button to open or create a new activity_ main.xml,

Add the following code:

Then create a new one named float_ window_ The layout file of small.xml is used as the layout of small suspended windows,

Add the following code:

Create a new one named float_ window_ The layout file of big.xml is used as the layout of large suspended windows,

Add the following code:

For the picture resources used in the two suspended window layout files, you can find some pictures to replace them. At the same time, I will give the source code, and you can also take them out of the source code.

Then open or create mainactivity, which is the main interface of the project

Add the following code:

It can be seen here that the code of mainactivity is not simple, that is, it registers a click event for the button to open the floating window, which is used to open a service and then close the current activity. The logic of creating a floating window is left to the service. OK, now let's create this service. Create a new class named floatwindowservice, which inherits from service

Add the following code:

A timer is enabled in the onstartcommand method of floatwindowservice, and refreshtask will be executed every 500 milliseconds. In refreshtask, it is necessary to judge that if the mobile phone is currently on the desktop, the floating window should be displayed. If the mobile phone opens an application, the floating window should be removed. If the mobile phone is on the desktop, the memory usage percentage data should also be updated. When the floatwindowservice is destroyed, the timer should be stopped, otherwise it will run all the time.

From the above code, we can also see that the creation and removal of the suspended window and the updating of the data in the suspended window are managed by the mywindowmanager class. It is much better to use a special tool class to manage these codes than to write them directly in the activity or service. However, to create a floating window, you must first write out the view of the floating window.

Create a new class called floatwindowsmallview, which inherits from LinearLayout. Create a new class called floatwindowbigview, which also inherits from LinearLayout.

Add the following code to floatwindowsmallview:

Among them, the ontouchevent event of this view is rewritten to realize the effect of dragging and clicking. If it is found that the user triggered the action_ The down event will record the coordinates and other data when pressed. If it is found that the user triggered the action_ Move event, the position of the suspended window in the screen is updated according to the coordinates of the current movement. If it is found that the user triggered the action_ Up event, and action_ If the coordinates recorded in down are found to be the same, it is deemed that the user has clicked on the suspended window. Click the small suspension window to open the large suspension window, and then we will realize the view of the large suspension window.

Add the following code to floatwindowbigview:

Compared with floatwindowsmallview, floatwindowbigview is much simpler. There are only two buttons. Click the close button to remove all the suspended windows and terminate the service. Click the back button to remove the large suspended window and recreate the small suspended window.

Now that the views of the two floating windows have been written, let's create mywindowmanager,

The code is as follows:

This class is responsible for controlling the creation and removal of large suspension windows and small suspension windows, and calculating the percentage of system memory usage.

Here, basically all the code has been written. Then let's take a look at the androidmanifest.xml file,

The code inside is as follows:

It's relatively simple. Remember to register the activity and service in it, and there is also an android.permission.system that needs to be added to the permission statement_ ALERT_ Windows means that user authorization is required to allow the creation of system prompt window, that is, our desktop floating window.

OK, now let's run the project. The effect is shown in the figure below. There is only one simple button in the main interface. After clicking the button, the activity will be closed and the small floating window will be displayed on the desktop. It shows the percentage of current memory usage.

@H_ 301_ 178@

The small suspension window can be dragged freely. If other applications are opened, the small suspension window will be hidden automatically. When you return to the desktop, the small suspension window will be displayed again.

If you click the small suspension window, the large suspension window will pop up. Here, the large suspension window is relatively simple, with only two buttons. When the large floating window is displayed, all other programs of the mobile phone cannot be clicked, because the focus is on the floating window. Click the back button to redisplay the small suspension window. Click the close suspension window button, and the service will stop together.

We won't do the one click acceleration function of 360 mobile guard. Just like Dugu Jiujian, the important thing is the sword meaning rather than the sword move. I believe you can make something more creative than 360 after learning the basic principle of creating a suspension window.

If you have any questions, please leave a message below.

Friends interested in the desktop suspension window can continue to read the advanced Android desktop suspension window, and the QQ mobile housekeeper small rocket effect is realized.

Please click here to download the source code

Add: a friend told me that the above code will crash when running on systems above Android 3.0. I looked at it and it is true. The main reason is that you need to add a permission statement to get running tasks after 3.0. Add in androidmanifest.xml

<uses-permission android:name="android.permission.GET_TASKS" />

This problem can be solved.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>