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

用 WindowManager 製作一個可拖動的浮動控件

編輯:關於 Android 程式設計

懸浮按鈕、可拖動的小窗,這類效果在 Android 裡靠 WindowManager 就能實現。它允許你把一個 View 直接添加到窗口層級上,並通過修改 LayoutParams 的 x、y 值來控制位置,跟著手指走毫不費力。

效果如下:

第一步:新建 DragView 繼承 RelativeLayout

 package com.rong.activity; import com.rong.test.R; import android.content.Context; import android.graphics.Color; import android.graphics.PixelFormat; import android.util.AttributeSet; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.RelativeLayout; public class DragView extends RelativeLayout { private WindowManager windowManager;// 用於可拖動的浮動窗口 private WindowManager.LayoutParams windowParams;// 浮動窗口的參數 private Button myButton; public DragView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { View.inflate(getContext(), R.layout.layout_my, this); myButton = new Button(getContext()); myButton.setText("我的"); myButton.setBackgroundColor(Color.RED); } @Override public boolean onTouchEvent(MotionEvent event) { // 獲取當前點的xy位置 int currentX = (int) event.getX(); int currentY = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (windowManager == null) { setWindowParams(currentX, currentY); windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); windowManager.addView(myButton, windowParams); } break; case MotionEvent.ACTION_MOVE: windowParams.x = currentX; windowParams.y = currentY; windowManager.updateViewLayout(myButton, windowParams); break; case MotionEvent.ACTION_UP: // windowManager.removeView(myButton); break; } return true; } private void setWindowParams(int x, int y) { // 建立item的縮略圖 windowParams = new WindowManager.LayoutParams(); windowParams.gravity = Gravity.TOP | Gravity.LEFT;// 這個必須加 // 得到preview左上角相對於屏幕的坐標 windowParams.x = x; windowParams.y = y; // 設置寬和高 windowParams.width = 200; windowParams.height = 200; windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; windowParams.format = PixelFormat.TRANSLUCENT; windowParams.windowAnimations = 0; } } 

幾個關鍵點:

gravity 必須顯式指定。不設置的話,x/y 的參考原點會依系統默認而變,位置計算跟著亂掉,所以這裡固定為 Gravity.TOP | Gravity.LEFT。

添加時機放在 ACTION_DOWN。手指按下才把浮動視圖加進窗口,避免頁面一進來就有個控件懸在那裡。

拖動靠 updateViewLayout。ACTION_MOVE 裡更新 windowParams 的 x、y,再調用 updateViewLayout 讓系統重排,這比不斷 remove/add 高效得多。

flags 的組合有講究。FLAG_NOT_FOCUSABLE 不搶焦點,FLAG_NOT_TOUCHABLE 讓觸摸事件穿透,FLAG_KEEP_SCREEN_ON 保持亮屏,FLAG_LAYOUT_IN_SCREEN 允許佈局覆蓋狀態列。按实际需求增減即可。

另外,示例中 ACTION_UP 裡的 removeView 被註釋掉了,也就是浮動視圖拖完仍留在窗口上。若希望只在拖動期間顯示,把它取消註釋即可;但要注意 windowManager 需同步置空,否則下次按下不會重新添加。

第二步:新建佈局文件 activity_main.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/main_touchlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical" > <com.rong.activity.DragView android:id="@+id/main_touchview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" android:background="#ff0000" /> </RelativeLayout> 

佈局裡放一個鋪滿父容器的 DragView 作為觸摸區域,浮動的按鈕由 WindowManager 獨立管理,兩者互不干擾。

最後提醒一句:如果這個浮動控件需要在其他應用之上顯示,還要額外申請 SYSTEM_ALERT_WINDOW 權限,並引導用戶到系統設置中授予「顯示在其他應用上層」的能力。

以上就是本文的全部內容,希望對大家的學習有所幫助。

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