Creates an WindowManager hover window effect over the current Activity

  • 2021-01-06 00:43:40
  • OfStack

Recently, a student did a graduation project and wanted to use the effect of floating window. In fact, it is very simple. We can use the system service WindowManager to achieve this function.

Step 1: Know WindowManager

This interface is used to interact with window manager (window manager, application framework layer).

An instance of WM is available via getSystemService(Context.WINDOW_SERVICE).

Inheritance relationships

public interface WindowManager implements ViewManager

Subordinate to the package


Important method

Add view addView ()
Delete view removeView ()
updateViewLayout () changes the parameters of view

Window Manager Service is global and is 1 only. It translates the user's operations into instructions and sends them to each Window presented on the interface. Activity will register the top-level controls in Window Manager. When the user actually touches the screen or keyboard, Window Manager will notify the user, and when the control has some requests, it will also be sent back to Window Manager via ViewParent. To complete the entire communication process

Step 2: Override onTouchEvent methods for ImageView

In the previous step, we learned that WindowManager can be added, deleted or changed. To achieve the drag effect of the floating window, we need to get the coordinate position of ImageView.

Gets the coordinates relative to the screen, with the upper-left corner of the screen as the origin

float x = event.getRawX();
float y = event.getRawY()-25; //25 Is the height of the system status bar 

Set x,y via WindowManager.LayoutParams wmParams

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

The current position of the floating window is set by the updateViewLayout() method

Step 3: Add permissions

AndroidManifest.xml Add the following permissions to AndroidManifest.xml:

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

The effect is as follows:

Important code: Create ES106en

import android.view.WindowManager;
public class MyApplication extends Application {
*  Creating global variables 
*  Note that in AndroidManifest.xml In the Application The node to add android:name=".MyApplication" attribute 
private WindowManager.LayoutParams wmParams=new WindowManager.LayoutParams();
public WindowManager.LayoutParams getMywmParams(){
return wmParams;

Create a custom View that inherits ImageView

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.ImageView;
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 Gets the global variable used to hold the properties of the hovering window 
private WindowManager.LayoutParams wmParams = ((MyApplication)getContext().getApplicationContext()).getMywmParams();
public MyFloatView(Context context) {
// TODO Auto-generated constructor stub
public boolean onTouchEvent(MotionEvent event) {
// Gets the coordinates relative to the screen, with the upper-left corner of the screen as the origin 
x = event.getRawX();
y = event.getRawY()-25; //25 Is the height of the system status bar 
Log.i("currP", "currX"+x+"====currY"+y);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// To obtain the relative View The coordinates of theta, which is this View The top left corner is the origin 
mTouchStartX = event.getX();
mTouchStartY = event.getY();
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
return true;
private void updateViewPosition(){
// Update floating window position parameters 
wmParams.x=(int)( x-mTouchStartX);
wmParams.y=(int) (y-mTouchStartY);
wm.updateViewLayout(this, wmParams);

Create Activity

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
public class MyFloatViewActivity extends Activity{
private WindowManager wm=null;
private WindowManager.LayoutParams wmParams=null;
private MyFloatView myFV=null;
public void onCreate(Bundle savedInstanceState) {
// Creating a floating window 
private void createView(){
myFV=new MyFloatView(getApplicationContext());
// To obtain WindowManager
// Set up the LayoutParams( Global variables) 
wmParams = ((MyApplication)getApplication()).getMywmParams();
wmParams.type=LayoutParams.TYPE_PHONE; // Set up the window type
wmParams.format=PixelFormat.RGBA_8888; // Set the image format to make the background transparent 
// Set up the Window flag
wmParams.gravity=Gravity.LEFT|Gravity.TOP; // Adjust the hover window to the upper left corner 
// Set with the upper left corner of the screen as the origin x , y The initial value 
// Sets the length and width data of the floating window 
// According to myFloatView image 
wm.addView(myFV, wmParams);
public void onDestroy(){
// At program exit (Activity Destroy the floating window when) 

Through the above example code to give you in detail on the current Activity to create suspended view suspended window effect WindowManager related knowledge, I hope this article is helpful to you.

Related articles: