Android使用WindowManager构造悬浮view
作者:summerpxy 发布时间:2022-08-03 00:43:13
标签:Android,WindowManager,悬浮,view
一般在android显示一个View都是通过Activity的setContentView设置的,但是还有一种方法,可以直接使用WindowManager在整个应用的最上层绘制我们需要显示的view,总体的效果类似于AlertDialog的弹出效果。
使用WindowManager构造这样的一个悬浮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()) { //显示WindowManager
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);
//设置popView的触摸事件,以便点击空白区域的时候使悬浮view消失
popView.setOnTouchListener(this);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
//窗口类型同系统弹出框
lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
//响应输入法
//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的可视区域
Rect globalRect = new Rect();
//获取悬浮view的可视区域
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:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://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="http://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 Studio下载(下文统称AS)AS最新版下载请戳:AS下载Android SDK下载SDK安装器下载SDK安装器下载请戳:
- 一、内部类介绍1.定义:一个类内部又嵌套了一个类,被嵌套的类就是内部类(inner class),嵌套其他类的称为外部类(outer cla
- springboot定时任务在springboot环境下有多种方法,这里记录下使用过的其中两种;1、使用注解,2、通过实现接口的方式。使用注
- 本博文将为您提供自Java 7以来增加的很棒的新功能的示例。我将展示每个Java版本的至少一项重大改进,一直到2020年秋季发布的Java
- 实现功能权限校验的功能有多种方法,其一使用 * 拦截请求,其二是使用AOP抛异常。 首先用 * 实现未登录时跳转到登录界面的功能。注意这里没
- 前言在web开发过程中涉及到表格时,例如dataTable,就会产生分页的需求,通常我们将分页方式分为两种:前端分页和后端分页。前端分页一次
- AOP :面向切面编程在程序设计中,我们需要满足高耦合低内聚,所以编程需满足六大原则,一个法则.AOP面向切面编程正是为了满足这些原则的一种
- 有时我们需要判断某个类是否实现了某个接口(Interface),比如在使用反射机制(Reflection)来查找特定类型的时候。简单来说,可
- 这篇文章主要介绍了springboot2.0如何通过fastdfs实现文件分布式上传,文中通过示例代码介绍的非常详细,对大家的学习或者工作具
- 本文实例为大家分享了Android实现闪光灯效果的具体代码,供大家参考,具体内容如下一、声明闪光灯的权限<uses-permissio
- 大家都知道使用java反射可以在运行时动态改变对象的行为,甚至是private final的成员变量,但并不是所有情况下,都可以修改成员变量
- indexof方法:注解:indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回-1
- 折半查找法仅适用于对已有顺序的数组、数据进行操作!!!(从小到大)自我总结:折半查找法就是相当于(通过改变low或high的大小)把中间位置
- 上篇文章给大家介绍了springboot全局字符编码设置解决乱码问题 感兴趣的朋友可以点击查看,下面通过两种方式给大家介绍Spri
- 本文实例讲述了C#实现汉字转拼音或转拼音首字母的方法。分享给大家供大家参考。具体实现方法如下:/// <summary>///
- 本文实例讲述了Java基于JDBC实现事务,银行转账及货物进出库功能。分享给大家供大家参考,具体如下:1. 转账业务转账必须执行2个sql语
- 一、概述应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe
- 一、前言 本学期学习了JAVA语言,在学期的结束,写一个有操作界面,与数据库关联的管理系统,用来巩固自己本学习所学的知识。 用到的知识:JA
- 定义队列是 Apache RocketMQ 中消息存储和传输的实际容器,也是 Apache RocketMQ 消息的最小存储单元。 Apac