Android T多屏多显——应用双屏间拖拽移动功能(更新中)

2024-04-16 01:28

本文主要是介绍Android T多屏多显——应用双屏间拖拽移动功能(更新中),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

功能以及显示效果简介

需求:在双屏显示中,把启动的应用从其中一个屏幕中移动到另一个屏幕中。
操作:通过双指按压应用使其移动,如果移动的距离过小,我们就不移动到另一屏幕,否则移动到另一屏。
请添加图片描述

功能分析

多屏中移动应用至另一屏本质就是Task的移动。
从窗口层级结构的角度来说,就是把Display1中的DefaultTaskDisplayArea上的Task,移动到Display2中的DefaultTaskDisplayArea上。
容器结构简化树状图如下所示:
在这里插入图片描述

窗口层级结构简化树状图如下所示:
在这里插入图片描述

关键代码知识点

移动Task至另一屏幕

代码路径:frameworks/base/services/core/java/com/android/server/wm/RootWindowContainer.java

    /*** Move root task with all its existing content to specified display.** @param rootTaskId Id of root task to move.* @param displayId  Id of display to move root task to.* @param onTop      Indicates whether container should be place on top or on bottom.*/void moveRootTaskToDisplay(int rootTaskId, int displayId, boolean onTop) {//根据displayId获取DisplayContentfinal DisplayContent displayContent = getDisplayContentOrCreate(displayId);if (displayContent == null) {throw new IllegalArgumentException("moveRootTaskToDisplay: Unknown displayId="+ displayId);}//调用moveRootTaskToTaskDisplayArea方法moveRootTaskToTaskDisplayArea(rootTaskId, displayContent.getDefaultTaskDisplayArea(),onTop);}

入参说明:
rootTaskId需要移动的Task的Id。可以通过Task中getRootTaskId()方法获取。
displayId需要移动到对应屏幕的Display的Id。可以通过DisplayContent中的getDisplayId()方法获取。
onTop移动后的Task是放在容器顶部还是底部。true表示顶部,false表示底部。
代码解释:
这个方法首先通过getDisplayContentOrCreate方法根据displayId获取DisplayContent,然后调用moveRootTaskToTaskDisplayArea方法进行移动。
其中传递参数displayContent.getDefaultTaskDisplayArea(),表示获取DisplayContent下面的DefaultTaskDisplayArea。

    /*** Move root task with all its existing content to specified task display area.** @param rootTaskId      Id of root task to move.* @param taskDisplayArea The task display area to move root task to.* @param onTop           Indicates whether container should be place on top or on bottom.*/void moveRootTaskToTaskDisplayArea(int rootTaskId, TaskDisplayArea taskDisplayArea,boolean onTop) {//获取Taskfinal Task rootTask = getRootTask(rootTaskId);if (rootTask == null) {throw new IllegalArgumentException("moveRootTaskToTaskDisplayArea: Unknown rootTaskId="+ rootTaskId);}final TaskDisplayArea currentTaskDisplayArea = rootTask.getDisplayArea();if (currentTaskDisplayArea == null) {throw new IllegalStateException("moveRootTaskToTaskDisplayArea: rootTask=" + rootTask+ " is not attached to any task display area.");}if (taskDisplayArea == null) {throw new IllegalArgumentException("moveRootTaskToTaskDisplayArea: Unknown taskDisplayArea=" + taskDisplayArea);}if (currentTaskDisplayArea == taskDisplayArea) {throw new IllegalArgumentException("Trying to move rootTask=" + rootTask+ " to its current taskDisplayArea=" + taskDisplayArea);}//把获取到的task重新挂载到了新display的taskDisplayArearootTask.reparent(taskDisplayArea, onTop);// Resume focusable root task after reparenting to another display area.//窗口或任务reparent之后,恢复焦点,激活相关任务的活动,并更新活动的可见性,以确保窗口管理器和用户界面的状态一致和正确。rootTask.resumeNextFocusAfterReparent();// TODO(multi-display): resize rootTasks properly if moved from split-screen.}

根据前面传递的TaskId获取到Task,在通过rootTask.reparent(taskDisplayArea, onTop);方法,把这个Task重新挂载到了新display的taskDisplayArea上。然后使用rootTask.resumeNextFocusAfterReparent();方法更新窗口焦点显示。

  • rootTask.reparent(taskDisplayArea, onTop);
    代码路径:frameworks/base/services/core/java/com/android/server/wm/Task.java

     void reparent(TaskDisplayArea newParent, boolean onTop) {if (newParent == null) {throw new IllegalArgumentException("Task can't reparent to null " + this);}if (getParent() == newParent) {throw new IllegalArgumentException("Task=" + this + " already child of " + newParent);}//通过调用 canBeLaunchedOnDisplay 方法检查任务是否可以在新父区域所在的显示设备上启动。if (canBeLaunchedOnDisplay(newParent.getDisplayId())) {//实际执行reparent的操作。reparent(newParent, onTop ? POSITION_TOP : POSITION_BOTTOM);//如果Task是一个叶子Task(即没有子Task的Task)if (isLeafTask()) {//调用新父区域的 onLeafTaskMoved 方法来通知新父区域叶子Task已经移动。newParent.onLeafTaskMoved(this, onTop);}} else {Slog.w(TAG, "Task=" + this + " can't reparent to " + newParent);}}
    

    其中reparent(newParent, onTop ? POSITION_TOP : POSITION_BOTTOM);实际执行reparent的操作。这里根据 onTop 的值来决定任务应该被放置在新父区域的顶部还是底部。我们再看看这方法的具体实现。
    代码路径:frameworks/base/services/core/java/com/android/server/wm/WindowContainer.java

    void reparent(WindowContainer newParent, int position) {if (newParent == null) {throw new IllegalArgumentException("reparent: can't reparent to null " + this);}if (newParent == this) {throw new IllegalArgumentException("Can not reparent to itself " + this);}final WindowContainer oldParent = mParent;if (mParent == newParent) {throw new IllegalArgumentException("WC=" + this + " already child of " + mParent);}// Collect before removing child from old parent, because the old parent may be removed if// this is the last child in it.//记录reparent的容器(this)相关信息,这里的this指的是移动的Task,newParent是新的TaskDisplayAreamTransitionController.collectReparentChange(this, newParent);// The display object before reparenting as that might lead to old parent getting removed// from the display if it no longer has any child.//获取之前的DisplayContent和新的DisplayContentfinal DisplayContent prevDc = oldParent.getDisplayContent();final DisplayContent dc = newParent.getDisplayContent();//设置 mReparenting 为 true,表示正在执行reparent操作。//然后从旧父容器中移除当前容器,并将其添加到新父容器的指定位置。//最后,将 mReparenting 设置为 false,表示reparent操作完成。mReparenting = true;oldParent.removeChild(this);newParent.addChild(this, position);mReparenting = false;// Relayout display(s)//标记新父容器对应的显示内容为需要布局。//如果新父容器和旧父容器的显示内容不同,//则触发显示内容改变的通知,并标记旧显示内容也需要布局。//最后,调用layoutAndAssignWindowLayersIfNeeded方法确保显示内容按需进行布局和窗口层级的分配。dc.setLayoutNeeded();if (prevDc != dc) {onDisplayChanged(dc);prevDc.setLayoutNeeded();}getDisplayContent().layoutAndAssignWindowLayersIfNeeded();// Send onParentChanged notification here is we disabled sending it in setParent for// reparenting case.//处理窗口容器在父容器变更时的各种逻辑onParentChanged(newParent, oldParent);//处理窗口容器在不同父容器之间同步迁移的逻辑onSyncReparent(oldParent, newParent);}
    
  • rootTask.resumeNextFocusAfterReparent();
    代码路径:frameworks/base/services/core/java/com/android/server/wm/Task.java

        void resumeNextFocusAfterReparent() {//调整焦点adjustFocusToNextFocusableTask("reparent", true /* allowFocusSelf */,true /* moveDisplayToTop */);//恢复当前焦点任务的顶部活动mRootWindowContainer.resumeFocusedTasksTopActivities();// Update visibility of activities before notifying WM. This way it won't try to resize// windows that are no longer visible.//更新activities的可见性mRootWindowContainer.ensureActivitiesVisible(null /* starting */, 0 /* configChanges */,!PRESERVE_WINDOWS);}
    

这篇关于Android T多屏多显——应用双屏间拖拽移动功能(更新中)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907459

相关文章

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

PHP应用中处理限流和API节流的最佳实践

《PHP应用中处理限流和API节流的最佳实践》限流和API节流对于确保Web应用程序的可靠性、安全性和可扩展性至关重要,本文将详细介绍PHP应用中处理限流和API节流的最佳实践,下面就来和小编一起学习... 目录限流的重要性在 php 中实施限流的最佳实践使用集中式存储进行状态管理(如 Redis)采用滑动

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

PostgreSQL简介及实战应用

《PostgreSQL简介及实战应用》PostgreSQL是一种功能强大的开源关系型数据库管理系统,以其稳定性、高性能、扩展性和复杂查询能力在众多项目中得到广泛应用,本文将从基础概念讲起,逐步深入到高... 目录前言1. PostgreSQL基础1.1 PostgreSQL简介1.2 基础语法1.3 数据库

基于Java和FFmpeg实现视频压缩和剪辑功能

《基于Java和FFmpeg实现视频压缩和剪辑功能》在视频处理开发中,压缩和剪辑是常见的需求,本文将介绍如何使用Java结合FFmpeg实现视频压缩和剪辑功能,同时去除数据库操作,仅专注于视频处理,需... 目录引言1. 环境准备1.1 项目依赖1.2 安装 FFmpeg2. 视频压缩功能实现2.1 主要功

使用Python实现无损放大图片功能

《使用Python实现无损放大图片功能》本文介绍了如何使用Python的Pillow库进行无损图片放大,区分了JPEG和PNG格式在放大过程中的特点,并给出了示例代码,JPEG格式可能受压缩影响,需先... 目录一、什么是无损放大?二、实现方法步骤1:读取图片步骤2:无损放大图片步骤3:保存图片三、示php

Python中的filter() 函数的工作原理及应用技巧

《Python中的filter()函数的工作原理及应用技巧》Python的filter()函数用于筛选序列元素,返回迭代器,适合函数式编程,相比列表推导式,内存更优,尤其适用于大数据集,结合lamb... 目录前言一、基本概念基本语法二、使用方式1. 使用 lambda 函数2. 使用普通函数3. 使用 N

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi