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
android.view.WindowManager
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.app.Application;
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) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
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();
break;
case MotionEvent.ACTION_MOVE:
updateViewPosition();
break;
case MotionEvent.ACTION_UP:
updateViewPosition();
mTouchStartX=mTouchStartY=0;
break;
}
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.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Creating a floating window
createView();
}
private void createView(){
myFV=new MyFloatView(getApplicationContext());
myFV.setImageResource(R.drawable.angry_birds);
// To obtain WindowManager
wm=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
// 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.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
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
wmParams.x=0;
wmParams.y=0;
// Sets the length and width data of the floating window
wmParams.width=40;
wmParams.height=40;
// According to myFloatView image
wm.addView(myFV, wmParams);
}
@Override
public void onDestroy(){
super.onDestroy();
// At program exit (Activity Destroy the floating window when)
wm.removeView(myFV);
}
}
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.