網站首頁 編程語言 正文
前言
懸浮窗是一種比較常見的需求。例如把視頻通話界面縮小成一個懸浮窗,然后用戶可以在其他界面上處理事情。
本文給出一個簡單的應用內懸浮窗實現。可縮小activity和還原大小。可懸浮在同一個app的其他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
原文鏈接:https://www.an.rustfisher.com/android/activity/floating-scale-act/
相關推薦
- 2022-02-27 Spring Boot -- 創建工程時 Spring Initializr 報錯 Error:co
- 2022-06-21 C語言全面講解順序表使用操作_C 語言
- 2022-04-20 Mac中pyenv的安裝與使用教程_python
- 2022-09-16 利用Python第三方庫xlrd讀取Excel中數據實例代碼_python
- 2022-06-26 Python實現將多張圖片合成視頻并加入背景音樂_python
- 2022-10-03 python中pandas操作apply返回多列的實現_python
- 2022-04-09 python中IO流和對象序列化詳解_python
- 2021-12-09 C語言數據結構與算法之鏈表(一)_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支