Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the floating window button in Android

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how to achieve the floating window button in Android, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to achieve the floating window button in Android. Let's take a look at it.

The first is the page layout:

The following step-by-step introduction to the creation of this suspension window.

1 display of suspended windows

/ / create WindowManager object private WindowManager windowManager;windowManager = (WindowManager) context.getSystemService (Context.WINDOW_SERVICE); / / create LayoutParams private void initLayoutParams () {try {DisplayMetrics metrics = new DisplayMetrics (); windowManager.getDefaultDisplay (). GetMetrics (metrics) of the suspension window; screenWidth = metrics.widthPixels; screenHeight = metrics.heightPixels; lp = new WindowManager.LayoutParams (); if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.O) {lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY } else {lp.type = WindowManager.LayoutParams.TYPE_TOAST;} lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; lp.gravity = Gravity.START | Gravity.TOP; lp.x = screenWidth-view.getLayoutParams (). Width * 2; lp.y = 0; lp.width = WindowManager.LayoutParams.WRAP_CONTENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; lp.format = PixelFormat.TRANSPARENT } catch (Exception e) {}}

The above creates the WindowManager that controls the display of the suspension window and the LayoutParams that controls the layout of the suspension window.

Then use the following code to display the floating window:

Public void show () {if (! isShowing) {isShowing = true; windowManager.addView (this, lp);}}

It is also easy to remove the floating window, as follows:

Public void dismiss () {if (isShowing) {isShowing = false; windowManager.removeView (this);}}

2 touch event

The touch event can make the suspended window follow the finger.

/ / Interface FloatLayoutBinding layoutBinding = DataBindingUtil.inflate (LayoutInflater.from (context), R.layout.floatbelt layout.ThisMagnegy false); FloatNormalViewModel floatNormalViewModel = new FloatNormalViewModel (context,layoutBinding,onClickCallback); layoutBinding.setViewModel (floatNormalViewModel); addView (layoutBinding.getRoot ()); view = layoutBinding.root;isShowControlView = layoutBinding.floatId;// this is the control button / / control variables private float downX, downY;private float moveX, moveY / / Touch event isShowControlView.setOnTouchListener (new OnTouchListener () {@ Override public boolean onTouch (View view, MotionEvent motionEvent) {switch (motionEvent.getActionMasked ()) {case MotionEvent.ACTION_DOWN: downX = motionEvent.getRawX (); downY = motionEvent.getRawY (); break; case MotionEvent.ACTION_MOVE: moveX = motionEvent.getRawX ()-downX; moveY = motionEvent.getRawY ()-downY; downX + = moveX; downY + = moveY; updateViewPosition () Break;} return false;}}); private void updateViewPosition () {lp.x + = (int) (moveX); lp.y + = (int) (moveY); windowManager.updateViewLayout (this, lp);}

3 Click event

The click event implements a callback function, because the logic of the click event should not be completed here, but should be controlled by the main layout, so a click interface is defined.

The order of handling events here is as follows: after clicking the button, the button handles the click event through the callback function, which is provided by the Activity, Fragment, Service, etc. that created the View, so the event handling is handed over to the outside.

/ / the interface public interface OnClickCallback {public void onClick (View view);} / the control button clicks the event public void onControlClick (View view) {if (onClickCallback! = null) onClickCallback.onClick (view);}

Multi-function suspension window

The multi-function suspension window is similar to the above, except that there are more click events.

And how to complete the switch between the two suspension windows, you can use the previously used OnClickCallback callback interface to display one and hide the other, and if the two suspension windows use the same LayoutParams, they can be displayed in the same position.

Private void init () {floatNormalView = new FloatNormalView (context, new OnClickCallback () {@ Override public void onClick (View view) {floatControlView.setLayoutParams (floatNormalView.getLayoutParams ()); floatControlView.show (); floatNormalView.dismiss ();}}); floatControlView = new FloatControlView (context, new OnClickCallback () {@ Override public void onClick (View view) {floatNormalView.setLayoutParams (floatControlView.getLayoutParams ()); floatNormalView.show (); floatControlView.dismiss () }, new FloatControlViewModel.OnVisibleChangeListener () {@ Override public void onChange (boolean isVisible) {if (isControlVisible) {floatControlView.show (); floatNormalView.dismiss ();} else {floatControlView.dismiss (); floatNormalView.show ();}); floatNormalView.show ();} about how to implement the floating window button in Android. Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to realize the floating window button in Android". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report