網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
在各種產(chǎn)品腦洞大開的時(shí)代,需求也是日益新異,筆者最近開發(fā)了一套雙屏異顯app。現(xiàn)在做一些總結(jié)
1.雙屏異顯第一種實(shí)現(xiàn)方式(官方提供的Presentation)
Android 提供了一個(gè)叫 Presentation 類,來(lái)實(shí)現(xiàn)第二屏, 繼承 Presentation 實(shí)現(xiàn)第二屏,相當(dāng)于一個(gè)特殊的彈窗窗口(具體實(shí)現(xiàn))
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;//屏幕數(shù)組
mDisplayManager =(DisplayManager)MainActivity.this.getSystemService(Context.DISPLAY_SERVICE);
displays =mDisplayManager.getDisplays(); //得到顯示器數(shù)組
DifferentDislay mPresentation =new DifferentDislay
(getApplicationContext(),displays[1]);//displays[1]是副屏
mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
mPresentation.show();
所需權(quán)限:
<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 實(shí)現(xiàn)的雙屏異顯,這種方式比較適合雙屏獨(dú)立操作沒(méi)有交際的時(shí)候,如果存在雙屏同顯,或者兩者之際要有一些數(shù)據(jù)同步,后比較麻煩,
比如:主屏播放適配 - >投影到第二屏,上面這種方法不適用了,因?yàn)樯婕暗竭m配同步顯示,還有主副屏幕都要啟動(dòng)一個(gè)播放器才能實(shí)現(xiàn),性能極大的浪費(fèi),設(shè)備性能比較好,還可以以這種方式實(shí)現(xiàn),如果設(shè)備性能不是很好,使用這種方式后照成視頻卡頓,嚴(yán)重者可能解碼失敗,照成視頻無(wú)法播放等等一些列并發(fā)問(wèn)題
針對(duì)上面開啟第二屏 雙屏同顯,播放視頻,我在原來(lái)的基礎(chǔ)上做了極大的改善,可以避免啟動(dòng)兩個(gè)播放器,照成性能的浪費(fèi)
2.雙屏異顯(同顯)實(shí)現(xiàn)方式
相信做雙屏異顯的同胞們,肯定看過(guò)來(lái)Presentation 的源碼 ,源碼中顯示Presentation 是繼承與 Dialog 來(lái)實(shí)現(xiàn)的,在文章的開頭我也有提到過(guò),第二屏可以看作一個(gè)特殊的 Dialog 來(lái)實(shí)現(xiàn)
在研究Presentation 源碼的時(shí)候發(fā)現(xiàn)它是通過(guò) Window w = getWindow(); 來(lái)獲取了一個(gè)窗口,做我們android 開發(fā)的都知道 Window是android 頂級(jí)窗口,看到這里我在想為何自己不能直接去創(chuàng)建一個(gè)窗口然后獲取屏幕數(shù)組放置在第二屏幕上呢?往下看
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;
}
}
上述代碼我們獲取窗口管理器,通過(guò)paramContext創(chuàng)建了第 paramContext.createDisplayContext(this.secondDisplay); 第二屏幕
創(chuàng)建好第二屏幕以后我們?nèi)ソo第二屏屏幕添加一個(gè)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;
}
這樣我們的第二屏幕就已經(jīng)完成,只需要根據(jù)自己的需求創(chuàng)建一個(gè)布局,調(diào)用addView方法添加進(jìn)去,把添加進(jìn)去的view返回出去,在主類中進(jìn)行操作,就解決了數(shù)據(jù)數(shù)據(jù)同步問(wèn)題
以下是完整代碼
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);
}
}
相當(dāng)于一個(gè)工具類,只復(fù)制到項(xiàng)目里可以直接使用
以下是調(diào)用方式
HelpHandPresentation helpHandPresentation = new HelpHandPresentation();
helpHandPresentation.addPresentation(context);
View view = helpHandPresentation.addView(layout);
三行代碼即可,調(diào)用方便
3.雙屏異顯還有一種方式是通過(guò) 投影來(lái)實(shí)現(xiàn)的,每次投影都會(huì)彈提示框,進(jìn)行確認(rèn),有一定的局限性
(MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
有興趣的可以看看 MediaProjectionManager 源碼實(shí)現(xiàn),這里就在敘述了
原文鏈接:https://blog.csdn.net/g1998_7_9/article/details/111373249
相關(guān)推薦
- 2022-11-04 Android自定義View實(shí)現(xiàn)時(shí)鐘功能_Android
- 2022-02-10 linux后臺(tái)運(yùn)行任務(wù)命令(nohup: 忽略輸入并把輸出追加到“nohup.out“)
- 2022-04-20 Django學(xué)習(xí)之路之請(qǐng)求與響應(yīng)_python
- 2022-06-15 golang中net的tcp服務(wù)使用_Golang
- 2022-11-05 Kotlin方法與Lambda表達(dá)式實(shí)踐使用介紹_Android
- 2023-04-06 python判斷列表為空的三種方法總結(jié)_python
- 2022-09-19 django安裝xadmin及問(wèn)題解決_python
- 2021-12-10 Ubuntu環(huán)境下mongodb安裝配置詳細(xì)步驟_MongoDB
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支