亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android三種雙屏異顯實(shí)現(xiàn)方法介紹

 更新時(shí)間:2023年01月31日 09:23:33   作者:我居然是個(gè)凡人  
現(xiàn)在越來(lái)越多的Android設(shè)備有多個(gè)屏幕,雙屏異顯應(yīng)用場(chǎng)景最多的應(yīng)該就是類似于收銀平臺(tái)那種設(shè)備,在主屏上店員能夠?qū)c(diǎn)商品進(jìn)行選擇錄入,副屏則是展示給我們的賬單詳情,但是它只通過(guò)了一個(gè)軟件系統(tǒng)就實(shí)現(xiàn)了雙屏異顯這個(gè)功能,而Presentation正是這其中的關(guān)鍵

在各種產(chǎn)品腦洞大開(kāi)的時(shí)代,需求也是日益新異,筆者最近開(kāi)發(fā)了一套雙屏異顯app?,F(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ì)上面開(kāi)啟第二屏 雙屏同顯,播放視頻,我在原來(lái)的基礎(chǔ)上做了極大的改善,可以避免啟動(dòng)兩個(gè)播放器,照成性能的浪費(fèi)

2.雙屏異顯(同顯)實(shí)現(xiàn)方式

相信做雙屏異顯的同胞們,肯定看過(guò)來(lái)Presentation 的源碼 ,源碼中顯示Presentation 是繼承與 Dialog 來(lái)實(shí)現(xiàn)的,在文章的開(kāi)頭我也有提到過(guò),第二屏可以看作一個(gè)特殊的 Dialog 來(lái)實(shí)現(xiàn)

在研究Presentation 源碼的時(shí)候發(fā)現(xiàn)它是通過(guò) Window w = getWindow(); 來(lái)獲取了一個(gè)窗口,做我們android 開(kāi)發(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),這里就在敘述了

到此這篇關(guān)于Android三種雙屏異顯實(shí)現(xiàn)方法介紹的文章就介紹到這了,更多相關(guān)Android雙屏異顯內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論