多屏联动 - 非动画

基于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...

相关推荐
Estar.Lee26 分钟前
时间操作[计算时间差]免费API接口教程
android·网络·后端·网络协议·tcp/ip
javaDocker1 小时前
业务架构、数据架构、应用架构和技术架构
架构
找藉口是失败者的习惯1 小时前
从传统到未来:Android XML布局 与 Jetpack Compose的全面对比
android·xml
Jinkey2 小时前
FlutterBasic - GetBuilder、Obx、GetX<Controller>、GetxController 有啥区别
android·flutter·ios
JosieBook3 小时前
【架构】主流企业架构Zachman、ToGAF、FEA、DoDAF介绍
架构
.生产的驴4 小时前
SpringCloud OpenFeign用户转发在请求头中添加用户信息 微服务内部调用
spring boot·后端·spring·spring cloud·微服务·架构
大白要努力!4 小时前
Android opencv使用Core.hconcat 进行图像拼接
android·opencv
丁总学Java4 小时前
ARM 架构(Advanced RISC Machine)精简指令集计算机(Reduced Instruction Set Computer)
arm开发·架构
天空中的野鸟5 小时前
Android音频采集
android·音视频