編輯:關於 Android 程式設計
懸浮窗這種效果看起來神奇,其實可以透過系統服務 WindowManager 直接做到。本文示範如何在目前的 Activity 之上放一個可以拖動的懸浮 View。
這個介面用於與視窗管理器(應用框架層)互動,可透過 getSystemService(Context.WINDOW_SERVICE) 取得實例。
public interface WindowManager implements ViewManager
所屬套件:android.view.WindowManager
重要方法:
addView():加入 view。removeView():移除 view。updateViewLayout():更新 view 的參數。Window Manager Service 是全域且唯一的。它把使用者操作翻譯成指令,派送給畫面上的各個 Window。Activity 會把頂層控制項註冊進 Window Manager,使用者觸碰螢幕或按鍵時由它負責通知;控制項有請求產生時,也會沿著 ViewParent 送回 Window Manager,整個通訊流程由此閉合。
既然 WindowManager 能新增、刪除、變更 view,那麼要做拖曳,就得取得 ImageView 的座標。先取得相對螢幕的座標(以螢幕左上角為原點):
float x = event.getRawX(); float y = event.getRawY() - 25; // 25 為系統狀態列高度
再透過 WindowManager.LayoutParams 寫入 x、y:
wmParams.x = (int) (x - mTouchStartX); wmParams.y = (int) (y - mTouchStartY);
最後呼叫 updateViewLayout() 讓新位置生效。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
建立 MyApplication,把 LayoutParams 提到全域,讓自訂 View 與 Activity 共用同一份參數:
import android.app.Application;
import android.view.WindowManager;
public class MyApplication extends Application {
/**
* 全域變數。
* 別忘了在 AndroidManifest.xml 的 application 節點加上
* android:name=".MyApplication"
*/
private WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
public WindowManager.LayoutParams getMywmParams() {
return wmParams;
}
}
import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.ImageView;
public class MyFloatView extends ImageView {
private float mTouchStartX;
private float mTouchStartY;
private float x;
private float y;
private WindowManager wm = (WindowManager)
getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
// 取得全域的 LayoutParams,用來保存懸浮視窗屬性
private WindowManager.LayoutParams wmParams =
((MyApplication) getContext().getApplicationContext()).getMywmParams();
public MyFloatView(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// 相對螢幕的座標,原點在螢幕左上角
x = event.getRawX();
y = event.getRawY() - 25; // 扣掉狀態列高度
Log.i("currP", "currX" + x + "====currY" + y);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 相對 View 自身的座標,原點在 View 左上角
mTouchStartX = event.getX();
mTouchStartY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
updateViewPosition();
break;
case MotionEvent.ACTION_UP:
updateViewPosition();
mTouchStartX = mTouchStartY = 0;
break;
}
return true;
}
private void updateViewPosition() {
wmParams.x = (int) (x - mTouchStartX);
wmParams.y = (int) (y - mTouchStartY);
wm.updateViewLayout(this, wmParams);
}
}
import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
public class MyFloatViewActivity extends Activity {
private WindowManager wm = null;
private WindowManager.LayoutParams wmParams = null;
private MyFloatView myFV = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createView();
}
private void createView() {
myFV = new MyFloatView(getApplicationContext());
myFV.setImageResource(R.drawable.angry_birds);
wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
wmParams = ((MyApplication) getApplication()).getMywmParams();
wmParams.type = LayoutParams.TYPE_PHONE; // 視窗類型
wmParams.format = PixelFormat.RGBA_8888; // 圖片格式,背景透明
wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE; // 不搶焦點
wmParams.gravity = Gravity.LEFT | Gravity.TOP; // 對齊左上角
wmParams.x = 0; // 初始 x
wmParams.y = 0; // 初始 y
wmParams.width = 40; // 寬
wmParams.height = 40; // 高
wm.addView(myFV, wmParams);
}
@Override
public void onDestroy() {
super.onDestroy();
// Activity 銷毀時務必移除,否則視窗會殘留
wm.removeView(myFV);
}
}
硬寫 25 來扣狀態列高度在不同機型上未必準確,建議改用 getWindowManager().getDefaultDisplay().getSize() 或 WindowInsets 取得真實高度。權限方面,Android 6.0 之後 SYSTEM_ALERT_WINDOW 需要另外透過 Settings.ACTION_MANAGE_OVERLAY_PERMISSION 引導使用者手動開啟;而 TYPE_PHONE 等高層級視窗類型在 Android 8.0 之後已被限制,需改用 TYPE_APPLICATION_OVERLAY。最後,onDestroy() 裡的 removeView() 千万不能省,否則會造成視窗洩漏。
Android ProGuard 代碼混淆指南:原理、配置與常見保留規則
Android ProGuard 代碼混淆指南 ProGuard 是 Android 打包流程中常見的工具,主要用來壓縮、優化、混淆與預檢 Java 程式碼。它不僅能讓
Android 通過 Path 實現復雜效果(搜索按鈕 + 時鐘的實現)
Path:在 Android 中,絕大多數複雜圖形的繪制都是通過 Path 實現的,比如繪製一條曲線並讓物體沿其運動,比如搜索按鈕,比如一個簡單的時鐘。 定義:Path
Android 屬性動畫完全解析:ObjectAnimator、ValueAnimator 與 AnimatorSet
屬性動畫——res/animator 所謂屬性動畫,顧名思義就是通過動畫的方式去改變對象的屬性。開始之前,我們先了解幾個關鍵概念: · Duration:動畫的持續時間,
為 RecyclerView 的 Item 添加點擊事件
由於 RecyclerView 不再負責 Item 的布局與顯示,它也沒有像 ListView 那樣內置 OnItemClickListener。要實現 Item 點擊,