日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

android view可视区域,Android使用WindowManager构造悬浮view

發(fā)布時(shí)間:2025/4/16 Android 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android view可视区域,Android使用WindowManager构造悬浮view 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一般在android顯示一個(gè)View都是通過Activity的setContentView設(shè)置的,但是還有一種方法,可以直接使用WindowManager在整個(gè)應(yīng)用的最上層繪制我們需要顯示的view,總體的效果類似于AlertDialog的彈出效果。

使用WindowManager構(gòu)造這樣的一個(gè)懸浮View也比較簡單,直接通過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()) { //顯示W(wǎng)indowManager

show();

}

if (mHideBtn.hashCode() == v.hashCode()) { //隱藏windowmanager

hide();

}

}

private void show() {

mWindowView = mLayoutInflater.inflate(R.layout.item_layout, null);

View popView = mWindowView.findViewById(R.id.root);

//設(shè)置popView的觸摸事件,以便點(diǎn)擊空白區(qū)域的時(shí)候使懸浮view消失

popView.setOnTouchListener(this);

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

//窗口類型同系統(tǒng)彈出框

lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

//響應(yīng)輸入法

//lp.flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;

//透明層

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的可視區(qū)域

Rect globalRect = new Rect();

//獲取懸浮view的可視區(qū)域

Rect tmpRect = new Rect();

v.getGlobalVisibleRect(globalRect);

View child = ((ViewGroup) v).getChildAt(0);

child.getHitRect(tmpRect);

if (!tmpRect.contains(x, y) && globalRect.contains(x, y)) {

hide();

}

return true;

}

}

activity_main.xml:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

android:id="@+id/showbtn"

android:layout_width="match_parent"

android:layout_height="50dp"

android:gravity="center"

android:text="show" />

android:id="@+id/hidebtn"

android:layout_width="match_parent"

android:layout_height="50dp"

android:gravity="center"

android:text="hide" />

item_layout.xml:

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:id="@+id/root"

android:orientation="vertical">

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"/>

實(shí)現(xiàn)效果如下:

以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)Android有所幫助,也希望大家多多支持腳本之家。

總結(jié)

以上是生活随笔為你收集整理的android view可视区域,Android使用WindowManager构造悬浮view的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。