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

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 需同步置空,否則下次按下不會重新添加。
<?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 權限,並引導用戶到系統設置中授予「顯示在其他應用上層」的能力。
以上就是本文的全部內容,希望對大家的學習有所幫助。
Android Animation 動畫 Demo:Tween 動畫四兄弟與組合動畫
Android 內建的動畫體系主要有兩套:Tween 動畫(補間動畫)與 Frame 動畫(影格動畫)。本文聚焦在 Tween,Frame 留待下一篇。 Tween 動
微信朋友圈氣泡大字怎麼做 生成教學一篇通
最近微信朋友圈裡流行起一種氣泡樣式的大字動態,配圖不多、字體碩大,視覺效果格外吸睛。不少朋友好奇這種氣泡大字究竟怎麼做出來,小編特意打聽到了製作方法,一起來看。 微信朋
Android 熱修復入門:微信 Tinker 方案解析
在快速迭代的 App 開發中,若每次修正錯誤都必须重新發版,成本會非常高。熱修復技術讓開發者能在不重新安裝的情況下更新程式碼、資源或 So 庫。本文以微信開源的 Tink
Java/Android 設計模式筆記(11):原型模式 Prototype
本文要談的是最後一個建立型模式——原型模式(Prototype Pattern)。這個模式先準備一個樣板物件,再由樣板複製出內部屬性一致的執行個體,這個複製的動作在 C+