日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2023/12/2 Android 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓系统双屏异显_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,作者:我居然是個凡人,版權歸原作者所有,如需轉載,請聯系作者。

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

總結

以上是生活随笔為你收集整理的安卓系统双屏异显_Android 双屏异显实现的三种方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。