Android設備雙屏顯示

    最近公司要求在Android觸摸設備上通過hdmi接口連接外置顯示器,實現雙屏異現功能,打開軟件後外置顯示器播放動畫,觸摸設備則可以進行其他的用戶交互。搗鼓了好幾天這東西,現在和大家分享一下

    首先你的安卓設備需要支持雙屏異現功能,然後就可以進行代碼的編寫了(這裏主要通過DisplayManager和Presentation來實現)
1. 新建一個 MyPresentation類繼承Presentation,

public class MyPresentation extends Presentation {
    Context context;

    @BindView(R.id.video)
    VideoView myVideoView;

    public MyPresentation(Context outerContext, Display display) {
        super(outerContext, display);
        this.context = outerContext;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.presentation_my);
        ButterKnife.bind(this);
        final String file = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
        myVideoView.setVideoPath(file);
        myVideoView.start();
    }
}

註釋:Presentation就是Dialog類的擴展,它提供了一塊區域可以顯示不同於主屏幕的內容。這裏我放了一個VideoView播放網絡上的一個視頻

2.新建一個MyApplication類繼承Application

public class MposApplication extends Application {
    private MyPresentation mPresentation;
    private Display[] displays; //定義一個屏幕數組

    private static MposApplication sMPosApplication;

    public static MposApplication getInstance() {
        return sMPosApplication;
    }

    @Override
    public void onCreate() {
        sMPosApplication = this;
        DisplayManager mDisplayManager;// 屏幕管理類
        mDisplayManager = (DisplayManager) this
                .getSystemService(Context.DISPLAY_SERVICE);
        displays = mDisplayManager.getDisplays();
        super.onCreate();
    }

    public MyPresentation showExternalAd(Context context) {
        if (mPresentation == null) {
            mPresentation = new MyPresentation(context, displays[displays.length - 1]);// displays[1]是副屏 displays[0]是主屏
            mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            mPresentation.show();
            return mPresentation;
        } else {
            return null;
        }
    }
}

3. 在 MainActivity的oncreate中添加一句代碼

MposApplication.getInstance().showExternalAd(this);

4. 在AndroidManifest.xml中添加自定義的Application

  <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.loadingtest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

荊軻刺秦王,大功已告成

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章