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

04-06 阅读 0评论

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

Android三种双屏异显实现方法介绍,Android三种双屏异显实现方法介绍,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,我们,管理,第1张
(图片来源网络,侵删)

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

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

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

1

2

3

Android三种双屏异显实现方法介绍,Android三种双屏异显实现方法介绍,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,我们,管理,第2张
(图片来源网络,侵删)

4

5

6

7

8

9

Android三种双屏异显实现方法介绍,Android三种双屏异显实现方法介绍,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,我们,管理,第3张
(图片来源网络,侵删)

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

注:以上是以 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三种双屏异显实现方法介绍的文章就介绍到这了


免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,人围观)

还没有评论,来说两句吧...

目录[+]