近年来,VR发展如火如荼,Google也在持续不断的为其提供技术支持。目前,谷歌主要支持硬纸板(Cardboard)和DayDream两种方式的VR体验方式。相对而言,硬纸板的用户体验效果较差,但其因为价格低廉,所以硬纸板比DayDream更加普及。
Google在自己的VR社区也详细介绍了Gvr,在Android中,支持使用Sdk和Ndk两种方式构建项目,在此仅介绍SDK方式。
首先简单介绍一下GVR的各种配置及其依赖,此部分比较简单,不废话,直接上代码。
配置GVR依赖
配置Project级build.gradle
allprojects { repositories { //确保使用jcenter仓库 jcenter() } } dependencies { // The Google VR SDK requires version 2.3.3 or higher. classpath 'com.android.tools.build:gradle:2.3.3' // The Google VR NDK requires experimental version 0.9.3 or higher. // classpath 'com.android.tools.build:gradle-experimental:0.9.3' }复制代码
添加GVR依赖库到Module
dependencies { // Adds Google VR spatial audio support //compile 'com.google.vr:sdk-audio:1.80.0' // Required for all Google VR apps compile 'com.google.vr:sdk-base:1.80.0' }复制代码
接下来再详细了解一下Gvc的各种功能及其用法,使用SDK方式集成gvr的方式中,各功能的用法都比较简单,在此做一一介绍。
Gvr的各种用法
1、使用VrPanoramaView展示全景图
见名知意,VrPanoramaView是Gvr用来展示全景图的View,用法与Button等常见组件基本一致。因为VrPanoramaView展示全景图时,需要占用大量的内存,因此需要自己管理‘生命周期’,当UI即将不可见时(onPause)需要暂停渲染,当UI开始可见时(onResume)需要开始渲染,当UI被destroy之后,需要结束渲染。
mVrPanoramaView = (VrPanoramaView) findViewById(R.id.pano_view);
//做一些全景图展示的配置,主要配置inputType,有TYPE_MONO(360单全景图)、TYPE_STEREO_OVER_UNDER(360立体全景图)
mOptions = new Options();
mOptions.inputType = TYPE_STEREO_OVER_UNDER;
//事件回调接口
mVrPanoramaView.setEventListener(new VrPanoramaEventListener() {
//加载成功
public void onLoadSuccess() {}
//加载失败
public void onLoadError(String errorMessage){}
public void onClick() {}
//支持三种模式,正常模式(1)、全屏模式(2)、VR模式(3)
public void onDisplayModeChanged(int newDisplayMode){}
})
mVrPanoramaView.loadImageFromBitmap(bitmap, mOptions);
mVrPanoramaView.loadImageFromByteArray(byte[], mOptions);复制代码
当然,还有一些其他的额外设置,如infoButton是否可见,VrButton是否可见等,在此不再介绍。
2、使用VrVideoView显示Vr视频
VrVideoView的用法也较简单,只要注意和Activity\Fragment的生命周期搭配即可,在此简单介绍一下。
VrVideoView videoWidgetView = (VrVideoView) findViewById(R.id.video_view);
videoWidgetView.setEventListener(new ActivityEventListener());
videoWidgetView.setVolume(isMuted ? 0.0f : 1.0f);
videoWidgetView.seekTo(progressTime);
//暂停播放
videoWidgetView.pauseVideo();
videoWidgetView.playVideo();
//暂停渲染,防止在后台不停耗费资源
videoWidgetView.pauseRendering();
videoWidgetView.resumeRendering();
videoWidgetView.getCurrentPosition();
videoWidgetView.getDuration();
videoWidgetView.loadVideoFromAsset("congo.mp4", options);
videoWidgetView.loadVideo(uri, options);复制代码