Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 全螢幕懸浮按鈕實作:用 Service + WindowManager 打造可拖動浮動視窗

編輯:關於 Android 程式設計

一開始我以為懸浮視窗可以用 Android 的 PopupWindow 來實現,雖然確實做得出來,但局限性非常大。比如 PopupWindow 必須要有載體 View,也就是說必須指定在哪個 View 之上、以它為相對位置來顯示,這就局限了它只能在使用者互動的視窗上相對顯示,而無法自由拖動位置或浮動在桌面。

於是查了一些資料,整理出兩種思路:一種是自訂 Toast,Toast 執行於所有介面之上,沒有介面能覆蓋它;另一種是使用 WindowManager 相關機制,透過 getApplication().getSystemService(Context.WINDOW_SERVICE) 取得真正的視窗管理器(注意:若用 activity.getSystemService 得到的只是屬於該 Activity 的 LocalWindowManager)。本範例採用後者,並用 Service 來託管懸浮視窗,讓它能在全域存在。

簡單介紹之後,我們直接看程式碼實現,註解已經寫在程式碼中。

 MainActivity.java package com.example.floatviewdemo; import com.example.floatviewdemo.service.FloatViewService; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onStart() { Intent intent = new Intent(MainActivity.this, FloatViewService.class); //啟動FloatViewService startService(intent); super.onStart(); } @Override protected void onStop() { // 銷毀懸浮窗 Intent intent = new Intent(MainActivity.this, FloatViewService.class); //終止FloatViewService stopService(intent); super.onStop(); } }

activity_main.xml

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.floatviewdemo.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>

實現懸浮窗功能的 Service 類別

 package com.example.floatviewdemo.service; import com.example.floatviewdemo.R; import android.annotation.SuppressLint; import android.app.Service; import android.content.Intent; import android.graphics.PixelFormat; import android.os.IBinder; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.WindowManager; import android.view.WindowManager.LayoutParams; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.Toast; public class FloatViewService extends Service { private static final String TAG = "FloatViewService"; //定義浮動窗口布局 private LinearLayout mFloatLayout; private WindowManager.LayoutParams wmParams; //創建浮動窗口設置布局參數的對象 private WindowManager mWindowManager; private ImageButton mFloatView; @Override public void onCreate() { super.onCreate(); Log.i(TAG, "onCreate"); createFloatView(); } @SuppressWarnings("static-access") @SuppressLint("InflateParams") private void createFloatView() { wmParams = new WindowManager.LayoutParams(); //通過getApplication獲取的是WindowManagerImpl.CompatModeWrapper mWindowManager = (WindowManager)getApplication().getSystemService(getApplication().WINDOW_SERVICE); //設置window type wmParams.type = LayoutParams.TYPE_PHONE; //設置圖片格式,效果為背景透明 wmParams.format = PixelFormat.RGBA_8888; //設置浮動窗口不可聚焦(實現操作除浮動窗口外的其他可見窗口的操作) wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE; //調整懸浮窗顯示的停靠位置為左側置頂 wmParams.gravity = Gravity.LEFT | Gravity.TOP; // 以屏幕左上角為原點,設置x、y初始值,相對於gravity wmParams.x = 0; wmParams.y = 152; //設置懸浮窗口長寬數據 wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT; wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT; LayoutInflater inflater = LayoutInflater.from(getApplication()); //獲取浮動窗口視圖所在布局 mFloatLayout = (LinearLayout) inflater.inflate(R.layout.alert_window_menu, null); //添加mFloatLayout mWindowManager.addView(mFloatLayout, wmParams); //浮動窗口按鈕 mFloatView = (ImageButton) mFloatLayout.findViewById(R.id.alert_window_imagebtn); mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); //設置監聽浮動窗口的觸摸移動 mFloatView.setOnTouchListener(new OnTouchListener() { boolean isClick; @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mFloatView.setBackgroundResource(R.drawable.circle_red); isClick = false; break; case MotionEvent.ACTION_MOVE: isClick = true; // getRawX是觸摸位置相對於屏幕的坐標,getX是相對於按鈕的坐標 wmParams.x = (int) event.getRawX() - mFloatView.getMeasuredWidth() / 2; // 減25為狀態欄的高度 wmParams.y = (int) event.getRawY() - mFloatView.getMeasuredHeight() / 2 - 75; // 刷新 mWindowManager.updateViewLayout(mFloatLayout, wmParams); return true; case MotionEvent.ACTION_UP: mFloatView.setBackgroundResource(R.drawable.circle_cyan); return isClick;// 此處返回false則屬於移動事件,返回true則釋放事件,可以出發點擊否。 default: break; } return false; } }); mFloatView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(FloatViewService.this, "一百塊都不給我!", Toast.LENGTH_SHORT).show(); } }); } @Override public void onDestroy() { super.onDestroy(); if(mFloatLayout != null) { //移除懸浮窗口 mWindowManager.removeView(mFloatLayout); } } @Override public IBinder onBind(Intent intent) { return null; } }

懸浮窗的 xml 檔案

 alert_window_menu.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageButton android:id="@+id/alert_window_imagebtn" android:layout_width="50dp" android:layout_height="50dp" android:background="@drawable/float_window_menu" android:contentDescription="@null" /> </LinearLayout>

整體來說,把懸浮視窗交給 Service 託管是一個關鍵決策:Service 在 Activity 於 onStop 時被停掉之前持續存活,讓懸浮按鈕能跨越不同介面存在;而透過 getRawX/getRawY 取得觸控的螢幕座標、再用 updateViewLayout 更新位置,就實現了自由拖動。onDestroy 中記得呼叫 removeView 移除視窗,避免記憶體洩漏。

以上是實現 Android 全螢幕懸浮按鈕的完整敘述,希望大家喜歡。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved