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 Android imitates IOS system to realize suspension window effect

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

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how Android imitates the IOS system to achieve the suspension window effect, the article introduces in great detail, has a certain reference value, interested friends must read it!

The code is as follows:

Before that, we need to apply for permission in manifest:

In addition, for the permission of suspension window, we need to manually find the application rights management on the phone to allow this permission.

The interface code of the small suspension window float_normal_view.xml:

The interface code of the large suspension window float_control_view:

Entry activity (FloatActivity):

Public class FloatActivity extends Activity {MyWindowManager myWindowManager; @ Override protected void onCreate (@ Nullable Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); myWindowManager = MyWindowManager.getInstance (); myWindowManager.createNormalView (this.getApplicationContext ());}}

Floating window Manager MyWindowManager:

/ * Created by shiweixian on 2017-3-7. * suspension window manager * create and remove * singleton mode * / public class MyWindowManager {private FloatNormalView normalView; private FloatControlView controlView; private static MyWindowManager instance; private WindowManager windowManager; private MyWindowManager () {} public static MyWindowManager getInstance () {if (instance = = null) instance = new MyWindowManager (); return instance } private WindowManager getWindowManager (Context context) {if (windowManager = = null) windowManager = (WindowManager) context.getSystemService (Context.WINDOW_SERVICE); return windowManager;} / * * determine whether a small suspension window exists * * @ return * / public boolean isNormalViewExists () {return normalView! = null } / * determine whether the large suspension window of the player exists * * @ return * / public boolean isControlViewExists () {return controlView! = null;} / * create a small suspension window * / public void createNormalView (Context context) {if (normalView = = null) {normalView = new FloatNormalView (context) }} / * remove suspended window * * @ param context * / public void removeNormalView (Context context) {if (normalView! = null) {windowManager.removeView (normalView); normalView = null }} / * create a small suspension window * / public void createControlView (Context context) {if (controlView = = null) controlView = new FloatControlView (context) Remove suspended window * * @ param context * / public void removeControlView (Context context) {if (controlView! = null) {WindowManager windowManager = (WindowManager) context.getSystemService (Context.WINDOW_SERVICE); windowManager.removeView (controlView); controlView = null;}

Small suspension window FloatNormalView:

/ * Created by shiwe on 2017-3-7. * reduced suspension window * / public class FloatNormalView extends LinearLayout {/ * record the width of the small suspension window * / public static int viewWidth; / * record the height of the small suspension window * / public static int viewHeight; / * record the height of the system status bar * / private static int statusBarHeight / * used to update the position of the small suspension window * / private WindowManager windowManager; / * * the parameters of the small suspension window * / private WindowManager.LayoutParams mParams; / * record the horizontal coordinate value of the current finger position on the screen * / private float xInScreen / * record the ordinate value of the current finger position on the screen * / private float yInScreen; / * record the Abscissa value on the screen when the finger is pressed * / private float xDownInScreen; / * record the ordinate value on the screen when the finger is pressed * / private float yDownInScreen / * record the Abscissa value on the View of the small suspension window when the finger is pressed * / private float xInView; / * record the value of the ordinate on the View of the small suspension window when the finger is pressed * / private float yInView; public FloatNormalView (Context context) {super (context); windowManager = (WindowManager) context.getSystemService (Context.WINDOW_SERVICE) LayoutInflater.from (context). Evaluate (R.layout.float_normal_view, this); View view = findViewById (R.id.ll_float_normal); viewWidth = view.getLayoutParams (). Width; viewHeight = view.getLayoutParams (). Height; initLayoutParams () } / * initialization parameter * / private void initLayoutParams () {/ / screen width and height int screenWidth = windowManager.getDefaultDisplay () .getWidth (); int screenHeight = windowManager.getDefaultDisplay () .getHeight (); mParams = new WindowManager.LayoutParams (); / / always appears above the application window. MParams.type = WindowManager.LayoutParams.TYPE_PHONE; / / FLAG_NOT_TOUCH_MODAL does not block the event passed to the rear window / / FLAG_NOT_FOCUSABLE suspension window is small, the following application icon changes from non-long press to long press, if you do not set this flag, there will be problems with the screen scratching of the home page mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL / / default display position of suspension window mParams.gravity = Gravity.START | Gravity.TOP; / / specified location mParams.x = screenWidth-viewWidth * 2; mParams.y = screenHeight / 2 + viewHeight * 2; / / width and height of suspension window mParams.width = WindowManager.LayoutParams.WRAP_CONTENT; mParams.height = WindowManager.LayoutParams.WRAP_CONTENT; mParams.format = PixelFormat.TRANSPARENT WindowManager.addView (this, mParams);} @ Override public boolean onTouchEvent (MotionEvent event) {switch (event.getAction ()) {case MotionEvent.ACTION_DOWN: / / record the necessary data when the finger is pressed. Even the coordinate value needs to subtract the height of the status bar xInView = event.getX (); yInView = event.getY () XDownInScreen = event.getRawX (); yDownInScreen = event.getRawY ()-getStatusBarHeight (); xInScreen = event.getRawX (); yInScreen = event.getRawY ()-getStatusBarHeight (); break; case MotionEvent.ACTION_MOVE: xInScreen = event.getRawX () YInScreen = event.getRawY ()-getStatusBarHeight (); / / updates the position of the small suspension window updateViewPosition () when the finger moves; break; case MotionEvent.ACTION_UP: / / if the finger leaves the screen, xDownInScreen and xInScreen are equal, and yDownInScreen and yInScreen are equal, the click event is considered to be triggered. If (xDownInScreen = = xInScreen & & yDownInScreen = = yInScreen) {openOrCloseControlView ();} break; default: break;} return true;} / * * pass in the parameters of the small suspension window to update the position of the small suspension window. The parameter * / public void setParams (WindowManager.LayoutParams params) {mParams = params;} / * * of param params small suspension window updates the position of the small suspension window on the screen. * / private void updateViewPosition () {mParams.x = (int) (xInScreen-xInView); mParams.y = (int) (yInScreen-yInView); windowManager.updateViewLayout (this, mParams);} / * Open or close large suspension window. * / private void openOrCloseControlView () {MyWindowManager manager = MyWindowManager.getInstance (); if (! manager.isControlViewExists ()) manager.createControlView (getContext ()); else manager.removeControlView (getContext ());} / * is used to obtain the height of the status bar. * * @ return returns the pixel value of the height of the status bar. * / private int getStatusBarHeight () {if (statusBarHeight = = 0) {try {Class c = Class.forName ("com.android.internal.R$dimen"); Object o = c.newInstance (); Field field = c.getField ("status_bar_height"); int x = (Integer) field.get (o) StatusBarHeight = getResources () .getDimensionPixelSize (x);} catch (Exception e) {e.printStackTrace ();}} return statusBarHeight;}}

Large suspension window FloatControlView:

/ * Created by shiwe on 2017-3-7. * reduced suspension window * / public class FloatNormalView extends LinearLayout {/ * record the width of the small suspension window * / public static int viewWidth; / * record the height of the small suspension window * / public static int viewHeight; / * record the height of the system status bar * / private static int statusBarHeight / * used to update the position of the small suspension window * / private WindowManager windowManager; / * * the parameters of the small suspension window * / private WindowManager.LayoutParams mParams; / * record the horizontal coordinate value of the current finger position on the screen * / private float xInScreen / * record the ordinate value of the current finger position on the screen * / private float yInScreen; / * record the Abscissa value on the screen when the finger is pressed * / private float xDownInScreen; / * record the ordinate value on the screen when the finger is pressed * / private float yDownInScreen / * record the Abscissa value on the View of the small suspension window when the finger is pressed * / private float xInView; / * record the value of the ordinate on the View of the small suspension window when the finger is pressed * / private float yInView; public FloatNormalView (Context context) {super (context); windowManager = (WindowManager) context.getSystemService (Context.WINDOW_SERVICE) LayoutInflater.from (context). Evaluate (R.layout.float_normal_view, this); View view = findViewById (R.id.ll_float_normal); viewWidth = view.getLayoutParams (). Width; viewHeight = view.getLayoutParams (). Height; initLayoutParams () } / * initialization parameter * / private void initLayoutParams () {/ / screen width and height int screenWidth = windowManager.getDefaultDisplay () .getWidth (); int screenHeight = windowManager.getDefaultDisplay () .getHeight (); mParams = new WindowManager.LayoutParams (); / / always appears above the application window. MParams.type = WindowManager.LayoutParams.TYPE_PHONE; / / FLAG_NOT_TOUCH_MODAL does not block the event passed to the rear window / / FLAG_NOT_FOCUSABLE suspension window is small, the following application icon changes from non-long press to long press, if you do not set this flag, there will be problems with the screen scratching of the home page mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL / / default display position of suspension window mParams.gravity = Gravity.START | Gravity.TOP; / / specified location mParams.x = screenWidth-viewWidth * 2; mParams.y = screenHeight / 2 + viewHeight * 2; / / width and height of suspension window mParams.width = WindowManager.LayoutParams.WRAP_CONTENT; mParams.height = WindowManager.LayoutParams.WRAP_CONTENT; mParams.format = PixelFormat.TRANSPARENT WindowManager.addView (this, mParams);} @ Override public boolean onTouchEvent (MotionEvent event) {switch (event.getAction ()) {case MotionEvent.ACTION_DOWN: / / record the necessary data when the finger is pressed. Even the coordinate value needs to subtract the height of the status bar xInView = event.getX (); yInView = event.getY () XDownInScreen = event.getRawX (); yDownInScreen = event.getRawY ()-getStatusBarHeight (); xInScreen = event.getRawX (); yInScreen = event.getRawY ()-getStatusBarHeight (); break; case MotionEvent.ACTION_MOVE: xInScreen = event.getRawX () YInScreen = event.getRawY ()-getStatusBarHeight (); / / updates the position of the small suspension window updateViewPosition () when the finger moves; break; case MotionEvent.ACTION_UP: / / if the finger leaves the screen, xDownInScreen and xInScreen are equal, and yDownInScreen and yInScreen are equal, the click event is considered to be triggered. If (xDownInScreen = = xInScreen & & yDownInScreen = = yInScreen) {openOrCloseControlView ();} break; default: break;} return true;} / * * pass in the parameters of the small suspension window to update the position of the small suspension window. The parameter * / public void setParams (WindowManager.LayoutParams params) {mParams = params;} / * * of param params small suspension window updates the position of the small suspension window on the screen. * / private void updateViewPosition () {mParams.x = (int) (xInScreen-xInView); mParams.y = (int) (yInScreen-yInView); windowManager.updateViewLayout (this, mParams);} / * Open or close large suspension window. * / private void openOrCloseControlView () {MyWindowManager manager = MyWindowManager.getInstance (); if (! manager.isControlViewExists ()) manager.createControlView (getContext ()); else manager.removeControlView (getContext ());} / * is used to obtain the height of the status bar. * * @ return returns the pixel value of the height of the status bar. * / private int getStatusBarHeight () {if (statusBarHeight = = 0) {try {Class c = Class.forName ("com.android.internal.R$dimen"); Object o = c.newInstance (); Field field = c.getField ("status_bar_height"); int x = (Integer) field.get (o) StatusBarHeight = getResources (). GetDimensionPixelSize (x);} catch (Exception e) {e.printStackTrace ();}} return statusBarHeight;}} these are all the contents of the article "how Android imitates the IOS system to achieve suspended window effect". Thank you for reading! Hope to share the content to help you, more related knowledge, 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