1. Add permissions
First, make sure your app has permission to display the floating window. existAdd the following permissions to the file:
<uses-permission android:name=".SYSTEM_ALERT_WINDOW"/>
2. Request floating window permissions
In Android 6.0 (API level 23) and later, you need to request floating window permissions at runtime:
import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends AppCompatActivity { private static final int REQUEST_CODE_OVERLAY_PERMISSION = 1001; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { (savedInstanceState); if (.SDK_INT >= Build.VERSION_CODES.M) { if (!(this)) { Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, ("package:" + getPackageName())); startActivityForResult(intent, REQUEST_CODE_OVERLAY_PERMISSION); } else { // Permission has been obtained, displaying floating window showFloatingWindow(); } } else { // Below Android 6.0, directly display floating window showFloatingWindow(); } } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { (requestCode, resultCode, data); if (requestCode == REQUEST_CODE_OVERLAY_PERMISSION) { if (.SDK_INT >= Build.VERSION_CODES.M) { if ((this)) { // Permission has been obtained, displaying floating window showFloatingWindow(); } } } } private void showFloatingWindow() { // Code to display floating window } }
3. Create floating windows and achieve edge-absorbing effect
Here is a complete example of how to create a floating window and achieve edge-absorbing effect:
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends AppCompatActivity { private WindowManager windowManager; private layoutParams; private ImageView floatingView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { (savedInstanceState); if (.SDK_INT >= Build.VERSION_CODES.M) { if (!(this)) { Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, ("package:" + getPackageName())); startActivityForResult(intent, 1001); } else { showFloatingWindow(); } } else { showFloatingWindow(); } } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { (requestCode, resultCode, data); if (requestCode == 1001) { if (.SDK_INT >= Build.VERSION_CODES.M) { if ((this)) { showFloatingWindow(); } } } } private void showFloatingWindow() { windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); floatingView = new ImageView(this); (.ic_launcher_foreground); // Replace with your icon layoutParams = new ( .WRAP_CONTENT, .WRAP_CONTENT, .SDK_INT >= Build.VERSION_CODES.O ? .TYPE_APPLICATION_OVERLAY : .TYPE_PHONE, .FLAG_NOT_FOCUSABLE, ); = | ; = 0; = 100; (floatingView, layoutParams); (new () { private int initialX; private int initialY; private float initialTouchX; private float initialTouchY; @Override public boolean onTouch(View v, MotionEvent event) { switch (()) { case MotionEvent.ACTION_DOWN: initialX = ; initialY = ; initialTouchX = (); initialTouchY = (); return true; case MotionEvent.ACTION_MOVE: = initialX + (int) (() - initialTouchX); = initialY + (int) (() - initialTouchY); (floatingView, layoutParams); return true; case MotionEvent.ACTION_UP: int screenWidth = getResources().getDisplayMetrics().widthPixels; if ( < screenWidth / 2) { = 0; } else { = screenWidth; } (floatingView, layoutParams); return true; } return false; } }); } @Override protected void onDestroy() { (); if (floatingView != null) { (floatingView); } } }
explain
-
Request floating window permissions:exist
onCreate
Check and request floating window permissions in the method. -
Create a floating window:use
WindowManager
Create a floating window and set。
-
Achieving edge-absorbing effect:pass
setOnTouchListener
Method to monitor the touch event of the floating window, inACTION_UP
Achieving edge-absorbing effect during events.
Things to note
- Permission Management: Ensure that floating window permissions are requested correctly in Android 6.0 and later.
-
Floating window type: In Android 8.0 and later, use
.TYPE_APPLICATION_OVERLAY
type. -
Resource Management:exist
onDestroy
The floating window view is removed in the method to avoid memory leakage.
This is the article about implementing floating windows and adding edge-sucking effects in Android. For more related content on Android to implement floating windows and adding edge-sucking content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!