安卓的双屏异显

参考文章:
布列瑟农的秋天 https://blog.csdn.net/wlwl0071986/article/details/48542923
后时代的觉悟 https://blog.csdn.net/liqianwei1230/article/details/78606935
明朗晨光 Android | 说说Presentation https://blog.csdn.net/u011386173/article/details/88717174

需求

在做项目时,有一个双屏显示的需求(类似饭店里面点餐的那种台式安卓机,两面屏幕)。百度一番,发现了 presentation 关键词是实现双屏异显功能的重点。在此记录一下笔记,供后续参考。

官方释义

如果你英语能力可以的话,官方文档请直接食用:

官方文档

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 相关联。

值得注意的是,presentation 的上下文与包含它的Activity 的上下文不同。重要的是要填充 presentation 的布局,并使用表示本身的上下文加载其他资源,以确保加载了用于目标显示的正确大小和密度的资源。

当presentation 关联的屏幕被移除后,presentation会自动被cancel,当Activity本身pause和resume的时候,Activity应该负责暂停和恢复presentation中的播放的任何内容。

解析总结

把官方文档总结整理一下,可以得出以下几点结论:

  • presentation 父类是 dialog,所以和dialog具有相似的属性和方法。
  • presentation 的容器是屏幕,创建时即需指定。屏幕移除,则presentation自动取消。
  • presentation 的上下文与包含它的Activity 的上下文不同。

使用

demo 示例

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

2.自定义类 继承Presentation

辅助显示屏的内容:

  • 需要扩展 Presentation 类,并实现 onCreate() 回调方法。在 onCreate() 中,调用setContentView() 来指定您要在辅助显示屏上显示的 UI。
  • 作为 Dialog 类的扩展,Presentation 类提供了一个区域,其中可以在辅助显示屏上显示不同的 UI。
 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.代码实现:
根据官网文档,获取显示屏的代码如下;

 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();
 }
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) {
        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;
    }
}

原理探究

presentation 处在哪个View层级?

presentation 布局

PhoneWindows $ DecorView
----LinearLayout
--------Iaction_mode bar_stub(ViewStub)
--------content(FrameLayout)
------------LinearLayout(presentation 布局的根节点)

Dialog布局

PhoneWindows $ DecorView
---- FrameLayout
---- ---- content(FrameLayout)
---- ---- ---- LinearLayout(presentation 布局的根节点)
---- ---- Iaction_mode bar_stub(ViewStub)

由此可见,presentation和dialog布局层级是一样的

琐碎知识

1.如何获取设备上的屏幕?

    DisplayManager  mDisplayManager;//屏幕管理类
    Display[]  displays;//屏幕数组
    mDisplayManager = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
    displays =mDisplayManager.getDisplays();

2.主屏和副屏的区分?

    主屏:displays[0]
    副屏:displays[1]

3.如何在副屏上展示内容?

通过Presentation来实现,Presentation继承了Dialog。假设我们写了一个DifferentDislay的类,这个类是要继承
Presentation类的。代码:

privateclass DifferentDislay extends Presentation{
    public DifferentDislay(ContextouterContext, Display display) {
        super(outerContext,display);
        //TODOAuto-generated constructor stub  
    }

    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout02);
    }
}

4.开启副屏

DifferentDislay  mPresentation =new DifferentDislay (getApplicationContext(),displays[1]);//displays[1]是副屏
mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
mPresentation.show();
发布了70 篇原创文章 · 获赞 176 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/zheng_weichao/article/details/94622847