Android三种双屏异显实现方法介绍

本文介绍了Android设备上实现双屏异显的三种方法:官方提供的Presentation方式,自定义窗口实现以及通过MediaProjectionManager的投影。作者详细解释了每种方法的原理、代码示例和适用场景,特别提到了性能和兼容性的问题。
摘要由CSDN通过智能技术生成

现在越来越多的Android设备有多个屏幕,双屏异显应用场景最多的应该就是类似于收银平台那种设备,在主屏上店员能够对点商品进行选择录入,副屏则是展示给我们的账单详情,但是它只通过了一个软件系统就实现了双屏异显这个功能,而Presentation正是这其中的关键

在各种产品脑洞大开的时代,需求也是日益新异,笔者最近开发了一套双屏异显app。现在做一些总结

1.双屏异显第一种实现方式(官方提供的Presentation)

Android 提供了一个叫 Presentation 类,来实现第二屏, 继承 Presentation 实现第二屏,相当于一个特殊的弹窗窗口(具体实现)

1

2

3

4

5

6

7

8

9

10

public class DifferentDislay extends Presentation {

    public DifferentDislay(Context outerContext, Display display) {

        super(outerContext,display);

    }

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.diffrentdisplay);

    }

}

引用:

1

2

3

4

5

6

7

8

9

//双屏显示

DisplayManager mDisplayManager;//屏幕管理类

Display[] displays;//屏幕数组

mDisplayManager =(DisplayManager)MainActivity.this.getSystemService(Context.DISPLAY_SERVICE);

displays =mDisplayManager.getDisplays(); //得到显示器数组

DifferentDislay mPresentation =new DifferentDislay

(getApplicationContext(),displays[1]);//displays[1]是副屏

mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

mPresentation.show();

所需权限:

1

2

3

4

5

6

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<uses-permission android:name="android.permission.TYPE_APPLICATION_OVERLAY" />

<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

注:以上是以 Presentation 实现的双屏异显,这种方式比较适合双屏独立操作没有交际的时候,如果存在双屏同显,或者两者之际要有一些数据同步,后比较麻烦,

比如:主屏播放适配 - >投影到第二屏,上面这种方法不适用了,因为涉及到适配同步显示,还有主副屏幕都要启动一个播放器才能实现,性能极大的浪费,设备性能比较好,还可以以这种方式实现,如果设备性能不是很好,使用这种方式后照成视频卡顿,严重者可能解码失败,照成视频无法播放等等一些列并发问题

针对上面开启第二屏 双屏同显,播放视频,我在原来的基础上做了极大的改善,可以避免启动两个播放器,照成性能的浪费

2.双屏异显(同显)实现方式

相信做双屏异显的同胞们,肯定看过来Presentation 的源码 ,源码中显示Presentation 是继承与 Dialog 来实现的,在文章的开头我也有提到过,第二屏可以看作一个特殊的 Dialog 来实现

在研究Presentation 源码的时候发现它是通过 Window w = getWindow(); 来获取了一个窗口,做我们android 开发的都知道 Window是android 顶级窗口,看到这里我在想为何自己不能直接去创建一个窗口然后获取屏幕数组放置在第二屏幕上呢?往下看

1

2

3

4

5

6

7

8

9

public void addPresentation(Context paramContext) {

        Display display = ((MediaRouter) paramContext.getSystemService(Context.MEDIA_ROUTER_SERVICE)).getSelectedRoute(2).getPresentationDisplay();

        this.secondDisplay = display;

        if (display != null) {

            this.windowManager = (WindowManager) paramContext.createDisplayContext(display).getSystemService(Context.WINDOW_SERVICE);

            this.secondDisplayContext = paramContext.createDisplayContext(this.secondDisplay);

            return;

        }

    }

上述代码我们获取窗口管理器,通过paramContext创建了第 paramContext.createDisplayContext(this.secondDisplay); 第二屏幕

创建好第二屏幕以后我们去给第二屏屏幕添加一个view

1

2

3

4

5

6

7

8

9

10

11

public View addView(int paramInt) {

    this.view = View.inflate(this.secondDisplayContext, paramInt, null);

    this.layoutParams = new WindowManager.LayoutParams(2003, 3, 3);

    if (Build.VERSION.SDK_INT >= 23) {

        this.layoutParams.type = 2038;

    } else {

        this.layoutParams.type = 2003;

    }

    this.windowManager.addView(this.view, this.layoutParams);

    return this.view;

}

这样我们的第二屏幕就已经完成,只需要根据自己的需求创建一个布局,调用addView方法添加进去,把添加进去的view返回出去,在主类中进行操作,就解决了数据数据同步问题

以下是完整代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

public class HelpHandPresentation {

    private WindowManager.LayoutParams layoutParams;

    private Display secondDisplay;

    private Context secondDisplayContext;

    private View view;

    private WindowManager windowManager;

    public void addPresentation(Context paramContext) {

        Display display = ((MediaRouter) paramContext.getSystemService(Context.MEDIA_ROUTER_SERVICE)).getSelectedRoute(2).getPresentationDisplay();

        this.secondDisplay = display;

        if (display != null) {

            this.windowManager = (WindowManager) paramContext.createDisplayContext(display).getSystemService(Context.WINDOW_SERVICE);

            this.secondDisplayContext = paramContext.createDisplayContext(this.secondDisplay);

            return;

        }

    }

    public View addView(int paramInt) {

        this.view = View.inflate(this.secondDisplayContext, paramInt, null);

        this.layoutParams = new WindowManager.LayoutParams(2003, 3, 3);

        if (Build.VERSION.SDK_INT >= 23) {

            this.layoutParams.type = 2038;

        } else {

            this.layoutParams.type = 2003;

        }

        this.windowManager.addView(this.view, this.layoutParams);

        return this.view;

    }

    public void presentationAddView() {

        this.windowManager.addView(this.view, this.layoutParams);

    }

    public void removeLayoutView() {

        this.windowManager.removeView(this.view);

    }

}

相当于一个工具类,只复制到项目里可以直接使用

以下是调用方式

1

2

3

HelpHandPresentation helpHandPresentation = new HelpHandPresentation();

helpHandPresentation.addPresentation(context);

View view = helpHandPresentation.addView(layout);

三行代码即可,调用方便

3.双屏异显还有一种方式是通过 投影来实现的,每次投影都会弹提示框,进行确认,有一定的局限性

1

(MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);

有兴趣的可以看看 MediaProjectionManager 源码实现,这里就在叙述了

到此这篇关于Android三种双屏异显实现方法介绍的文章就介绍到这了

  • 18
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要UniApp中实现安卓主副屏异显,您可以使用Android原生的API来实现。下面是一个简单的示例: 1. 在UniApp项目中创建一个自定义插件(plugin)用于调用原生Android代码。可以参考UniApp的插件开发文档来创建插件。 2. 在插件的Java代码中,使用Android的Presentation类来创建一个副屏幕的显示。 ```java import android.content.Context; import android.hardware.display.DisplayManager; import android.os.Bundle; import android.view.Display; public class SecondaryDisplayPresentation extends Presentation { public SecondaryDisplayPresentation(Context context, Display display) { super(context, display); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 在这里设置副屏幕的显示内容 setContentView(R.layout.secondary_display_layout); } } ``` 3. 在插件的JS代码中,通过调用原生方法来创建并显示副屏幕。 ```javascript export default { async createSecondaryDisplay() { const result = await uni.requireNativePlugin('YourPluginName').createSecondaryDisplay(); if (result.success) { uni.showToast({ title: '副屏幕显示成功', icon: 'success' }); } else { uni.showToast({ title: '副屏幕显示失败', icon: 'none' }); } } } ``` 4. 在UniApp的页面中,调用插件方法来创建副屏幕。 ```html <template> <view> <button @click="createSecondaryDisplay">创建副屏幕</button> </view> </template> <script> import YourPluginName from '@/plugins/your-plugin-name'; export default { methods: { createSecondaryDisplay() { YourPluginName.createSecondaryDisplay(); } } } </script> ``` 请注意,上述代码只是一个简单示例,实际情况可能会更复杂。您需要根据您的具体需求进行相应的修改和调整。 同时,需要注意的是,副屏幕的异显功能在不同的设备和Android版本上可能会有所不同。您可能需要在原生开发中处理一些设备和版本兼容性的问题。 希望这对您有所帮助!如果您有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值