Android floating window

Source: Internet
Author: User

Android floating window

The iPhone has a very good white dot. Today I will study the floating box in Android. Here I mainly implement a shortcut key function. Of course, I can also do what I want in the floating box!

The implementation of floating window is mainly implemented through WindowManager. Of course, WindowManager is only an interface. If you want to know the source code, you can check WindowManagerImpl. The floating window is implemented through addView, updateView, and removeView in WindowManager.
WindowManager. LayoutParams is used to provide parameters required for floating windows.
WindowManager. LayoutParams parameter description:

Type is used to determine the type of the floating window (window type, window has three types, application window, sub window, System window, where the floating window uses the system window), generally uses TYPE_PHONE, it indicates that many types are provided on top of all applications and under the status bar, of course, TYPE_STATUS_BAR (status bar) TYPE_SEARCH_BAR (search box) TYPE_SYSTEM_ALERT (system prompt box, such as prompt box when the power is low, there are many. Select flags as needed to determine the behavior of the floating window. FLAG_NOT_FOCUSABLE (the window does not need to obtain the focus or receive various input events) FLAG_NOT_TOUCHABLE (do not click) FLAG_NOT_TOUCH_MODAL (the system will pass the click event outside the current window area to the underlying window, And the click event within the current window area will be handled by itself) FLAG_SHOW_WHEN_LOCKED (displayed on the screen lock) gravity is used to determine the alignment mode of the floating window. x is used to determine the y coordinate of the floating window. The width value is used to determine the width and height of the floating window.

After learning about these parameters, you can create a floating box.

private void createFloatView() {        mWindowManager = (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);        wmParams = new WindowManager.LayoutParams();        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;        wmParams.gravity = Gravity.LEFT | Gravity.TOP;        wmParams.x = 100;        wmParams.y = 100;        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;        LayoutInflater inflater = LayoutInflater.from(getApplication());        mFloatLayout = (LinearLayout) inflater.inflate(R.layout.layout_float_window, null);        mWindowManager.addView(mFloatLayout, wmParams);        mFloatView = (ImageView) mFloatLayout.findViewById(R.id.img_float_window);        mFloatView.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                wmParams.x = (int) event.getRawX() - mFloatView.getMeasuredWidth() / 2;                wmParams.y = (int) event.getRawY() - mFloatView.getMeasuredHeight() / 2 - 25;                mWindowManager.updateViewLayout(mFloatLayout, wmParams);                return false;            }        });        mFloatView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(FloatWindowService.this, SearchActivity.class);                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                getApplication().startActivity(intent);            }        });    }
if (mFloatLayout != null) {            mWindowManager.removeView(mFloatLayout);        }

The above is the process of floating box, of course, you must apply for permissions to use the system window!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.