安卓系统双屏异显_Android 双屏异显实现的三种方式

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

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

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

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);

}

}

引用:

//双屏显示

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();

所需权限:

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

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

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

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

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

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

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

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返回出去,在主类中进行操作,就解决了数据数据同步问题

以下是完整代码

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);

}

}

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

以下是调用方式

HelpHandPresentation helpHandPresentation = new HelpHandPresentation();

helpHandPresentation.addPresentation(context);

View view = helpHandPresentation.addView(layout);

三行代码即可,调用方便

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

(MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);

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

完结

这篇文章呢,作为一个开头,后续笔者会针对这一模块 发布一套三方sdk,方便大家使用,节省开发时间

欢迎业界各位同胞提一些好的意见,我会总结归纳,进行梳理,更好的完善sdk的开发进度与工作

有缘留个关注把

原文链接:blog.csdn.net/g1998_7_9/article/details/111373249

相关资源:iZotope Ozone VST (臭氧) V4.0.3.274 绿色汉化版.zip-制造工具类…

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月11日
下一篇 2021年1月11日

相关推荐