網站首頁 編程語言 正文
在各種產品腦洞大開的時代,需求也是日益新異,筆者最近開發了一套雙屏異顯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();
所需權限:
<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 頂級窗口,看到這里我在想為何自己不能直接去創建一個窗口然后獲取屏幕數組放置在第二屏幕上呢?往下看
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 源碼實現,這里就在敘述了
原文鏈接:https://blog.csdn.net/g1998_7_9/article/details/111373249
相關推薦
- 2022-08-23 Shell?腳本自動輸入密碼的三種方式小結_linux shell
- 2022-04-15 使用python測試prometheus的實現_python
- 2022-10-13 Windows?Server?2012下FTP服務器站點搭建程序_FTP服務器
- 2023-02-09 Python去除html標簽的幾種方法總結_python
- 2022-05-17 python的列表生成式,生成器和generator對象你了解嗎_python
- 2022-06-16 gin框架中使用JWT的定義需求及解析_Golang
- 2021-11-30 Linux?Autofs自動掛載服務安裝部署教程_Linux
- 2022-06-08 Spring源碼之Bean的掃描以及創建
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支