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

用 WindowManager 在應用最上層繪製懸浮 View

編輯:關於 Android 程式設計

一般來說,Android 顯示一個 View 都是透過 Activity 的 setContentView 來設定。但其實還有一條路:直接用 WindowManager 把 View 畫在整個應用的最上層,整體效果近似 AlertDialog 的彈出體驗。

做法並不難,核心就是 windowmanager.addView() 這個方法。

package com.gearmotion.app.windowmanagermotion;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends AppCompatActivity
        implements View.OnClickListener, View.OnTouchListener {

    Button mShowBtn;
    Button mHideBtn;
    WindowManager mWm;
    LayoutInflater mLayoutInflater;
    View mWindowView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mShowBtn = (Button) this.findViewById(R.id.showbtn);
        mHideBtn = (Button) this.findViewById(R.id.hidebtn);
        mShowBtn.setOnClickListener(this);
        mHideBtn.setOnClickListener(this);

        init();
    }

    private void init() {
        mWm = (WindowManager) this.getApplicationContext()
                .getSystemService(Context.WINDOW_SERVICE);
        mLayoutInflater = LayoutInflater.from(this);
    }

    @Override
    public void onClick(View v) {
        if (mShowBtn.hashCode() == v.hashCode()) {
            show();
        }
        if (mHideBtn.hashCode() == v.hashCode()) {
            hide();
        }
    }

    private void show() {
        mWindowView = mLayoutInflater.inflate(R.layout.item_layout, null);
        View popView = mWindowView.findViewById(R.id.root);
        // 設置 popView 的觸控事件,以便點擊空白區域時讓懸浮 view 消失
        popView.setOnTouchListener(this);

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        // 窗口類型同系統彈出框
        lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        // 透明層
        lp.format = PixelFormat.TRANSPARENT;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        lp.gravity = Gravity.CENTER_VERTICAL;

        mWm.addView(mWindowView, lp);
    }

    private void hide() {
        if (mWindowView != null && mWindowView.getParent() != null) {
            mWm.removeView(mWindowView);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        // 獲取主 view 的可視區域
        Rect globalRect = new Rect();
        // 獲取懸浮 view 的可視區域
        Rect tmpRect = new Rect();
        v.getGlobalVisibleRect(globalRect);
        View child = ((ViewGroup) v).getChildAt(0);
        child.getHitRect(tmpRect);

        // 點在懸浮 view 之外、但在窗口範圍之內,就關閉它
        if (!tmpRect.contains(x, y) && globalRect.contains(x, y)) {
            hide();
        }
        return true;
    }
}

這裡有幾個配置值得留意:lp.type 設為 TYPE_SYSTEM_ALERT 讓窗口行為接近系統彈窗;lp.format 用 PixelFormat.TRANSPARENT 保持透明底;而 onTouch 中透過 getHitRect 判斷觸點是否落在內容區之外,就能實現「點空白處關閉」的常見互動。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/showbtn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="show" />

    <Button
        android:id="@+id/hidebtn"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="hide" />
</LinearLayout>

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:id="@+id/root"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="I am WindowManager layout view"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_gravity="center"
        android:background="#FFF8DC"
        android:textColor="#7AC5CD" />
</LinearLayout>

成果如下:

以上就是本文的全部內容,希望對大家學習 Android 有所幫助,也歡迎多多支持本站。

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