Android 懸浮窗

來源:互聯網
上載者:User

標籤:

iPhone有個很好用的白色圓點,今天就來研究下Android中的懸浮框,這裡主要是實現一個快速鍵的功能,當然也可以在懸浮框中做想做的事!

懸浮窗的實現主要是通過WindowManager實現,當然WindowManager只是一個介面,想瞭解源碼的同志們可以去看WindowManagerImpl,懸浮框主要是通過WindowManager中的addView,updateView,removeView實現
WindowManager.LayoutParams這個類用於提供懸浮窗所需的參數
WindowManager.LayoutParams參數說明:

  • type 用於確定懸浮窗的類型(window類型,window有三種類型,應用window,子window,系統window,其中懸浮窗中使用的是系統window),一般使用TYPE_PHONE,表示在所有應用程式之上,狀態列之下,當然這裡還提供了很多類型,TYPE_STATUS_BAR(狀態列)TYPE_SEARCH_BAR(搜尋方塊)TYPE_SYSTEM_ALERT(系統提示框,例如電量很低時提示)等等,有很多,根據需求去選擇
  • flags 用於確定懸浮窗的行為,FLAG_NOT_FOCUSABLE(window不需要獲得焦點,也不需要接收各種輸入事件)FLAG_NOT_TOUCHABLE(不可點擊)FLAG_NOT_TOUCH_MODAL(系統會通過當前window地區以外的單擊事件傳遞給底層的window,當前window地區以內的單擊事件則自己處理)FLAG_SHOW_WHEN_LOCKED(顯示在鎖屏的介面上)等等
  • gravity 用於確定懸浮窗的對齊
  • x 用於確定懸浮窗的橫座標
  • y 用於確定懸浮窗的縱座標
  • width 值用於指定懸浮窗的寬度
  • height 值用於指定懸浮窗的高度

瞭解這些參數基本就能建立一個懸浮框了

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);        }

以上就是懸浮框的過程,當然使用系統window必須要申請許可權:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

通過上面這個過程知道了如何通過WindowManager建立懸浮框

下面給你一個Demo,其中有兩個懸浮窗,第一個懸浮窗就是普通的懸浮窗,第二個懸浮窗加了一些判斷,可以直接在app中使用,這個懸浮窗在案頭和你自己的app中顯示

地址:https://github.com/zimoguo/FloatWindow

Android 懸浮窗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.