SoFunction
Updated on 2025-03-04

Create a WindowManager floating window effect of the floating view on the current activity

Recently, a student is doing graduation design and wants to use the effect of floating windows. It is actually very simple. We can implement this function through the system service WindowManager. In this chapter, we will experiment to create a suspended view on the current activity.

Step 1: Understand WindowManager

This interface is used to interact with window manager (Window Manager, Application Framework Layer).

An instance of WM can be obtained through getSystemService(Context.WINDOW_SERVICE).

Inheritance relationship

public interface WindowManager implements ViewManager

Package


Important Methods

addView() Add view
removeView() delete view
updateViewLayout () Change the view parameters

Window Manager Service is global and unique. It translates the user's operations into instructions and sends them to various Windows presented on the interface. Activity will register top-level controls in Window Manager. When the user really touches the screen or keyboard, Window Manager will notify you. When some requests are generated by the control, it will also be sent back to Window Manager through ViewParent. This completes the entire communication process

Step 2: Rewrite the onTouchEvent method of ImageView

In the previous step, we know that WindowManager can add, delete, and change the view. If we want to achieve the dragging effect of the floating window, we need to obtain the coordinate position of the ImageView.

Get the coordinates of the relative screen, that is, use the upper left corner of the screen as the origin

float x = ();
float y = ()-25; //25It is the height of the system status bar

Set x,y via wmParams

=(int)( x-mTouchStartX);
=(int) (y-mTouchStartY);

Then set the current position of the floating window through updateViewLayout() method

Step 3: Add permissions

Add the following permissions to:

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

The effects are as follows:

Important code: Create MyApplication

import ;
import ;
public class MyApplication extends Application {
/**
 * Create global variables
 * Note that the android:name=".MyApplication" attribute is added to the Application node in
 *
 */
private  wmParams=new ();
public  getMywmParams(){
return wmParams;
}
}

Create a custom view Inherit ImageView

import ;
import ;
import ;
import ;
import ;
public class MyFloatView extends ImageView {
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
private WindowManager wm=(WindowManager)getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
//This wmParams is the global variable obtained, which is used to save the properties of the suspended windowprivate  wmParams = ((MyApplication)getContext().getApplicationContext()).getMywmParams();
public MyFloatView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//Get the coordinates of the relative screen, that is, use the upper left corner of the screen as the origin pointx = ();
y = ()-25; //25 is the height of the system status bar("currP", "currX"+x+"====currY"+y);
switch (()) {
case MotionEvent.ACTION_DOWN:
//Get the coordinates of the relative View, that is, use the upper left corner of this View as the origin pointmTouchStartX = ();
mTouchStartY = ();
break;
case MotionEvent.ACTION_MOVE:
updateViewPosition();
break;
case MotionEvent.ACTION_UP:
updateViewPosition();
mTouchStartX=mTouchStartY=0;
break;
}
return true;
}
private void updateViewPosition(){
//Update the floating window position parameters=(int)( x-mTouchStartX);
=(int) (y-mTouchStartY);
(this, wmParams);
}
}

Create an Activity

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MyFloatViewActivity extends Activity{
private WindowManager wm=null;
private  wmParams=null;
private MyFloatView myFV=null;
@Override
public void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
//Create a floating windowcreateView();
}
private void createView(){
myFV=new MyFloatView(getApplicationContext());
(.angry_birds);
//Get WindowManagerwm=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
//Set LayoutParams (global variable) related parameterswmParams = ((MyApplication)getApplication()).getMywmParams();
=LayoutParams.TYPE_PHONE; //Set window type=PixelFormat.RGBA_8888; //Set the image format, the effect is transparent background//Set Window flag=LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
=|; //Adjust the floating window to the upper left corner//Set the initial values ​​of x and y with the upper left corner of the screen as the origin point.=0;
=0;
//Set the length and width data of the floating window=40;
=40;
//Show myFloatView image(myFV, wmParams);
}
@Override
public void onDestroy(){
();
//Destroy the floating window when the program exits (Activity destruction)(myFV);
}
}

Through the above example code, we will explain the relevant knowledge about creating a WindowManager floating window effect of a floating view on the current Activity. I hope that the description in this article will be helpful to everyone.