Android 双屏异显

需求分析:

在做一个车载项目时,有一个双屏显示的需求,当时一脸蒙逼完全不知如何着手,不过幸好有 demo,在看 demo 过程中,发现了 presentation 关键词,Google 一番,原来实现双屏异显完全是这东东起的作用。在此记录一下学习的笔记,供后续参考。

文档解析:

任何新鲜的 API,第一件事当然是上 Google 官网查阅一番。

A presentation is a special kind of dialog whose purpose is to present content on a secondary display. A Presentation is associated with the target Display at creation time and configures its context and resource configuration according to the display’s metrics.

Notably, the Context of a presentation is different from the context of its containing Activity. It is important to inflate the layout of a presentation and load other resources using the presentation’s own context to ensure that assets of the correct size and density for the target display are loaded.

A presentation is automatically canceled (see cancel()) when the display to which it is attached is removed. An activity should take care of pausing and resuming whatever content is playing within the presentation whenever the activity itself is paused or resumed.

presentation 是一个特殊的 dialog ,主要的目的是在辅助显示屏上显示内容,Presentation 在创建的时候需要和特定的 Display 相关联。
值得注意的是,在构造函数中传递的 context 必须是一个 activity 的 context。

辅助显示屏的内容:

1.您需要扩展 Presentation 类,并实现 onCreate() 回调方法。在 onCreate() 中,调用setContentView() 来指定您要在辅助显示屏上显示的 UI。
2.作为 Dialog 类的扩展,Presentation 类提供了一个区域,在其中,您的应用可以在辅助显示屏上显示不同的 UI。

获取显示Presentation的辅助显示屏:

根据官网文档,获取显示屏的代码如下;

 DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
 Display[] presentationDisplays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
 if (presentationDisplays.length > 0) {
     // If there is more than one suitable presentation display, then we could consider
     // giving the user a choice.  For this example, we simply choose the first display
     // which is the one the system recommends as the preferred presentation display.
     Display display = presentationDisplays[0];
     Presentation presentation = new MyPresentation(context, presentationDisplay);
     presentation.show();
 }

demo 示例

1. 设置权限

<!-- 显示系统窗口权限 -->
 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
 <!-- 在 屏幕最顶部显示addview-->
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

2.自定义类 继承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.second_screen);

        }
    }

3.代码实现:

public class MainActivity extends AppCompatActivity {

    private DifferentDislay mPresentation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        DisplayManager manager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = manager.getDisplays();
        // displays[0] 主屏
        // displays[1] 副屏
        DifferentDislay differentDislay = new DifferentDislay(this,displays[1]);
        differentDislay.getWindow().setType(
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        differentDislay.show();
    }

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

        }
    }
}

整体效果如下:

这里写图片描述

在辅助显示屏上播放视频:

public class MainActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {

    private static final String TAG = "Bradlley";
    private DifferentDislay mPresentation;
    private MediaPlayer mMediaPlayer;
    private SurfaceView mSurfaceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DisplayManager manager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
        Display[] displays = manager.getDisplays();
        // displays[0] 主屏
        // displays[1] 副屏
        DifferentDislay differentDislay = new DifferentDislay(this,displays[1]);
        differentDislay.getWindow().setType(
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        differentDislay.show();

        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnPreparedListener(this);

        mSurfaceView= differentDislay.getSurfaceView();
        mSurfaceView.getHolder().addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "surfaceCreated: ");
        try {
            mMediaPlayer.setDataSource("/sdcard/Movies/mv.wmv");
            mMediaPlayer.setDisplay(mSurfaceView.getHolder());
            mMediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        Log.d(TAG, "onPrepared: ");
        mMediaPlayer.start();
    }
}

DifferentDislay 代码片段:

public class DifferentDislay extends Presentation {

    private static final String TAG = "DifferentDislay";
    private SurfaceView mSurfaceView;

    public DifferentDislay(Context outerContext, Display display) {
        super(outerContext,display);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: ");
        setContentView(R.layout.second_screen);
        mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);

    }

    public SurfaceView getSurfaceView(){
        return mSurfaceView;
    }
}

happy a nice day!

  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是对代码的注释和优化: ```java package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.app.Presentation; import android.content.Context; import android.hardware.display.DisplayManager; import android.os.Bundle; import android.view.Display; import android.widget.TextView; // 继承自 Presentation 类,用于显示在外部显示设备上的内容 class MyPresentation extends Presentation { private TextView mText; public MyPresentation(Context context, Display display) { super(context, display); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 为这个 Presentation 设置布局 setContentView(R.layout.presentation_layout); // 获取布局中的 TextView,并设置显示文本 mText = findViewById(R.id.presentation_text); mText.setText("This is my presentation!"); } } public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取 DisplayManager 实例 DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE); // 获取所有的外部显示设备 Display[] displays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION); // 如果有外部显示设备,则在第一个设备上显示内容 if (displays.length > 0) { MyPresentation presentation = new MyPresentation(this, displays[0]); presentation.show(); } } } ``` 优化:添加了注释,增强了代码可读性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值