多屏联动 - 非动画

基于Android R版本分析

新增 DoubleScreenMovePointerEventListener 类:

java 复制代码
package com.android.server.wm;
​
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManagerPolicyConstants;
​
public class DoubleScreenMovePointerEventListener implements WindowManagerPolicyConstants.PointerEventListener {
    private static final String TAG = DoubleScreenMovePointerEventListener.class.getName();
​
    private boolean shouldBeginMove = false;
    private int mPoint0FirstX = 0;
    private int mPoint1FirstX = 0;
    private int mPoint0LastX = 0;
    private int mPoint1LastX = 0;
    int START_GAP = 20; //动作触发阈值,最少移动为20个像素才可以
    private final WindowManagerService mService;
    private final DisplayContent mDisplayContent;
​
    public DoubleScreenMovePointerEventListener(WindowManagerService service, DisplayContent displayContent) {
        mService = service;
        mDisplayContent = displayContent;
    }
​
    @Override
    public void onPointerEvent(MotionEvent motionEvent) {
        Log.d(TAG, "onPointerEvent: motionEvent = " + motionEvent);
        switch (motionEvent.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:
                if (motionEvent.getPointerCount() > 1) {
                    shouldBeginMove = false;
                    Log.d(TAG, "onPointerEvent: motionEvent.getPointerCount() > 2 end DoubleScreenMove");
                }
                if (motionEvent.getPointerCount() == 1) {
                    if (mPoint0FirstX == 0) {
                        //if (mPoint0FirstX == 0 && mPoint1FirstX == 0) {
                        mPoint0FirstX = (int) motionEvent.getX(0);
//                        mPoint1FirstX = (int) motionEvent.getX(1);
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (motionEvent.getPointerCount() == 1) {
                    if (!shouldBeginMove && (motionEvent.getX(0) - mPoint0FirstX) > START_GAP) {
                        // && (motionEvent.getX(1) - mPoint1FirstX) > START_GAP
                        //识别了双指动作达到触发task移动条件,则调用对应mDisplayContent.doTestMoveTaskToOtherDisplay方法
                        Log.d(TAG, "onPointerEvent: start DoubleScreenMove");
                        shouldBeginMove = true;
                        mDisplayContent.doMoveStackToDisplay();
                    }
                    mPoint0LastX = (int) motionEvent.getX(0);
//                    mPoint1LastX = (int) motionEvent.getX(1);
                }
                break;
            case MotionEvent.ACTION_POINTER_UP:
            case MotionEvent.ACTION_UP:
                shouldBeginMove = false;
                mPoint0FirstX = mPoint1FirstX = 0;
                Log.d(TAG, "onPointerEvent: ACTION_UP end DoubleScreenMove");
                break;
        }
    }
}

新增DoubleScreenMovePointerEventListener初始化(在DisplayContent中):

scss 复制代码
final DoubleScreenMovePointerEventListener mPointerEventListener;
​
DisplayContent(Display display, RootWindowContainer root) {
    ........................
    mTapDetector = new TaskTapPointerEventListener(mWmService, this);
    mPointerEventListener = new DoubleScreenMovePointerEventListener(mWmService,this);
    registerPointerEventListener(mTapDetector);
    registerPointerEventListener(mPointerEventListener);
    registerPointerEventListener(mWmService.mMousePositionTracker);
    ........................
}

修改DisplayContent,新增doMoveStackToDisplay()方法:

ini 复制代码
public void doMoveStackToDisplay() {
    DisplayContent otherDisplay = null;
    if (mRootWindowContainer.getChildCount() >= 2) {
        otherDisplay = (mRootWindowContainer.getChildAt(0) == this)
            ? mRootWindowContainer.getChildAt(1) : mRootWindowContainer.getChildAt(0);
    }
    if (otherDisplay != this && otherDisplay != null) {
        int topStackId = 0;
        try {
            Task topStack = getTopStack();
            if (topStack.isActivityTypeHome()) {
                Log.d(TAG, "doMoveStackToDisplay: isActivityTypeHome");
                return;
            }
            topStackId = topStack.mTaskId;
            mRootWindowContainer.moveStackToDisplay(topStackId, otherDisplay.mDisplayId, true);
        } catch (Exception ex) {
            Log.d(TAG, "doMoveStackToDisplay: exception = " + ex);
        }
    }
}

参考:blog.csdn.net/learnframew...

相关推荐
银河系栋梁25 分钟前
Android AIDL理解
android·运维·服务器
掘根32 分钟前
【jsonRpc项目】Dispatcher模块
android·网络
独行soc32 分钟前
2026年渗透测试面试题总结-10(题目+回答)
android·网络·python·安全·web安全·渗透测试·安全狮
studyForMokey36 分钟前
【Android面试】Java & Kotlin语言
android·java·面试
独自归家的兔37 分钟前
系统存储机制深度剖析:从Win11临时文件夹设计看微软存储架构演进
架构
●VON1 小时前
Flutter for OpenHarmony:基于原子清空与用户意图防护的 TodoList 批量删除子系统实现
学习·flutter·架构·跨平台·von
molaifeng1 小时前
万字长文解析:Redis 8.4 网络 IO 架构深度拆解
网络·redis·架构
SJLoveIT1 小时前
【深度复盘】Redis 分布式锁:从 SETNX 到 Redisson 看门狗的架构权衡
redis·分布式·架构
鸣弦artha1 小时前
Flutter框架跨平台鸿蒙开发——Drawer抽屉导航组件详解
android·flutter
csgo打的菜又爱玩1 小时前
数仓整体架构和建模架构
java·大数据·开发语言·架构