Android 悬浮窗
悬浮窗是一种比较常见的需求。例如把视频通话界面缩小成一个悬浮窗,然后用户可以在其他界面上处理事情。
本文给出一个简单的悬浮窗实现。可缩小activity和还原大小。可悬浮在其他activity上。使用TouchListener监听触摸事件,拖动悬浮窗。
缩放方法
缩放activity需要使用WindowManager.LayoutParams,控制window的宽高
在activity中调用
android.view.WindowManager.LayoutParams p = getWindow().getAttributes();
p.height = 480; // 高度
p.width = 360; // 宽度
p.dimAmount = 0.0f; // 不让下面的界面变暗
getWindow().setAttributes(p);
dim:
adj. 暗淡的; 昏暗的; 微弱的; 不明亮的; 光线暗淡的;
v. (使)变暗淡,变微弱,变昏暗; (使)减弱,变淡漠,失去光泽;
修改了WindowManager.LayoutParams的宽高,activity的window大小会发生变化。
要变回默认大小,在activity中调用
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
如果缩小时改变了位置,需要把window的位置置为0
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.x = 0;
lp.y = 0;
getWindow().setAttributes(lp);
activity变小时,后面可能是黑色的背景。这需要进行下面的操作。
悬浮样式
在styles.xml里新建一个MeTranslucentAct。
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowNoTitle">true</item>
</style>
<style name="TranslucentAct" parent="AppTheme">
<item name="android:windowBackground">#80000000</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
</style>
</resources>
主要style是AppCompat的。
指定一个window的背景android:windowBackground
使用的Activity继承自androidx.appcompat.app.AppCompatActivity
activity缩小后,背景是透明的,可以看到后面的其他页面
点击穿透空白
activity缩小后,点击旁边空白处,其他组件能接到点击事件
在onCreate方法的setContentView之前,给WindowManager.LayoutParams添加标记FLAG_LAYOUT_NO_LIMITS和FLAG_NOT_TOUCH_MODAL
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
mBinding = DataBindingUtil.setContentView(this, R.layout.act_float_scale);
移动悬浮窗
监听触摸事件,计算出手指移动的距离,然后移动悬浮窗。
private boolean mIsSmall = false; // 当前是否小窗口
private float mLastTx = 0; // 手指的上一个位置x
private float mLastTy = 0;
// ....
mBinding.root.setOnTouchListener((v, event) -> {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "down " + event);
mLastTx = event.getRawX();
mLastTy = event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "move " + event);
float dx = event.getRawX() - mLastTx;
float dy = event.getRawY() - mLastTy;
mLastTx = event.getRawX();
mLastTy = event.getRawY();
Log.d(TAG, " dx: " + dx + ", dy: " + dy);
if (mIsSmall) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.x += dx;
lp.y += dy;
getWindow().setAttributes(lp);
}
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "up " + event);
return true;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG, "cancel " + event);
return true;
}
return false;
});
mIsSmall用来记录当前activity是否变小(悬浮)。
在触摸监听器中返回true,表示消费这个触摸事件。
event.getX()和event.getY()获取到的是当前View的触摸坐标。
event.getRawX()和event.getRawY()获取到的是屏幕的触摸坐标。即触摸点在屏幕上的位置。
例子的完整代码
启用了databinding
android {
dataBinding {
enabled = true
}
}
styles.xml
新建一个样式
<style name="TranslucentAct" parent="AppTheme">
<item name="android:windowBackground">#80000000</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item>
</style>
layout
act_float_scale.xml里面放一些按钮,控制放大和缩小。
ConstraintLayout拿来监听触摸事件。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#555555">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/to_small"
style="@style/NormalBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="变小" />
<Button
android:id="@+id/to_reset"
style="@style/NormalBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="还原" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
activity
FloatingScaleAct
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import com.rustfisher.tutorial2020.R;
import com.rustfisher.tutorial2020.databinding.ActFloatScaleBinding;
public class FloatingScaleAct extends AppCompatActivity {
private static final String TAG = "rfDevFloatingAct";
ActFloatScaleBinding mBinding;
private boolean mIsSmall = false; // 当前是否小窗口
private float mLastTx = 0; // 手指的上一个位置
private float mLastTy = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
mBinding = DataBindingUtil.setContentView(this, R.layout.act_float_scale);
mBinding.toSmall.setOnClickListener(v -> toSmall());
mBinding.toReset.setOnClickListener(v -> {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.x = 0;
lp.y = 0;
getWindow().setAttributes(lp);
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mIsSmall = false;
});
mBinding.root.setOnTouchListener((v, event) -> {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d(TAG, "down " + event);
mLastTx = event.getRawX();
mLastTy = event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "move " + event);
float dx = event.getRawX() - mLastTx;
float dy = event.getRawY() - mLastTy;
mLastTx = event.getRawX();
mLastTy = event.getRawY();
Log.d(TAG, " dx: " + dx + ", dy: " + dy);
if (mIsSmall) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.x += dx;
lp.y += dy;
getWindow().setAttributes(lp);
}
break;
case MotionEvent.ACTION_UP:
Log.d(TAG, "up " + event);
return true;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG, "cancel " + event);
return true;
}
return false;
});
}
private void toSmall() {
mIsSmall = true;
WindowManager m = getWindowManager();
Display d = m.getDefaultDisplay();
WindowManager.LayoutParams p = getWindow().getAttributes();
p.height = (int) (d.getHeight() * 0.35);
p.width = (int) (d.getWidth() * 0.4);
p.dimAmount = 0.0f;
getWindow().setAttributes(p);
}
}
manifest里注册这个activity
<activity
android:name=".act.FloatingScaleAct"
android:theme="@style/TranslucentAct" />
运行效果
在红米9A(Android 10,MIUI 12.5.1 稳定版)和荣耀(Android 5.1)上运行OK

小结
为实现悬浮窗效果,思路是改变activity大小,将activity所在window的背景设置透明,监听触摸事件改变window的位置。
主要使用的类 WindowManager.LayoutParams
Android 悬浮窗的更多相关文章
- Android悬浮窗实现 使用WindowManager
Android悬浮窗实现 使用WindowManager WindowManager介绍 通过Context.getSystemService(Context.WINDOW_SERVICE)可以获得 ...
- Android悬浮窗及其拖动事件
主页面布局很简单,只有一个RelativelyLayout <?xml version="1.0" encoding="utf-8"?> <R ...
- Android 悬浮窗权限校验
原文:Android 悬浮窗权限校验 悬浮窗权限: <uses-permission android:name="android.permission.SYSTEM_ALERT_WIN ...
- Android 悬浮窗、悬浮球开发
原文:Android 悬浮窗.悬浮球开发 1.权限管理 直接看我另外一篇博客吧,传送门: https://my.oschina.net/u/1462828/blog/1933162 2.Base类Ba ...
- Android 悬浮窗 System Alert Window
悬浮窗能显示在其他应用上方.桌面系统例如Windows,macOS,Ubuntu,打开的程序能以窗口形式显示在屏幕上. 受限于屏幕大小,安卓系统中主要使用多任务切换的方式和分屏的方式.视频播放,视频对 ...
- 关于Android悬浮窗要获取按键响应的问题
要在Android中实现顶层的窗口弹出,一般都会用WindowsManager来实现,但是几乎所有的网站资源都是说弹出的悬浮窗不用接受任何按键响应. 而问题就是,我们有时候需要他响应按键,比如电视上的 ...
- Android 悬浮窗权限各机型各系统适配大全
这篇博客主要介绍的是 Android 主流各种机型和各种版本的悬浮窗权限适配,但是由于碎片化的问题,所以在适配方面也无法做到完全的主流机型适配,这个需要大家的一起努力,这个博客的名字永远都是一个将来时 ...
- Android 使用WindowManager实现Android悬浮窗
WindowManager介绍 通过Context.getSystemService(Context.WINDOW_SERVICE)可以获得 WindowManager对象. 每一个WindowMan ...
- Android悬浮窗注意事项
一 动画无法运行 有时候,我们对添加的悬浮窗口,做动画的时候,始终无法运行. 那么,这个时候,我们可以对要做动画的View,再添加一个parent,即容器.将要做动画的View放入容器中. 二 悬浮窗 ...
随机推荐
- linux 定时导出sql查询结果文件
如果想在服务器端生成sql查询结果的txt文件. 大体思路就是: 1.创建一个到处txt文件的sql脚本. set ARRAYSIZE 50 --从数据库往客户端一次发送记录数 set linesiz ...
- SVN终端演练-版本回退
1. 版本回退概念以及原因? 概念: 是指将代码(本地代码或者服务器代码), 回退到之前记录的某一特定版本 原因: 如果代码做错了, 想返回之前某个状态重做;2. 修改了,但未提交的情况下 ...
- 使用jstl和el表达式来展示request域中存放的user对象的信息
<%@ page import="java.util.ArrayList" %><%@ page import="java.util.List" ...
- 【手帐】Bullet Journal教程
最近觉得自己的日程记录本有待提高,于是从今年开始开始入坑了手帐. *内容源自Bullet Journal官网.https://bulletjournal.com/pages/learn 快速笔记 Bu ...
- 【C/C++】【输入】关于scanf:输入空格,多次使用
一.C/C++中带空格字符串的输入 C++中的cin和C中的scanf都是遇到空格或回车结束. 如果要让scanf接收空格,可以用读入字符集合的方式.%[] char a[100]; scanf(&q ...
- QPS和TPS的区别于理解
TPS: (每秒事务处理量(TransactionPerSecond)) 一个表达系统处理能力的性能指标,每秒处理的消息数(Transaction Per Second),每秒事务处理量 - 性能测试 ...
- java多线程4:volatile关键字
上文说到了 synchronized,那么就不得不说下 volatile关键字了,它们两者经常协同处理多线程的安全问题. volatile保证可见性 那么volatile的作用是什么呢? 在jvm运行 ...
- iOS 实现简单的界面切换
以下是在iOS中最简单的界面切换示例.使用了多个Controller,并演示Controller之间在切换界面时的代码处理. 实现的应用界面: 首先,创建一个window-based applicat ...
- Windows11下载地址
10月5日微软推出了Win11正式版本,但是仍然不支持apk格式,不免让人失望.下面是Win11商业版本下载地址: 下载地址1: ed2k://|file|zh-cn_windows_11_busin ...
- [BUUCTF]PWN12——[BJDCTF 2nd]r2t3
[BUUCTF]PWN12--[BJDCTF 2nd]r2t3 题目网址:https://buuoj.cn/challenges#[BJDCTF%202nd]r2t3 步骤: 例行检查,32位,开启了 ...