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"/>
 
<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);

        }
    }
}

整体效果如下:

Android 双屏异显_第1张图片

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

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!

你可能感兴趣的:(车载应用)